为图例添加一个手动添加的行条目

N.M*_*N.M 3 r ggplot2

我有一个用ggplot(ggplot + geom_bar)创建的直方图,我添加了一行,如下所示:

+ geom_hline(aes(yintercept = 0.05),linetype='dashed')
Run Code Online (Sandbox Code Playgroud)

我想在图例中添加一个条目,表示虚线是预期值.

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

知道怎么做吗?

Jul*_*ora 5

在ggplot问题的情况下制作可重复的例子非常方便,下次你应该这样做.这是答案:

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)))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述