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

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

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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