关于r:图例[ggplot2]中的geom_hline颜色不正确

Colour of geom_hline is not correct in legend [ggplot2]

我一直在寻找SO,但是似乎找不到解决方案。我在ggplot2中使用了geom_point和geom_hline,并获得了令人满意的图例。但是,图中我有一条黑线和一条蓝线,但是在图例中它们都是黑色的-如何在图例中将其校正为正确的颜色?

1
2
3
4
5
6
7
mcgc <- ggplot(sam, aes(x = Person,y = mm, colour = X)) +
                  geom_point(size = 0.75) +
                  scale_colour_gradient2(high="red", mid="green", limits=c(0,1), guide ="colourbar") +
                  geom_hline(aes(yintercept = mad, linetype ="mad"), colour ="blue", size=0.75, show_guide = TRUE) +
                  geom_hline(aes(yintercept = mmad, linetype ="mmad"), colour ="black", size=0.75, show_guide = TRUE)  +
                  facet_wrap(~ Plan, scales ="free", ncol = 4) +
                  scale_linetype_manual(name ="Plan of Health Care", values = c("mad" = 1,"mmad" = 1),guide ="legend")

我确定我已经在这里写完了...只是不确定在哪里(ggplot的新功能)

数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
Plan  Person X       mm  mad mmad
1  1 95 0.323000 0.400303 0.12
1  2 275 0.341818 0.400303 0.12
1  3  2 0.618000 0.400303 0.12
1  4 75 0.320000 0.400303 0.12
1  5 13 0.399000 0.400303 0.12
1  6 20 0.400000 0.400303 0.12
2  1 219 0.393000 0.353350 0.45
2  2 50 0.060000 0.353350 0.45
2  3 213 0.390000 0.353350 0.45
2  4 204 0.496100 0.353350 0.45
2  5 19 0.393000 0.353350 0.45
2  6 201 0.388000 0.353350 0.45

计划最多可以使用40个,但这里我只包含了一段数据...

更新:对于那些可能会有所帮助的人-我的代码来绘制数据是错误的。我已经更新了。


因为您已经在使用颜色作为点,所以不能使用scale_color_..作为线以在图例中显示颜色。解决方法是将函数guides()与参数override.aes=一起使用,以为图例添加颜色。将此行添加到您现有的代码中。

1
  + guides(linetype=guide_legend(override.aes=list(colour = c("blue","black"))))

enter