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
Figure 1: Plot with Default X-Axis Values.
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")
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])
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.
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.