Remove grid and background from plot using ggplot2 in R
A plot by default is produced with a grid background and grayish colored background. In this article we will see, how a gird can be removed from a plot. We will use a line plot, for demonstration but the same can be employed for any other Visualization.
To understand the difference better let us first create a plot with grid and background.
df <- data.frame(a=c(2,4,8), b=c(5, 10, 15))
plot = ggplot(df, aes(x = a, y = b)) + geom_point() + geom_line()
#output
plot
Output:

Now, We are going to perform some operation/ modification on this plot.
Remove Grid:
Assigning the grid's major and minor with the element_blanck() function, will remove the grid but not remove the background color and borderlines.
df <- data.frame(a=c(2,4,8),b=c(5, 10, 15))
plot + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
Output:

Only Grid and Axis Line:
Using theme_bw() function with the plot removes the grayish background but doesn't affect the Grid.
df <- data.frame(a=c(2,4,8),b=c(5, 10, 15))
# theme_bw() function
plot + theme_bw()
Output:

Remove Background and Grid:
Assigning panel.background with element_blank() function will remove both grid and the background.
df <- data.frame(a=c(2,4,8),b=c(5, 10, 15))
plot + theme(panel.background = element_blank())
Output:
