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


You can use the following basic syntax to change axis intervals on a plot in base R:

#create plot with no axis intervals
plot(x, y, xaxt='n', yaxt='n')

#specifty x-axis interval
axis(side=1, at=c(1, 5, 10, 15))

#specify y-axis interval
axis(side=2, at=seq(1, 100, by=10))

The following examples show how to use this syntax in practice.

Example 1: Specify Axis Intervals Using Individual Values

The following code shows how to modify the x-axis and y-axis intervals in a plot in base R using the c() function:

#define data
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
y <- c(1, 3, 3, 4, 6, 7, 8, 14, 17, 15, 14, 13, 19, 22, 25)

#create scatterplot
plot(x, y, col='steelblue', pch=19, xaxt='n', yaxt='n')

#modify x-axis and y-axis intervals
axis(side=1, at=c(1, 5, 10, 15))
axis(side=2, at=c(1, 12.5, 25))

Notice that the only values shown along the x-axis and y-axis are the specific values that we specified.

Example 2: Specify Axis Intervals Using a Sequence of Values

The following code shows how to modify the x-axis and y-axis intervals in a plot in base R using the seq() function:

#define data
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
y <- c(1, 3, 3, 4, 6, 7, 8, 14, 17, 15, 14, 13, 19, 22, 25)

#create scatterplot
plot(x, y, col='steelblue', pch=19, xaxt='n', yaxt='n')

#modify x-axis and y-axis intervals
axis(side=1, at=seq(5, 15, by=5))
axis(side=2, at=seq(0, 25, by=5))

Notice that the only values shown along the x-axis and y-axis are the values we specified using the seq() function.

Example 3: Specify Axis Intervals Using a Range of Values

The following code shows how to modify the x-axis interval in a plot in base R using the : function:

#define data
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
y <- c(1, 3, 3, 4, 6, 7, 8, 14, 17, 15, 14, 13, 19, 22, 25)

#create scatterplot
plot(x, y, col='steelblue', pch=19, xaxt='n')

#modify x-axis interval
axis(side=1, at=1:15)

Notice that base R automatically produced y-axis interval values and then used the range of x-axis interval values that we specified.

Additional Resources

The following tutorials explain how to perform other common plotting operations in R:

How to Set Axis Limits in R
How to Change Axis Scales in R
How to Draw a Legend Outside of a Plot in R

Leave a Reply

Your email address will not be published.