从 ggplot2 中的图例中删除灰色

标签 r ggplot2 legend

我想从图例框中删除灰色(这是因为来自 geom_smooth 的 SE)。不过,我想将 SE 保留在实际情节中。所以在图例框中,我只想要线条的颜色,而不是阴影。这是一个例子:

library(ggplot2)

x <- rnorm(100)
y <- rnorm(100)
g_ <- sample(c("group1", "group2"), 100, replace = TRUE)

ggplot(data.frame(x, y, g_), aes(x = x, y = y, color = g_)) + geom_smooth()

最佳答案

这是一个方法。首先,绘制带有置信区间但没有图例的线条。然后,绘制无间隔的线条和图例,最后将图例键涂成白色。

ggplot(data.frame(x, y, g_), aes(x = x, y = y, color = g_)) + 
  geom_smooth(show_guide=FALSE) +
  geom_smooth(fill=NA) +
  theme(legend.key = element_rect(fill = "white"))

enter image description here

关于从 ggplot2 中的图例中删除灰色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30962946/

相关文章:

python - 如何用 distplot 绘制两个图例

r - ggplot geom_point(),其颜色基于特定的离散值

r - ggplot : adding normal distribution curve using stat_function to existing histogram and distribution layers

r - 如何更改和/或分离 r 图形图例中的组件?

python - 当轴限制发生变化时,很好地标记没有图例的线条?

javascript - 使用图表师在条形图中的图例对齐

r - 根据条件拆分为 list(),省略 False 元素

r - 如何使点大小独立于点阵绘图中的分组

r - CRAN 包检查和 R CMD 检查 -as--cran 之间的差异

R:ggfortify:指定用于图中标签的列