10.7 C
London
Sunday, July 7, 2024
HomeTidyverse in Rggplot2 in RHow to Set Axis Limits in ggplot2

How to Set Axis Limits in ggplot2

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories