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.
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.
Here's my code:
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
Actual 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:
Any help would be greatly appreciated!
See Question&Answers more detail:
os