ggplot2使图例键填充透明

Rei*_*ein 11 plot r ggplot2

我正在尝试使用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") )
Run Code Online (Sandbox Code Playgroud)

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") )
Run Code Online (Sandbox Code Playgroud)

编辑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") )
Run Code Online (Sandbox Code Playgroud)

geom_line

MLa*_*oie 10

如果你愿意,你可以欺骗它.添加一秒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"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 10

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

对于整个图例:

theme(legend.background=element_rect(fill = alpha("white", 0.5)))
Run Code Online (Sandbox Code Playgroud)

alpha("white", 0)完全透明,如element_blank(),并且alpha("white", 1)不透明。

现在,对于关键点 - 如果关键点和背景具有不同的透明度:

theme(legend.background=element_rect(fill = alpha("white", 0)),
      legend.key=element_rect(fill = alpha("white", .5)))
Run Code Online (Sandbox Code Playgroud)

注意:背景透明度优先于键的透明度,即背景 alpha 必须小于键 alpha。


小智 9

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


Jel*_*sen 7

此外,以legend.key = element_blank()你可以把 legend.background=element_blank()theme(),使文字透明以及

这也将使默认的白色背景在使用时透明 gg_themes


小智 6

我也为这种行为而疯狂,所以这是您示例的可行解决方案:

plot + guides(color=guide_legend(override.aes=list(fill=NA)))
Run Code Online (Sandbox Code Playgroud)

检查线程以获取更多信息。