2

Is there a way to remove the grey background from a ggplot graph, without removing the gridlines and without using theme_bw()?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Sorcha
  • 21
  • 2
  • [Change the colors of the plot panel background and the grid lines](http://www.sthda.com/english/wiki/ggplot2-themes-and-background-colors-the-3-elements) – AndrewGB Apr 03 '22 at 22:30
  • 1
    is there a particular reason you *don't* want to use `theme_bw()` ? – Ben Bolker Apr 04 '22 at 00:20

1 Answers1

6

As the default ggplot theme (theme_grey) has a "white" color for the grid lines you have to set a different color for the grid lines when removing the grey background, i.e. when setting the fill for the background to "white" or NA. In the code below I simply use black grid lines:

library(ggplot2)

ggplot(mtcars, aes(hp, mpg)) +
  geom_point() +
  theme(panel.background = element_rect(fill = "white"),
        panel.grid = element_line(color = "black"))

stefan
  • 90,330
  • 6
  • 25
  • 51