I am new to ggplot2
. I am trying to understand how to use ggplot
. I am reading Wickham\'s book and still trying to wrap my head around how to use <
When you specify, the colour outside aes() gg_plot does not consider the color information being part of the data (and it overwrites previous information) , so there is no legend to display anymore.
If you want to specify your own colors and keep the colour information as "relevant data" and not "display information", you should add a scale_colour_manual()
command to specify the legend colours and leave the colour attribute in aes
:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(aes(colour = "loess"), method = "loess", se = FALSE) +
geom_smooth(aes(colour = "lm"), method = "lm", se = FALSE) +
labs(colour ="Method") + scale_colour_manual(values = c("loess" = "magenta", "lm" = "red"))