在ggplot2中删除图例中的灰色 [英] Remove grey from legend in ggplot2

查看:330
本文介绍了在ggplot2中删除图例中的灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从图例框中移除灰色(这是因为来自geom_smooth的SE)。尽管如此,我仍想让SE保持在实际情节中。所以在图例框中,我只想要线条的颜色,而不是阴影。这是一个例子:

I would like to remove the grey (which is there because of the SE from geom_smooth) from the legend boxes. I would like to keep the SE in the actual plot though. So in the legend boxes, I just want the color of the lines, not the shadings. Here is an example:

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()


推荐答案

这是一种方法。首先,画出可信区间的线条,但没有传说。然后,画出没有间隔和图例的线条,最后将图例的颜色设为白色。

Here's a way. First, draw lines with confidence intervals, but no legend. Then, draw lines with no intervals and a legend, and finally, color the legend key white.

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"))

这篇关于在ggplot2中删除图例中的灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆