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

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

by Tutor Aspire

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 #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 #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 #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

You may also like