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

`grid()`

functionThe 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

`abline()`

functionThe 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

`axis()`

functionThe `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**:

- text() function in R: Add the text labels to the plot
- The lines Function In R: Adding Lines To A Plot
- Get column names in r

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.

**Name of the university: **HUST

**Major**: IT

**Programming Languages**: Python, C, C++, Machine Learning/Deep Learning/NLP