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 <- data.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')

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 <- data.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 <- data.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 <- data.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.