3.1 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Change Axis Intervals in R Plots (With Examples)

How to Change Axis Intervals 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...

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

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