Remove lines from color and fill legends

前端 未结 3 990
不知归路
不知归路 2020-12-03 07:43

I have a plot with three different legends: one for linetype, one for color, and one for fill. In the color and fil

相关标签:
3条回答
  • 2020-12-03 08:11

    You may set linetype = 0 or "blank" (on different linetypes here) for the filland color guides in your override.aes call.

    Also note that I moved the fill aes from the 'top level' in ggplot to geom_bar.

    ggplot(df, aes(x, y)) +
      geom_bar(aes(fill = con), stat = 'identity') + 
      geom_point(aes(color = col)) +
      geom_hline(data = hline_df, aes(yintercept = y, linetype = name), color = 'red', show_guide = TRUE) +
      guides(fill = guide_legend(override.aes = list(linetype = 0)),
             color = guide_legend(override.aes = list(linetype = 0)))
    

    enter image description here

    0 讨论(0)
  • 2020-12-03 08:19

    Using:

    ggplot(df, aes(x,y,fill=con)) + geom_bar(stat='identity') + 
      geom_point(aes(color=col)) +
      geom_hline(data=hline_df,aes(yintercept=y,linetype=name),color='red',show_guide=FALSE) +
      guides(linetype=FALSE,color=FALSE)
    

    gives me this plot: enter image description here

    0 讨论(0)
  • 2020-12-03 08:25

    As suggested by user20650

    ggplot(df, aes(x,y)) + 
      geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=TRUE) + 
      geom_point(aes(color=col), size=5) + 
      geom_bar(aes(fill=con), stat='identity') + 
      geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=F) + 
      guides(color = guide_legend(override.aes = list(linetype = 0)))
    

    So the first geom_hline creates the legend but the line is behind the bars...
    the second call brings the line in front of the bars but does not print a legend (great idea).
    The las guide is overwriting the aesthetics line type with 0... In this way it removes the line from the legends... I tried with NULL but this didn't worked before...

    Thanks again.

    enter image description here

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