Remove Vertical or Horizontal Gridlines in ggplot2 Plot in R
Last Updated :
14 Sep, 2021
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
R
library("ggplot2")
# Store 10 entries of data in data frame
A <- data.frame(x = 1:10, y = c(1,4,2,3,7,5,4,8,2,5))
# A ggplot2 plot with gridlines
p <- ggplot(A, aes(x, y)) + geom_point()
# Display the plot
p
Output:
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.
Syntax:
scale_x_continuous(breaks =NULL )
Example: Removing vertical gridlines
R
library("ggplot2")
# Store 10 entries of data in data frame
A <- data.frame(x = 1:10, y = c(1,4,2,3,7,5,4,8,2,5))
# A ggplot2 plot with gridlines
p <- ggplot(A, aes(x, y)) + geom_point()
# Remove Vertical Gridlines
p + scale_x_continuous(breaks = NULL)
Output:
Similarly for removing horizontal gridlines break parameter of scale_y_continuous() is set to NULL.
Example: Removing horizontal lines
R
library("ggplot2")
# Store 10 entries of data in data frame
A <- data.frame(x = 1:10, y = c(1,4,2,3,7,5,4,8,2,5))
# A ggplot2 plot with gridlines
p <- ggplot(A, aes(x, y)) + geom_point()
# Remove Horizontal Gridlines
p + scale_y_continuous(breaks = NULL)
Output:
Similar Reads
Add Vertical and Horizontal Lines to ggplot2 Plot in R In this article, we will see how to add Vertical and Horizontal lines to the plot using ggplot2 in R Programming Language. Adding Vertical Line To R Plot using geom_vline() For adding the Vertical line to the R plot, geom_vline() draws a vertical line at a specified position. Syntax: geom_vline(xint
4 min read
How to suppress the vertical gridlines using ggplot2 in R? In this article, we will discuss various ways of suppressing vertical gridlines in ggplot using an R programming language. Let's first draw a regular plot without making any changes, so the difference is traceable: R library("ggplot2") function1<- function(x){x**2} function2<-function(x){x**3}
1 min read
Remove NA Values from ggplot2 Plot in R In this article, we are going to see how to remove the NA values from the ggplot2 plot in the R programming language. Using complete.cases() function complete.cases() function: This function will be returning a logical vector indicating which cases are complete, i.e., have no missing values. Syntax:
2 min read
Plot Only One Variable in ggplot2 Plot in R In this article, we will be looking at the two different methods to plot only one variable in the ggplot2 plot in the R programming language. Draw ggplot2 Plot Based On Only One Variable Using ggplot & nrow Functions In this approach to drawing a ggplot2 plot based on the only one variable, firs
5 min read
Remove Axis Labels and Ticks in ggplot2 Plot in R In this article, we will discuss how to remove axis labels and ticks in ggplot2 in R Programming Language. The axes labels and ticks can be removed in ggplot using the theme() method. This method is basically used to modify the non-data components of the made plot. It gives the plot a good graphical
2 min read
Adding Partial Horizontal Lines with ggplot2 in R Visualizing data effectively often requires emphasizing specific aspects or values. In ggplot2, horizontal lines help highlight thresholds, averages, or other reference values. While geom_hline() allows adding full horizontal lines across the entire plot, there are situations where you may want to d
4 min read