Cannot remove grey area behind legend symbol when using smooth

前端 未结 1 879
梦毁少年i
梦毁少年i 2021-01-21 01:12

I\'m using ggplot2 with a GAM smooth to look at the relationship between two variables. When plotting I\'d like to remove the grey area behind the symbol for the two types of va

相关标签:
1条回答
  • 2021-01-21 01:38

    Here is a very hacky workaround, based on the notion that if you map things to aestethics in ggplot, they appear in the legend. geom_smooth has a fill aesthetic which allows for different colourings of different groups if one so desires. If it's hard to fix that downstream, sometimes it's easier to keep those unwanted items out of the legend altogether. In your case, the color of the se appeared in the legend. As such, I've created two geom_smooths. One without a line color (but grouped by type) to create the plotted se's, and one with linetype mapped to aes but se set to false.

    p <- ggplot(df, aes(x=x, y=y)) + 
      #first smooth; se only
      stat_smooth(aes(group=type),col=NA, method = "auto", size=1, se=TRUE)+
      #second smooth: line only
      stat_smooth(aes(lty=type),col="black", method = "auto", size=1, se=F) +
      theme_classic() +
      theme(
        legend.title = element_blank(),
        legend.key = element_rect(fill = NA, color = NA)) #thank you @alko989
    

    0 讨论(0)
提交回复
热议问题