我查看了之前类似的问题,并且(我认为)已经按照建议进行了所有操作,但仍然无法得到所需的输出。
我有很多分布,我正在通过facetted图形来显示它们。然后我画出垂直线,代表不同的干预措施。
我试图显示一个包含分布填充颜色以及额外线条的颜色的图例。据我所知,我做的一切都是正确的(在aes()
中设置颜色命令,使用scale_colour_manual()
定义图例等)。但我仍然只得到填充颜色的图例。
以下是我的代码:
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")
这是我得到的输出结果:
![在此输入图像描述](https://istack.dev59.com/vIKEA.webp)
dput(modCosts)
和dput(surgCosts)
,那么这将变得容易得多。 - jlhowardgeom_vline(...)
有一个show_guide=...
参数,默认为FALSE
。 - jlhoward