2
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?

Display name
  • 4,153
  • 5
  • 27
  • 75

2 Answers2

3

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)

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
2

This happens because the theme you are using has explicitly set the panel.grid.minor element as element_blank(). When you then set panel.grid.minor.y it inherits its parameters from the blank element rather than the theme’s panel.grid element. element_blank() doesn't have a colour, so the default black is used instead.

If you set panel.grid.minor to NULL, both x and y minor grids will then inherit their parameters from panel.grid:

p1 + theme(panel.grid.minor = NULL)

Note that if you were using a continuous x axis you'd also have to set panel.grid.minor.x = element_blank() to avoid vertical gridlines in this case.

Created on 2019-08-14 by the reprex package (v0.2.0).

Mikko Marttila
  • 10,972
  • 18
  • 31