Using colors in aes() function in ggplot2

前端 未结 1 978
一整个雨季
一整个雨季 2021-01-19 09:06

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 <

相关标签:
1条回答
  • 2021-01-19 09:35

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

    0 讨论(0)
提交回复
热议问题