3
votes

I've looked around on SO but I can't seem to find a solution. I've used geom_point and geom_hline in ggplot2 and have gotten satisfactory legends for both. However, I have one black line and one blue line in the figure but in the legend they are both black - how can I correct this in the legend to be the right colors?

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")

I'm sure I've over written something here... just not sure where (am new to ggplot)

Data:

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

Plan goes up to 40, but I've only included a snippet of data here...

UPDATE: To those that might be helping out - my code to plot the data was wrong. I've updated this.

1
You are more likely to get help if you make this a reproducible example, by editing the question to include your data (e.g., sam, and the values for mad and mmad).jlhoward
Updated my question to include data.user2726449

1 Answers

5
votes

As you are already using colors for points then you can't use scale_color_.. for line to show colors in legend. Workaround is to use function guides() with argument override.aes= to add your colors for legend. Add this line to your existing code.

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

enter image description here