ggplot2 make legend key fill transparent

前端 未结 5 2053
不知归路
不知归路 2021-02-07 03:27

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

5条回答
  •  鱼传尺愫
    2021-02-07 03:50

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

提交回复
热议问题