Search code examples
rggplot2cowplot

How to get cowplot minor grid lines to be the standard ggplot default soft grey, not solid black


library(tidyverse)
library(cowplot)
p1 <- ggplot(mtcars, aes(factor(cyl))) +
  geom_bar(fill = "#56B4E9", alpha = 0.8) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
  theme_minimal_hgrid()
p1

cowplot default

Cowplot v1.0.0 seems to work fine. See above. Things break though when I try to add minor gridlines to the plot above:

p1 + theme(panel.grid.minor.y = element_line())

plot with minor gridlines

Why does it plot the gridlines as solid black instead of a soft grey? I know I could probably specify color = "lightgrey" but I'm not sure if lightgrey is the proper grey to match everything else, and would prefer to use defaults. Did I miss something?


Solution

  • The other answer is correct. However, note that cowplot provides a function background_grid() specifically to make this easier. You can simply specify which major and minor lines you want via the major and minor arguments. However, the default minor lines are thinner than the major lines, so you'd have to also set size.minor = 0.5 to get them to look the same.

    library(ggplot2)
    library(cowplot)
    #> 
    #> ********************************************************
    #> Note: As of version 1.0.0, cowplot does not change the
    #>   default ggplot2 theme anymore. To recover the previous
    #>   behavior, execute:
    #>   theme_set(theme_cowplot())
    #> ********************************************************
    ggplot(mtcars, aes(factor(cyl))) +
      geom_bar(fill = "#56B4E9", alpha = 0.8) +
      scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
      theme_minimal_hgrid() +
      background_grid(major = "y", minor = "y", size.minor = 0.5)
    

    Created on 2019-08-15 by the reprex package (v0.3.0)