8.5 C
London
Monday, December 30, 2024
HomeStatistics TutorialRHow to Change Axis Scales in R Plots (With Examples)

How to Change Axis Scales in R Plots (With Examples)

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 change the scale used on an axis in R plots.

This tutorial explains how to change axis scales on plots in both base R and ggplot2.

Example 1: Change Axis Scales in Base R

To change the axis scales on a plot in base R, we can use the xlim() and ylim() functions.

The following code shows how to use these functions in practice:

#define data
df frame(x=c(1, 3, 3, 4, 6, 8, 12, 13, 15, 18, 21, 22),
                 y=c(13, 15, 9, 17, 22, 25, 29, 35, 39, 44, 45, 40))

#create plot with default axis scales
plot(df$x, df$y, pch=19, main='Default Axes')

#create plot with custom axis scales
plot(df$x, df$y, pch=19, xlim=c(0,30), ylim=c(0,150), main='Custom Axes')

Change axis scales in R plots

Note that you can also quickly transform one of the axes to a log scale by using the log argument. For example, the following code shows how to transform the y-axis to a log scale:

#define data
df frame(x=c(1, 3, 3, 4, 6, 8, 12, 13, 15, 18, 21, 22),
                 y=c(13, 15, 9, 17, 22, 25, 29, 35, 39, 44, 45, 40))

#create plot with log y-axis
plot(df$x, df$y, log='y', pch=19)

Example 2: Change Axis Scales in ggplot2 

To change the axis scales on a plot in base R, We can also use the xlim() and ylim() functions to change the axis scales

The following code shows how to use these functions in practice:

library(ggplot2)

#define data
df frame(x=c(1, 3, 3, 4, 6, 8, 12, 13, 15, 18, 21, 22),
                 y=c(13, 15, 9, 17, 22, 25, 29, 35, 39, 44, 45, 40))

#create scatterplot with custom axes
ggplot(data=df, aes(x=x, y=y)) +
  geom_point() +
  xlim(0, 30) +
  ylim(0, 150)

We can also transform either of the axes to a log scale by using the following arguments:

  • scale_x_continuous(trans=’log10′)
  • scale_y_continuous(trans=’log10′)

For example, the following code shows how to transform the y-axis to a log scale:

library(ggplot2)

#define data
df frame(x=c(1, 3, 3, 4, 6, 8, 12, 13, 15, 18, 21, 22),
                 y=c(13, 15, 9, 17, 22, 25, 29, 35, 39, 44, 45, 40))

#create scatterplot with log y-axis
ggplot(data=df, aes(x=x, y=y)) +
  geom_point() +
  scale_y_continuous(trans='log10')

 

You can find more R data visualization tutorials on this page.

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