regrading adding a legend using ggplot2 for different lines

后端 未结 1 615
日久生厌
日久生厌 2021-01-29 05:59

I want to add a legend that will tell which color represents which line using ggplot2. My code is as follows :

require(lme4)
require(ggplot2)
m1 <         


        
相关标签:
1条回答
  • 2021-01-29 06:22

    Put the color parameter in aes and give it the name you want to show in the the legend and then choose the title and colors in scale_color_manual

    Example:

    library(ggplot2)
    
    ggplot(mpg, aes(displ, hwy)) +
      geom_point() +
      geom_smooth(aes(color='Smooth 1')) +
      geom_smooth(aes(y = (hwy -1), color='Smooth 2')) +
      scale_color_manual('Legend Title', values=c('Smooth 1'='blue', 'Smooth 2'='red'))
    
    0 讨论(0)
提交回复
热议问题