Fitting a quadratic curve in ggplot

大憨熊 提交于 2020-07-05 06:04:11

问题


This is my sample data. I want to plot both y1 and y2 against x1 in a single plot. This is what I did:

library(ISLR)
library(ggplot2)

y1<-scale(Auto$horsepower,scale = T,center=T)
y2<-scale(Auto$weight,scale = T,center=T)
x1<-Auto$mpg
df<-data.frame(y1,y2,x1)

p<-ggplot(df,aes(x=x1)) + 
   geom_point(aes(y = y1), shape = 16) +
   geom_point(aes(y = y2), shape = 2) 

I want to insert a quadratic line for both y1 and y2 against x. I did this:

p + stat_smooth(method = "lm", formula = y ~ x + I(x^2), size = 1)

It throws up an error:

Warning message:
Computation failed in `stat_smooth()`:
variable lengths differ (found for 'x')  

Other than this, the stat_smooth command will only put one quadratic line while I need two quadratic lines for both y1 and y2.

How did I achieve this in R?

Thanks


回答1:


You should add two stat_smooth() calls and add aes() to show which y to use.

ggplot(df,aes(x=x1)) + 
      geom_point(aes(y = y1), shape = 16) +
      geom_point(aes(y = y2), shape = 2) +
      stat_smooth(aes(y = y1),method = "lm", formula = y ~ x + I(x^2), size = 1) +
      stat_smooth(aes(y = y2),method = "lm", formula = y ~ x + I(x^2), size = 1, color = "red")

Or make long format table and then you will need just one call of stat_smooth() and geom_point().

library(tidyr)
df_long <- df %>% gather(variable, value, y1:y2)

ggplot(df_long, aes(x1, value, color = variable)) +
      geom_point() +
      stat_smooth(method = "lm", formula = y ~ x + I(x^2), size = 1)



来源:https://stackoverflow.com/questions/42764028/fitting-a-quadratic-curve-in-ggplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!