Remove Grid, Background Color, Top & Right Borders from ggplot2 Plot in R (Example)


In this tutorial you’ll learn how to delete background grids, colors, and top/right borders of ggplot2 plots in the R programming language.

The table of content is structured like this:

Let’s take a look at some R codes in action…


Creation of Example Data

First, we need to create some example data in R:

set.seed(121232)                                        # Create example data
data <- data.frame(x = rnorm(20),
                   y = rnorm(20))
head(data)                                              # Print example data
#            x          y
# 1  1.8418341  0.1200722
# 2 -0.6052858  1.1024212
# 3 -1.0399547 -0.8373018
# 4  0.2559966 -0.1287953
# 5 -0.1262574  3.0912941
# 6 -0.6611680 -0.8596049

As you can see based on the output of the RStudio console, our exemplifying data frame consists of two numeric columns x and y.

We also need to install and load the ggplot2 package:

install.packages("ggplot2")                             # Install and load ggplot2

Now, we can draw a graphic with the ggplot2 package as shown below:

ggp <- ggplot(data, aes(x, y)) +                        # Create basic ggplot
ggp                                                     # Draw ggplot


basic line plot in r ggplot2

Figure 1: ggplot2 Line Plot with Default Layout Specifications.


Figure 1 shows the output of the previous R code: A ggplot2 plot with default specifications of the background colors, the grid panel, and the lines around the graph.


Example: Reduced Layout of ggplot2 Plot in R

If we want to remove the background grid, colors, and the top and right borders from our ggplot2 plot, we can use the theme function in combination with the axis.line, panel.grid.major, panel.grid.minor, panel.border, and panel.background arguments. Have a look at the following R programming syntax:

ggp +                                                   # Remove grid, color & borders
  theme(axis.line = element_line(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())


reduced layout of ggplot2 plot in r

Figure 2: ggplot2 Plot without Grid, Background Color, and Top and Right Borders.


As you can see in Figure 2, we have removed all colors and grid elements as well as the border lines at the top and on the left side of the ggplot2 graph.


Video & Further Resources

In case you need further information on the R programming codes of this page, you might want to have a look at the following video of my YouTube channel. I show the topics of this tutorial in the video:


The YouTube video will be added soon.


Also, you could read the other tutorials on this homepage.


In this tutorial you learned how to improve the layout of ggplot2 plots by removing certain unnecessary parts of the graph in R. If you have further questions, let me know in the comments section.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.