如何为geom_hline添加自定义图例 [英] How to add a custom legend for geom_hline

查看:992
本文介绍了如何为geom_hline添加自定义图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下数据,请注意图例未正确描述geom_hline:

df<-data.frame(
    points=c(.153,.144,.126,.035, .037, .039, .010,.015,.07),
    days=gl(3,1,9,labels=c("Sun","Mon","Tues")),
    lang=c("en","en","en","pt","pt","pt","ko","ko","ko")) 

ggplot(data=df[df$lang=="en",])+
    geom_point(aes(x=days,y=points),size=5,colour='cyan',show_guide=F)+
    geom_point(aes(x=days,y=points,colour=days),size=4,show_guide=F)+
    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(yintercept=.136,color='cyan',size=2, show_guide=T)

是否可以通过某种方式来创建描述geom_hline的自定义图例? [我希望图例的名称为"Legend",并且只有一个值被标记为"avg"] >

通过搜索,我找到了一个示例:显示透明度和填充颜色的ggplot图例.他们使用了我尝试过的scale_fill_manual,但无法改善上面显示的图例.

解决方案

有点尴尬,但这似乎可行:

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

啊,但是您在其中包括了legend.title = element_blank(),这就是为什么未命名图例的原因.删除该名称以包含名称.

Using the data below, notice the legend does not properly describe geom_hline:

df<-data.frame(
    points=c(.153,.144,.126,.035, .037, .039, .010,.015,.07),
    days=gl(3,1,9,labels=c("Sun","Mon","Tues")),
    lang=c("en","en","en","pt","pt","pt","ko","ko","ko")) 

ggplot(data=df[df$lang=="en",])+
    geom_point(aes(x=days,y=points),size=5,colour='cyan',show_guide=F)+
    geom_point(aes(x=days,y=points,colour=days),size=4,show_guide=F)+
    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(yintercept=.136,color='cyan',size=2, show_guide=T)

Is there some way to create a custom legend that describes the geom_hline? [I'd like the legend to to have the name "Legend" with only one value labled "avg"]

Searching so, I found an example: ggplot legend showing transparency and fill color. They used scale_fill_manual, which I tried, but could not improve the legend displayed above.

解决方案

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.

这篇关于如何为geom_hline添加自定义图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆