Adding grid in R: How to create grid lines in R

grid in r

Visualization plays an important part in exploring data. In this tutorial, we will show you how to add a grid to the figure to have an intuitive look at the data. Keep reading; all you need are available below.

Grid in R

In R, the grid is vertical and horizontal lines dividing the figure into rectangular cells. There are three functions to create the grid: the grid() function, the abline() function, and the axis() function.

Create grid lines in R

Use the grid() function

The name of the function tells us all about its function. The grid() function is used to create grids both vertically and horizontally. 

Syntax:

grid(nx, ny, col, lty, lwd)

Parameters:

  • nx: Number of cells in the x direction
  • ny: Number of cells in the y direction
  • col: Color of the grid lines
  • lty: Type of the grid lines
  • lwd: Width of the grid lines

Code:

set.seed(0)

# Create 100 normally distributed random numbers
x <- rnorm(100, mean = 0, sd = 1)

# Plot the numbers
plot(x, pch = 19, col = "red")

# Add grid lines to the figure
grid(nx = 3, ny = 3, lty = "dotted", col = "gray", lwd = 1)

Result:

Use the abline() function

The previous article showed you how to draw straight lines horizontally and vertically. You can also add grid lines indirectly by adding horizontal and vertical lines.

Code:

set.seed(0)

# Create 100 normally distributed random numbers
x <- rnorm(100, mean = 0, sd = 1)

# Range of the grid lines
x_line <- seq(0, 100, 20)
y_line <- seq(-2, 2, 1)

# Plot the numbers
plot(x, pch = 19, col = "red")

# Add verticle grid lines
abline(v = x_line, lty = "dotted", col = "green")

# Add horizontal grid lines
abline(h = y_line, lty = "dotted", , col = "green")

Result:

Use the axis() function

The axis() functions add axes to the figure. The function will draw the axes as grid lines when passing value 1 to the ‘tck’ parameter. 

Syntax:

axis(side, at, labels, …)

Parameters:

  • side: Side to draw axis: 1, 2, 3, 4 for below, left, above, and right, respectively.
  • at: Point to draw tick-mark.
  • labels: Name for tick-mark labels.

For more parameters, visit the official website.

Code:

set.seed(0)

# Create 100 normally distributed random numbers
x <- rnorm(100, mean = 0, sd = 1)

# Range of the grid lines
x_line <- seq(0, 100, 20)
y_line <- seq(-2, 2, 1)

# Plot the numbers
plot(x, pch = 19, col = "red")

# Draw the verticle axes as grid lines
axis(side = 1, tck = 1, lty = 2, col = "green")

# Draw the horizontal axes as grid lines
axis(side = 2, tck = 1, lty = 2, col = "green")

Result:

Summary

In summary, the grid() function is the formal function to add grid lines into figures. Moreover, you can also use the abline() and axis() functions to create vertical and horizontal lines as grid lines. However, we suggest using the grid() function as the most effective way.

Maybe you are interested:

Posted in R

Leave a Reply

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