ggplot2 使图例键填充透明

33

我试图使ggplot的图例填充透明。我遵循了Hadley的ggplot2指南之一中更改图例键填充的说明,但是出现了问题,当我将填充设置为透明时,它会填充为灰色。即使我将图例填充设置为白色,最终的绘图中仍然显示为灰色。

以下是一个例子:

library(ggplot2)

data1 = c(0,10, 11, 23, 33, 40, 41, 50, 59, 68, 76, 88, 90, 99)
data2 = c(2, 8, 10, 22, 39, 47, 49, 55, 62, 70, 76, 86, 88, 95)

df = data.frame(data1, data2)

(plot = ggplot() +
  geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
  geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
  scale_x_continuous(expand=c(0,0), limits=c(0,100)) + 
  scale_y_continuous(expand=c(0,0), limits=c(0,100))+
  theme_classic()+
  labs(y="data2", x="data1", 
       title="sample 1 data1 vs data2") +
  theme(plot.title = element_text(size=18, face="bold"),
        legend.key = element_rect(colour = "transparent", fill = "white"),
        legend.justification = c(1,0), legend.position = c(1,0))+
  scale_color_discrete(name="Sample") )

Example_plot

如果我设置theme(legend.key = element_rect(colour = "transparent", fill = "red")),就会得到以下绘图: red_fill

所以看起来我可以改变图例键填充颜色,但不能设置为白色或透明。
有人知道我做错了什么吗?还是说没有办法让图例键填充透明/白色? 编辑:设置theme(legend.key = element_rect(fill = alpha("white", 0.0)))无法解决问题。

请参见此处:

library(ggplot2)
library(scales)

data1 = c(0,10, 11, 23, 33, 40, 41, 50, 59, 68, 76, 88, 90, 99)
data2 = c(2, 8, 10, 22, 39, 47, 49, 55, 62, 70, 76, 86, 88, 95)

df = data.frame(data1, data2)

(plot = ggplot() +
  geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
  theme_classic()+
  labs(y="data2", x="data1", 
       title="sample 1 data1 vs data2") +
  theme(plot.title = element_text(size=18, face="bold"),
        legend.key = element_rect(colour = "transparent", fill = alpha("red", 0)),
        legend.justification = c(1,0), legend.position = c(1,0))+
  scale_color_discrete(name="Sample") )

编辑2:如果我使用geom_line()而不是geom_smooth,我可以将图例键填充设置为NA,因此这必须是因为geom_smooth中的线具有置信区间的灰色区域,因此图例键反映了那种外观。

(plot = ggplot() +
  geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
  geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
  scale_x_continuous(expand=c(0,0), limits=c(0,100)) + 
  scale_y_continuous(expand=c(0,0), limits=c(0,100))+
  theme_classic()+
  labs(y="data2", x="data1", 
       title="sample 1 data1 vs data2") +
  theme(plot.title = element_text(size=18, face="bold"),
        legend.key = element_rect(colour = NA, fill = NA),
        legend.justification = c(1,0), legend.position = c(1,0))+
  scale_color_discrete(name="Sample") )

geom_line


1
可能是 Controlling the 'alpha' level in a ggplot2 legend 的重复问题。 - CinchBlue
2
legend.key = element_rect(fill = NA)设置后,效果与之前一样。键填充仍然是灰色的。 - Reilstein
在我的编辑中,我解释了为什么你提供的链接中的解决方案在这种情况下无法奏效。 - Reilstein
如果您在geom_smooth()表达式中添加se = FALSE,则您的代码将正常工作。 因此,图例中看到的是置信区间带。 - MLavoie
我刚刚意识到了这一点。我发现如果我使用geom_line()而不是geom_smooth(),那么我就可以将填充设置为NA。我想我可以忍受没有置信区间,但很遗憾我不能两全其美!感谢您的见解。编辑:啊,但是在我的实际数据集上使用geom_line()将无法工作,因为它没有平滑处理,所以我将使用您的解决方案将se=FALSE。 - Reilstein
@Reilstein 我猜填充背景与置信带的背景有关。如果有多条线,那可能有意义。这只是我的猜测。 - CinchBlue
5个回答

25
我也为这种行为疯狂了,所以这里是您的示例的解决方案:
plot + guides(color=guide_legend(override.aes=list(fill=NA)))

查看主题以获取更多信息。


1
你能否提供更多关于为什么这是一个可行的解决方案的细节? - Nico Haase
1
如果我理解正确,当设置se=TRUE时,geom_smooth使用fill来绘制带。正如其名称所示,override.aes覆盖了生成的图例的这种美学特征,因此,虽然所有其他设置保持默认值,但是geom_smooth的fill不再为图例绘制。 - bontus

18

如果你想的话,你可以欺骗它。添加第二个geom_smooth()。第一个带置信区间,但不显示图例。第二个则去除区间,但显示图例。

df$Color <- "Red"
df1 <- df
(plot = ggplot() +
  geom_smooth(data=df, aes(data1, data2,colour=Color), se = TRUE, show.legend = FALSE) + 
  geom_smooth(data=df1, aes(data1, data2,colour=Color), se=FALSE) +
  geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
  scale_x_continuous(expand=c(0,0), limits=c(0,100)) + 
  scale_y_continuous(expand=c(0,0), limits=c(0,100))+
  theme_classic()+
  labs(y="data2", x="data1", 
       title="sample 1 data1 vs data2") +
  theme(plot.title = element_text(size=18, face="bold"),
        legend.key = element_rect(colour = "transparent", fill = "white"),
        legend.justification = c(1,0), legend.position = c(1,0))+
  scale_color_discrete(name="Sample"))

这里输入图片描述


1
这个解决方案对我不起作用。你试过吗? - Reilstein
1
啊,我通过删除具有se = TRUE的geom_smooth()的映射使其正常工作。因此,如果我使用geom_smooth(data = df,aes(data1,data2),se = TRUE)+ geom_smooth(data = df1,aes(data1,data2,colour =“Sample1”),se = FALSE),那么它可以正常工作。 - Reilstein
我刚刚添加了一行代码,之前漏掉了,对此很抱歉。在得到这个答案之前,我测试了几件事情。 - MLavoie
1
这个解决方案可行。在我的情况下,我还能够做到这一点:legend.key = element_rect(colour = "transparent", fill = "transparent") - Joben R. Ilagan

15

除了legend.key = element_blank()之外,您还可以将legend.background=element_blank()放在theme()中,使文本也透明。

这也会在使用gg_themes时将默认白色背景变为透明。


11

这个答案似乎是最简单的解决方案,在theme()定义中设置legend.key = element_blank()


对我没有用。你有用geom_smooth测试过吗? - see24

9

要利用透明度级别,可以使用以下方法:

对于整个图例:

theme(legend.background=element_rect(fill = alpha("white", 0.5)))

alpha("white", 0) 完全透明,类似于 element_blank(),而alpha("white",1) 则没有透明度。

现在,就关键部分而言——如果图例(key)和背景有不同的透明度:

theme(legend.background=element_rect(fill = alpha("white", 0)),
      legend.key=element_rect(fill = alpha("white", .5)))

注意:背景透明度会覆盖关键色的透明度,即背景透明度必须小于关键色透明度。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接