在ggplot中添加手动图例 [英] Adding manual legend in ggplot
问题描述
我已经浏览了之前的类似问题,并且(我认为)已经完成了建议中的所有操作.仍然没有得到我想要的输出.
I've looked through prior similar questions and (I think) have done everything that's been recommended in them. Still not getting the output I want.
我有一堆分布,这些分布以多面图显示.然后,我通过它们绘制垂直线,代表不同的干预措施.
I have a bunch of distributions, which I'm displaying in facetted graphs. I then draw vertical lines through them, which represent different interventions.
我正在尝试显示一个图例,该图例既包含分布的填充色,又包含那些多余行的线色.据我所知,我做的一切正确(在aes()
中设置color命令,使用scale_colour_manual()
定义图例,等等).我仍然只得到填充色的图例.
I'm trying to display a legend that contains both the fill color of the distributions as well as the line color of those extra lines. As far as I can tell, I'm doing everything right (setting the color command within aes()
, using scale_colour_manual()
to define the legend, etc). I'm still only getting the legend for the fill colors.
这是我的代码:
ggplot(modCosts, aes(x=cost)) + geom_density(aes(fill=group)) + theme_bw() +
facet_wrap(~ country, scales="free") + scale_x_continuous(label = dollar) +
scale_fill_brewer(palette = "RdGy", name = "Income group", labels = c("HIC" = "High income", "UMIC" = "Upper-middle income", "LIC" = "Low income")) +
labs(y = "Density", x = "Cost", title = "Medical costs of surgery\nActual vs. modeled") +
geom_vline(data = surgCosts, aes(xintercept = CS.tert.lo, color = "red4")) +
geom_vline(data = surgCosts, aes(xintercept = CS.tert.hi, color = "red4")) +
geom_vline(data = surgCosts, aes(xintercept = CS.prim.lo, color = "red4"), lty = "dashed") +
geom_vline(data = surgCosts, aes(xintercept = CS.prim.hi, color = "red4"), lty = "dashed") +
geom_vline(data = surgCosts, aes(xintercept = Lap.tert.lo, color = "deepskyblue")) +
geom_vline(data = surgCosts, aes(xintercept = Lap.tert.hi, color = "deepskyblue")) +
geom_vline(data = surgCosts, aes(xintercept = Lap.prim.lo, color = "deepskyblue"), lty = "dashed") +
geom_vline(data = surgCosts, aes(xintercept = Lap.prim.hi, color = "deepskyblue"), lty = "dashed") +
geom_vline(data = surgCosts, aes(xintercept = Fx.tert.lo, color = "yellowgreen")) +
geom_vline(data = surgCosts, aes(xintercept = Fx.tert.hi, color = "yellowgreen")) +
scale_color_manual(name = "Reported cost", values = c("red4" = "red4", "deepskyblue" = "deepskyblue", "yellowgreen" = "yellowgreen"),
labels = c("Int1", "Int2", "Int3")) +
theme(axis.ticks = element_blank(), axis.text.y = element_blank(), legend.position = "right")
这是我得到的输出:
And here's the output I'm getting:
任何帮助将不胜感激!
推荐答案
geom_vline(...)
(以及_hline
和_abline
)有一个show_guide=...
参数,默认为FALSE
.显然,这种观点是,大多数时候您都不希望线条颜色显示在图例中.这是一个例子.
There's a show_guide=...
argument to geom_vline(...)
(and _hline
and _abline
) which defaults to FALSE
. Evidently the view was that most of the time you would not want the line colors to show up in a legend. Here's an example.
df <- mtcars
library(ggplot2)
ggp <- ggplot(df, aes(x=wt, y=mpg, fill=factor(cyl))) +
geom_point(shape=21, size=5)+
geom_vline(data=data.frame(x=3),aes(xintercept=x, color="red"), show_guide=TRUE)+
geom_vline(data=data.frame(x=4),aes(xintercept=x, color="green"), show_guide=TRUE)+
geom_vline(data=data.frame(x=5),aes(xintercept=x, color="blue"), show_guide=TRUE)
ggp +scale_color_manual("Line.Color", values=c(red="red",green="green",blue="blue"),
labels=paste0("Int",1:3))
如果您坚持使用颜色名称,BTW更好地指定比例的方法是:
BTW a better way to specify the scale if you insist on using color names is this:
ggp +scale_color_identity("Line.Color", labels=paste0("Int",1:3), guide="legend")
产生与上面相同的情节.
which produces the identical plot above.
这篇关于在ggplot中添加手动图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!