Remove Vertical or Horizontal Gridlines in ggplot2 Plot in R
In this article, we are going to learn how to remove Vertical or Horizontal Gridlines in the background of a ggplot2 Plot in R programming language.
To understand how to remove Vertical or Horizontal Gridlines, first import all the required libraries to the working space and create a dataframe. Let us first create a regular plot so that the difference is apparent.
Example: default plot
Now for removing gridlines, separate functions are added while creating a plot. In ggplot2, scales control how the data is mapped into aesthetics. When we provide some data, it takes the data and converts it into something visual for us like position, size, color, or shape.
Scales toolbox can have different attributes like the following two main types:
- Position scales and axes
- Colour scales and legend
In order to remove gridlines, we are going to focus on position scales. There are two position scales in a plot corresponding to x and y aesthetics. Two most common types of continuous position scales are the default scale_x_continuous() and scale_y_continuous() functions. We are going to use these functions to remove the gridlines.
To remove vertical grid lines scale_x_continuous() function is passed with the breaks parameter as NULL.
scale_x_continuous(breaks =NULL )
Example: Removing vertical gridlines
Similarly for removing horizontal gridlines break parameter of scale_y_continuous() is set to NULL.
Example: Removing horizontal lines