How to add a custom legend for geom_hline

时光总嘲笑我的痴心妄想 提交于 2019-12-06 00:10:12

It's a bit awkward, but this seems to work:

hline <- data.frame(yint = 0.136,lt = 'Avg') 

ggplot(data=df[df$lang=="en",])+
    geom_point(aes(x=days,y=points),size=5,colour='cyan')+
    geom_point(aes(x=days,y=points,colour=days),size=4)+
    facet_wrap(~lang,ncol=1,scales="free")+
    xlab("")+
    ylab("")+
    scale_y_continuous(labels = percent_format())+
    theme(legend.position="right",
          legend.title = element_blank(),
          strip.text.x = element_text(size = 13, colour = 'black', angle = 0),
          axis.text.x=element_text(angle=0, hjust=.5, vjust=0),
          legend.position = 'none',
          panel.background = element_rect(fill = "#545454"),
          panel.grid.major = element_line(colour = "#757575"),
          panel.grid.minor = element_line(colour = "#757575"))+
    geom_hline(data = hline,aes(yintercept=yint,linetype = lt),color = "cyan",size=2,show_guide = TRUE) + 
    scale_colour_discrete(guide = "none") + 
    scale_linetype_manual(name = 'Legend',values = 1,guide = "legend")

Ah, but you've included legend.title = element_blank() in there, which is why the legend is not named. Remove that to include the name.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!