简体   繁体   English

ggplot2 使图例键填充透明

[英]ggplot2 make legend key fill transparent

I am trying to make the legend key fill for a ggplot transparent.我正在尝试使 ggplot 的图例键填充透明。 I followed the instructions on one of Hadley's ggplot2 guides for changing the legend key fill, but for some reason when I set the fill to transparent it fills with gray.我按照 Hadley 的 ggplot2 指南之一的说明更改图例键填充,但是由于某种原因,当我将填充设置为透明时,它会填充灰色。 Even when I set the legend key fill to white, it still appears gray in the final plot.即使我将图例键填充设置为白色,它在最终图中仍然显示为灰色。

Here is an example:下面是一个例子:

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

If I set theme(legend.key = element_rect(colour = "transparent", fill = "red")) I get the following plot:如果我设置theme(legend.key = element_rect(colour = "transparent", fill = "red"))我得到以下图: red_fill

So it appears that I can change the legend key fill, but just not to the color white or transparent.所以看起来我可以更改图例键填充,但不能更改为白色或透明。

Does anyone know what I am doing wrong, or if there is just no way to make the legend key fill transparent/white ?有谁知道我做错了什么,或者是否没有办法让图例键填充透明/白色?

EDIT: Setting theme(legend.key = element_rect(fill = alpha("white", 0.0))) Does not fix the problem.编辑:设置theme(legend.key = element_rect(fill = alpha("white", 0.0)))不能解决问题。

See here:看这里:

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

EDIT2: If I use geom_line() instead of geom_smooth I am able to set the legend key fill to NA, so it must be because the line in geom_smooth has a gray area for the confidence interval around it, therefore the legend key mirrors that look. EDIT2:如果我使用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

I was going crazy with this behavior as well, so here is a working solution for your example:我也对这种行为感到疯狂,所以这里有一个适用于您的示例的可行解决方案:

plot + guides(color=guide_legend(override.aes=list(fill=NA)))

Check this thread for more information.检查线程以获取更多信息。

You could trick it if you want.如果你愿意,你可以欺骗它。 Add a second geom_smooth() .添加第二个geom_smooth() The first with a confidence band and you don't show the legend.第一个带有信心带并且您不显示传奇。 With the second one you remove the band but show the legend.使用第二个,您删除乐队但显示图例。

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

在此处输入图片说明

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

Additionally to legend.key = element_blank() you can put legend.background=element_blank() within theme() , to make the text transparent as well除了legend.key = element_blank()您还可以将legend.background=element_blank()放在theme() ,以使文本也透明

This will also make the default white background transparent when using gg_themes这也将使使用gg_themes时默认的白色背景透明

To make use of the transparency levels, one can use:要利用透明度级别,可以使用:

For the entire legend:对于整个传说:

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

alpha("white", 0) being completely transparent like element_blank() , and alpha("white", 1) having no transparency. alpha("white", 0)element_blank()一样完全透明,而alpha("white", 1)没有透明度。

Now, for the key - if key and background have different transparencies:现在,对于关键 - 如果关键和背景具有不同的透明度:

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

Note: background transparency overrules the one for the key, ie background alpha must be smaller than key alpha.注意:背景透明度高于关键的透明度,即背景 alpha 必须小于关键 alpha。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM