Ggplot2 make vowel key fill transparent

I am trying to make the fill of the legend transparent ggplot. I followed the instructions in one of the Hadley ggplot2 tutorials to change the legend key fill, but for some reason, when I set the fill to transparent, it fills in gray. Even when I set the legend fill to white, it still looks gray in the last 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 graph: red_fill

So it seems that I can change the fill of the legend, but not only the color is white or transparent.

Does anyone know what I'm doing wrong, or if there is no way to make the legend key filled with transparent / white?

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

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 can set the legend filling to NA, so it should be because the line in geom_smooth has a gray value for the confidence interval around it, so the legend mirror looks like this.

 (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

+7
r plot ggplot2
source share
2 answers

You can trick him if you want. Add a second geom_smooth() . The first is with a trust group, and you are not showing the legend. With the second, you delete the group, 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")) 

enter image description here

+8
source share

I also went crazy with this behavior, so here is an example of a working solution for your example:

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

View this thread for more information.

+2
source share

All Articles