Add Grid Line Consistent with Ticks on Axis to Plot in R (2 Examples)

 

In this post, I’ll demonstrate how to draw a grid with tick marks on the corresponding default axis in R programming.

Table of contents:

Let’s do this…

 

Example 1: Add Grid with Certain Number of Lines to Plot

This example demonstrates how to draw a particular number of grid lines to a Base R plot.

For this, we can use the plot and grid functions as shown below:

plot(1:10)                           # Draw plot
grid(10, 10)                         # Add grid

 

r graph figure 1 add grid line consistent ticks on axis r

 

In Figure 1 it is shown that we have created a plot with grid lines by executing the previous R programming syntax.

However, you can also see that these grid lines are not aligned to the tick marks of our plot. In the next example, I’ll explain how to do that!

 

Example 2: Align Grid with Tick Marks on Corresponding Default Axis

Example 2 demonstrates how to align grid lines to the tick marks of a Base R graph.

For this, we have to specify NULL within the grid function as shown below:

plot(1:10)                           # Draw plot
grid(NULL, NULL)                     # Add grid

 

r graph figure 2 add grid line consistent ticks on axis r

 

After running the previous syntax the plot with grid lines aligned to the axis tick marks shown in Figure 2 has been created.

 

Video, Further Resources & Summary

If you need further explanations on the R programming code of this tutorial, I recommend having a look at the following video on my YouTube channel. I explain the R syntax of this article in the video.

 

 

In addition, you could have a look at the other tutorials on this website.

 

At this point in the article you should have learned how to add a grid to a graphic with tick marks on the corresponding default axis in R. Please let me know in the comments section below, if you have any further questions.

 

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.

Top