How can I set axis limits in ggplot2?

“Setting axis limits in ggplot2 allows for precise control over the range of values displayed on the x and y axes of a graph. This feature is particularly useful in visualizing data that may have extreme or outlier values. By specifying the minimum and maximum values for each axis, users can effectively zoom in or out on specific portions of their data, providing a more accurate and informative representation. This can be done by using the ‘xlim’ and ‘ylim’ functions within the ggplot2 package in R, allowing for customizable and tailored visualizations.”

Set Axis Limits in ggplot2


Often you may want to set the axis limits on a plot using ggplot2. You can easily do this using the following functions:

  • xlim(): specifies the lower and upper limit of the x-axis.
  • ylim(): specifies the lower and upper limit of the y-axis.

Note that both of these methods will remove data outside of the limits, which can sometimes produce unintended consequences. To change the axis limits without dropping data observations, you can instead use coord_cartesian():

  • coord_cartesian(): specifies the limits for the x-axis and y-axis without dropping observations.

This tutorial explains several ways to use these functions using the following scatterplot made with the built-in R dataset mtcars:

#load ggplot2
library(ggplot2)

#create scatterplot
ggplot(mtcars, aes(mpg, wt)) +
  geom_point()

Example 1: Set X-Axis Limits Using xlim()

The following code shows how to set the x-axis limits of the scatterplot using the xlim() function:

#create scatterplot with x-axis ranging from 15 to 30
ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  xlim(15, 30)

Warning message:
“Removed 9 rows containing missing values (geom_point).”

Setting x-axis limits in ggplot2

You can also use NA to only set the upper limit of the x-axis and let ggplot2 automatically choose the lower limit:

#create scatterplot with x-axis upper limit at 30
ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  xlim(NA, 30)

Warning message:
“Removed 4 rows containing missing values (geom_point).”

How to set axis limits in ggplot2

Example 2: Set Y-Axis Limits Using ylim()

The following code shows how to set the y-axis limits of the scatterplot using the ylim() function:

#create scatterplot with y-axis ranging from 2 to 4
ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  ylim(2, 4)

Warning message:
“Removed 8 rows containing missing values (geom_point).”

Set y-axis limits in ggplot2

You can also use NA to only set the lower limit of the y-axis and let ggplot2 automatically choose the upper limit:

#create scatterplot with y-axis lower limit at 2
ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  xlim(2, NA)

Warning message:
“Removed 4 rows containing missing values (geom_point).”

Example 3: Set Axis Limits Using coord_cartesian()

The following code shows how to set the y-axis limits of the scatterplot using the coord_cartesian() function:

#create scatterplot with y-axis ranging from 2 to 4
ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  coord_cartesian(xlim =c(15, 25), ylim = c(3, 4))

Set axis limits in ggplot2 using coord_cartesian() function

You can find more ggplot2 tutorials here.

x