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

 

default axis 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 ticks 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")

 

without x axis plot r

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])

 

custom axis r

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 to have 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.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

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.

 

In summary: You learned in this tutorial how to change the values on our axis scale in the R programming language. If you have further questions, please let me know in the comments section below.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top