Set Axis Limits of ggplot2 Facet Plot in R – ggplot2

28 Nov, 2021
In this article, we will discuss how to set the axis limits of the ggplot2 facet plot in the R programming language.
Here the role of the ggplot2 package is to plot the facet plot and provide some functionalities to the user, further the user needs to set the argument of the scales function to “free” this will be freely set the axis limits of the facet ggplot2 plot.
Scale function:
This is a generic function whose default method centers and/or scales the columns of a numeric matrix.
Syntax: scale(x, center = TRUE, scale = TRUE)
Parameters:
x: a numeric matrix
center: either a logical value or numeric-alike vector of length equal to the number of columns of x
scale: either a logical value or a numeric-alike vector of length equal to the number of columns of x.
Example:
In this example, we will be looking at the facet plot created with 100 random data points and then with the help of the free scaling method, we will be limiting the axis of the facet ggplot2 in the R programming language.
Initial plot with setting the axis limits:
Code:
R
# load ggplot2library("ggplot2")  # Data from the facet plotx1 < -rnorm(100)x2 < -rnorm(100)+x1grp < - rbinom(100, 1, 0.1)  x1[grp == 1] < - x1[grp == 1] * 5x2[grp == 1] < - x2[grp == 1] * 5  # Data from the facet plotgfg < - data.frame(x1, x2, grp)  # facet plot with facet_wrapgfg_plot < - ggplot(gfg, aes(x1, x2)) +geom_point() + facet_wrap(~ grp)  # Draw plot with free scalesgfg_plot + facet_wrap(~ grp, scales="free")
Output:
 Here the role of the ggplot2 package is to plot the facet plot and provide some functionalities to the user, further the user needs to set the argument of the scales function to “free_x” this will be freely set the axis limits of x-axis of the facet ggplot2 plot and the remaining y-axis will be unchanged.
Example:
In this example, we will be looking at the facet plot created with 100 random data points and then with the help of the free X-axis scaling method, where the x-axis of the plot with be changing the limits and the remaining y-axis will be unchanged the facet ggplot2 in the R programming language.
Initial plot with setting the axis limits:
Code:
R
# load ggplot2library("ggplot2")  # Data from the facet plotx1 < -rnorm(100)x2 < -rnorm(100)+x1grp < - rbinom(100, 1, 0.1)x1[grp == 1] < - x1[grp == 1] * 5x2[grp == 1] < - x2[grp == 1] * 5  # Data from the facet plotgfg < - data.frame(x1, x2, grp)  # facet plot with facet_wrapgfg_plot < - ggplot(gfg, aes(x1, x2)) +geom_point() + facet_wrap(~ grp)  # Draw plot with free x-axis scalesgfg_plot + facet_wrap(~ grp, scales="free_x")
Output:
Here the role of the ggplot2 package is to plot the facet plot and provide some functionalities to the user, further the user needs to set the argument of the scales function to “free_y” this will be freely set the axis limits of y-axis of the facet ggplot2 plot and the remaining x-axis will be unchanged.
Example:
In this example, we will be looking at the facet plot created with 100 random data points and then with the help of the free Y-axis scaling method, where the y-axis of the plot with be changing the limits and the remaining x-axis will be unchanged the facet ggplot2 in the R programming language.
Initial plot with setting the axis limits:
Code:
R
# load ggplot2library("ggplot2")  # Data from the facet plotx1 < -rnorm(100)x2 < -rnorm(100)+x1grp < - rbinom(100, 1, 0.1)  x1[grp == 1] < - x1[grp == 1] * 5x2[grp == 1] < - x2[grp == 1] * 5  # Data from the facet plotgfg < - data.frame(x1, x2, grp)  # facet plot with facet_wrapgfg_plot < - ggplot(gfg, aes(x1, x2)) +geom_point() + facet_wrap(~ grp)  # Draw plot with free y-axis scalesgfg_plot + facet_wrap(~ grp, scales="free_y")
Output:
Here, the user needs to set the argument of the scales function to “free_x” this will be freely set the axis limits of y-axis of the facet ggplot2 plot and the remaining x-axis will be changed using the ylim function which is the manual setting up the plot axis .
Example:
In this example, we will be looking at the facet plot created with 100 random data points and then with the help of the free X-axis scaling method, where the x-axis of the plot with be changing the limits and the remaining y-axis will be changed  with the help of the ylim() function the facet ggplot2 in the R programming language.
Initial plot with setting the axis limits:-
Code:
R
# load ggplot2library("ggplot2")    # Data from the facet plotx1<-rnorm(100)            x2<-rnorm(100)+x1grp <- rbinom(100, 1, 0.1)x1[grp == 1] <- x1[grp == 1] * 5x2[grp == 1] <- x2[grp == 1] * 5  # Data from the facet plotgfg <- data.frame(x1,x2, grp)        # facet plot with facet_wrapgfg_plot <- ggplot(gfg, aes(x1,x2)) +            geom_point() +  facet_wrap(~ grp)gfg_plot + facet_wrap(~ grp, scales = "free_x")+coord_cartesian(ylim = c(-20,20))
Output:
Picked
R-ggplot
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?
R - if statement
Logistic Regression in R Programming
Replace Specific Characters in String in R
How to import an Excel File into R ?
Joining of Dataframes in R Programming