Both the grid and the background colors (i.e. the color of the area that contains the data points and the color behind the area that surrounds the plot) are probably not critical elements of the plot, but may give a better look to your plot, or on the contrary make the plot look unreadable, or atrociously confusing. Here is a quick overview of the codes that let you modify them easily.

Let’s start with the code for a reference violin plot. This plot is stored in the object baseplot so that we can reuse it throughout the whole tutorial:

baseplot <- ggplot(df, aes(category, values, fill=category)) +
  geom_violin()
baseplot



In ggplot, the grid and background colors are elements defined by the theme. When you create a simple plot without specific instructions about the theme, ggplot puts one by default which sets the background color of the plot to gray, the background of the whole figure to white, and places white grid lines wherever required (according to the ticks). To realize how much the theme does for your plot, let’s just erase it by use the command theme_void():

baseplot +
  theme_void()

As you may realize, there is nothing left but the data and the legend. Even the axes and axis titles are gone.

Preset themes

ggplot offers a series of predetermined complete themes which are listed here with examples. Here is our example with one of these predefined themes, namely theme_dark():

baseplot +
  theme_dark()

And here is our same example with another theme called theme_classic():

baseplot +
  theme_classic()

Check here for a list of all preset themes in ggplot.

It is possible to tweak all the elements of the theme to make your own personalized theme. It is not the purpose of this page, but if you wish to do so, adjust the various arguments that theme() offers or uses (type ?theme in the R console for the documentation or click here for a full list of these arguments).

Background colors

To set the color of the plot background, use the argument fill= in panel.background = element_rect() in theme():

baseplot +
  theme(panel.background = element_rect(fill = "lightblue"))

To set the color of the surrounding area, use the argument fill= in plot.background = element_rect() in theme():

baseplot +
  theme(plot.background = element_rect(fill = "lightblue"))



Grid

To set the parameters of the grid (not only its color, but also the thickness, line type, etc), use the necessary arguments in panel.grid.major = element_line() or/and panel.grid.minor = element_line() in theme(). The arguments that you may use in these two functions are color=, size=, linetype= and lineend=:

baseplot +
  theme(
    panel.grid.major = element_line(colour="darkblue", size=0.5, linetype="solid"),
    panel.grid.minor = element_line(colour="lightblue", size=0.4, linetype="dashed"))

Finally, here is a way to remove specific elements of the theme (text, lines,…). Use element_blank() to simply discard whatever you do not need (for example, the grid) in the following way:

baseplot +
 theme(panel.grid.major = element_blank(),
       panel.grid.minor = element_blank())



Again, type ?theme in the R console for the documentation or click here for a full list of the components of the theme.