Adding a manual legend to ggplot

I looked at previous similar questions and (I think) did everything that was recommended in them. Still not getting the output I want.

I have a bunch of distributions that I show on the edges of the graphs. Then I draw vertical lines through them that represent various interventions.

I am trying to display a legend containing both the fill color of the distributions and the line color of these extra lines. As far as I can tell, I'm doing everything right (setting the color command inside aes() , using scale_colour_manual() to define the legend, etc.). I still get only the legend of fill colors.

Here is 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\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 is the result that I get: enter image description here

Any help would be greatly appreciated!

+5
source share
1 answer

Here's the argument show_guide=... for geom_vline(...) (both _hline and _abline ), which is FALSE by default. Obviously, the opinion was that most of the time you would not want the colors of the lines displayed in the legend. Here is 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)) 

By the way, the best way to specify the scale if you insist on using color names is as follows:

 ggp +scale_color_identity("Line.Color", labels=paste0("Int",1:3), guide="legend") 

which gives an identical chart above.

+3
source

All Articles