当前位置:   article > 正文

ggplot2--geom_smooth和曲线拟合画图

geom_smooth
目的:使用geom_smooth来画曲线拟合图
 
R代码:
#!/usr/bin/Rscript
 
##Reference:
#http://stackoverflow.com/questions/1476185/how-to-overlay-a-line-for-an-lm-object-on-a-ggplot2-scatterplot
 
library(ggplot2)
N = 300
x=1:N+rnorm(N, 10, 60)
y=1:N+rnorm(N, 10, 60)
colour=sample(c('red','blue'), N, replace=TRUE)
 
df = data.frame(x=x, y=y, colour=colour)
 
##
g1=ggplot(df)+geom_smooth(aes(x=x,y=y),method='lm')+geom_point(data=df, aes(x=x,y=y, colour=colour), alpha=.9)
SSS= rnorm(N, 4, 10)
 
ggplot(df)+geom_smooth(aes(x=x,y=y),method='lm')+geom_point(data=df, aes(x=x,y=y, colour=colour,size=SSS),alpha=.9)
 
## You can provide a formula for regression
g2=ggplot(df)+geom_smooth(aes(x=x,y=y),method='lm', formula=y~I(x*x))+geom_point(data=df, aes(x=x,y=y, colour=colour), alpha=.9)
 
## Provide an arbitrary formula for regressing
g3=ggplot(df)+geom_smooth(aes(x=x,y=y, colour=colour),method='lm', formula=y~I(poly(x,3)))+geom_point(data=df, aes(x=x,y=y, colour=colour), alpha=.9)
 
 
 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/181969
推荐阅读
相关标签
  

闽ICP备14008679号