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 v1.0.0 似乎运行良好。往上看。当我尝试向上面的图中添加小网格线时,事情发生了变化:
p1 + theme(panel.grid.minor.y = element_line())
为什么它将网格线绘制为纯黑色而不是柔和的灰色?我知道我可能可以指定 color = "lightgrey"
但我不确定 lightgrey 是否是匹配其他所有内容的正确灰色,并且更愿意使用默认值。我错过了什么吗?
最佳答案
other answer是正确的。但是,请注意,cowplot 专门提供了一个函数 background_grid()
来简化此操作。您可以通过 major
和 minor
参数简单地指定所需的主行和次行。但是,默认的次要线比主要线细,因此您还必须设置 size.minor = 0.5
以使它们看起来相同。
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)
由reprex package于2019年8月15日创建(v0.3.0)
关于r - 如何使cowplot小网格线成为标准ggplot默认软灰色,而不是纯黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57499543/