Replace X-Axis Values in R (Example) | How to Change & Customize Ticks
In this R tutorial you’ll learn how to modify x-axis values manually.
The content of the post looks as follows:
Let’s get started.
Creating Example Plot
Let’s first create an exemplifying graphic in R:
plot(1:5) # Create plot in Base R |
plot(1:5) # Create plot in Base R
Figure 1: Plot with Default X-Axis Values.
Figure 1 shows the output of the previous R code: A scatterplot with an x-axis ranging from 1 to 5.
Example: Changing X-Axis Values to Letters
Let’s assume that we want to modify our x-axis tick marks so that the currently shown numbers are replaced by upper case letters. Then, we first have to create a graph without any axis values:
plot(1:5, # Create plot without x-axis xaxt = "n", xlab = "My X-Axis") |
plot(1:5, # Create plot without x-axis xaxt = "n", xlab = "My X-Axis")
Figure 2: Plot without X-Axis Values.
Figure 2 shows our plot without axis values.
Now, we can add custom axis values by using the axis function as shown below:
axis(1, # Define x-axis manually at = 1:5, labels = LETTERS[1:5]) |
axis(1, # Define x-axis manually at = 1:5, labels = LETTERS[1:5])
Figure 3: Plot with Custom X-Axis Values.
Figure 3 shows our final result: A scatterplot with custom axis values.
Video & Further Resources
Do you need more explanations on the R codes of this article? Then I can recommend having a look at the following video instruction which I have published on my YouTube channel. I show the R programming syntax of the present tutorial in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might have a look at the related articles that I have published on my website. I have released numerous tutorials already.
- Remove Axis Values of Plot in Base R
- Rotate Axis Labels of Base R Plot
- axis() Function in R
- Change Formatting of Numbers of ggplot2 Plot Axis
- R Graphics Gallery
- The R Programming Language
In summary: You have learned in this tutorial how to change the values on our axis scale in the R programming language.
In this tutorial, I have illustrated how to do this based on an exemplifying scatterplot. However, please note that it would also be possible to change the axis values of other types of graphs such as a histogram, boxplot, barchart, line plot, or a density plot by using the same kind of R code.
If you have further questions, please let me know in the comments section below.
Statistics Globe Newsletter
4 Comments. Leave new
How can I customize ticks to string values? E.g. “Control”, “Trial 1”, “Trial 2”.
Thank you for any help.
Hey,
You can add basically any text you want by using the label argument in the axis function. Have a look at the following example code:
Regards,
Joachim
How can I customize range in the axis, for instance point A= 5-10, B=10-15 and so on
Hey Apsana,
Are you looking for this?
Regards,
Joachim