如何在R中的ggplot中将图例添加到geom_smooth [英] How to add legend to geom_smooth in ggplot in R

查看:519
本文介绍了如何在R中的ggplot中将图例添加到geom_smooth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 库(样条曲线)
库(ggplot2) )
temp< - data.frame(x = rnorm(200,20,15),y = rnorm(200,30,8))

ggplot(data = temp,aes (x,y))+ geom_point()+
geom_smooth(方法='lm',公式= y〜bs(x,df = 5,截距= T),col ='blue')+
geom_smooth(method ='lm',formula = y〜ns(x,df = 2,intercept = T),col ='red')

我有两个样条:红色和蓝色。如何为他们添加图例?

解决方案

将颜色放在 aes()并添加 scale_colour_manual()

  ggplot(data = x,df = 5,截距= T),aes(color =A(x,y))+ geom_point()+ 
geom_smooth(method ='lm' ))+
geom_smooth(method ='lm',formula = y〜ns(x,df = 2,intercept = T),aes(color =B))+
scale_colour_manual =legend,values = c(blue,red))


Have a problem to add legend to different smooth in ggplot.

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

    ggplot(data = temp, aes(x, y)) + geom_point() + 
      geom_smooth(method = 'lm', formula = y ~ bs(x, df=5, intercept = T), col='blue') + 
      geom_smooth(method = 'lm', formula = y ~ ns(x, df=2, intercept = T), col='red')

I have two splines: red and blue. How I can add a legend for them?

解决方案

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

这篇关于如何在R中的ggplot中将图例添加到geom_smooth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆