r - 如何使cowplot小网格线成为标准ggplot默认软灰色,而不是纯黑色

标签 r ggplot2 cowplot

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 似乎运行良好。往上看。当我尝试向上面的图中添加小网格线时,事情发生了变化:

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

plot with minor gridlines

为什么它将网格线绘制为纯黑色而不是柔和的灰色?我知道我可能可以指定 color = "lightgrey" 但我不确定 lightgrey 是否是匹配其他所有内容的正确灰色,并且更愿意使用默认值。我错过了什么吗?

最佳答案

other answer是正确的。但是,请注意,cowplot 专门提供了一个函数 background_grid() 来简化此操作。您可以通过 majorminor 参数简单地指定所需的主行和次行。但是,默认的次要线比主要线细,因此您还必须设置 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/

相关文章:

r - (错误)理解 `first`中的 `xts`

r - 如何在R-studio的功能主体中设置断点?

r - 如何检查R中函数的所有参数

r - 当ggplot2中的stat ='count'时,如何手动更改条形图的填充颜色

r - cowplot:如何通过修改轴限制来自定义主面板和边缘图之间的间隙?

使用 R 从 Microsoft Azure 读取 csv 文件

r - 如何格式化 R plotly 工具提示中的变量名称?

r - 使用ggplot2隐藏比例时如何保持绘图大小?

r - 不能与 cowplot 包左对齐

r - 如何对齐直方图和箱线图以便它们共享 x 轴?