从 ggplot2 中的图例中删除灰色

Remove grey from legend in ggplot2

我想从图例框中删除灰色(这是因为来自 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"))