ggplot2使图例键填充透明 [英] ggplot2 make legend key fill transparent

查看:500
本文介绍了ggplot2使图例键填充透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使ggplot的图例键填充透明.我按照哈德利(Hadley)的ggplot2指南之一中的说明更改图例键填充,但是由于某些原因,当我将填充设置为透明时,它将用灰色填充.即使将图例键填充设置为白色,最终绘图中它仍显示为灰色.

I am trying to make the legend key fill for a ggplot transparent. 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. Even when I set the legend key fill to white, it still appears gray in the final plot.

这里是一个例子:

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

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

If I set theme(legend.key = element_rect(colour = "transparent", fill = "red")) I get the following plot:

看来我可以更改图例键的填充,但不能更改为白色或透明.

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 ?

设置theme(legend.key = element_rect(fill = alpha("white", 0.0)))不能解决问题.

Setting theme(legend.key = element_rect(fill = alpha("white", 0.0))) Does not fix the problem.

查看此处:

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

如果我使用geom_line()而不是geom_smooth,则能够将图例键填充设置为NA,因此必须是因为geom_smooth中的行的置信区间为灰色区域因此,图例键会镜像它.

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.

(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_smooth().第一个带有置信度的频段,您不会显示图例.在第二个中,您删除了乐队,但显示了图例.

You could trick it if you want. Add a second 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"))

这篇关于ggplot2使图例键填充透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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