为geom_hline添加图例条目 [英] Add legend entry for a geom_hline

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

问题描述

我有一个用gggraph(ggplot+geom_bar)创建的直方图,我在其中添加了一条线,如下所示:

+ geom_hline(aes(yintercept = 0.05), linetype = 'dashed')

我要向图例中添加一个条目,该条目将指示虚线为预期值。

虽然在Stack Overflow上也有类似的问题,但我找不到我需要的答案...

知道怎么做吗?

推荐答案

制作可重复使用的示例非常方便,下次应该这样做。以下是答案:

ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar(position = "dodge") + 
# linetype has to be aes; show_guide = TRUE is important
  geom_hline(aes(yintercept = 1500, linetype = "Expected value"), 
             show_guide = TRUE) + 
# 2 means dashed
  scale_linetype_manual("Title", values = 2) +
# This fixes some problems, try linetype = 1 and another legend will be ruined
  guides(fill = guide_legend(override.aes = list(linetype = 0)))

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

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