How to add legend to geom_smooth in ggplot in R

后端 未结 1 1127
独厮守ぢ
独厮守ぢ 2021-01-11 23:44

Have a problem of adding legend to different smooth in ggplot.

    library(splines)
    library(ggplot2)
    temp <- data.frame(x = rnorm(200, 20, 15), y          


        
相关标签:
1条回答
  • 2021-01-12 00:22

    Put the colour in aes() and add scale_colour_manual():

    ggplot(data = temp, aes(x, y)) + geom_point() + 
      geom_smooth(method = 'lm', formula = y ~ bs(x, df=5, intercept = T), aes(colour="A")) + 
      geom_smooth(method = 'lm', formula = y ~ ns(x, df=2, intercept = T), aes(colour="B")) +
      scale_colour_manual(name="legend", values=c("blue", "red"))
    

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