【发布时间】:2018-04-11 13:25:22
【问题描述】:
我正在处理多面图,并在geom_smooth() 中使用lm 方法添加线条
d<-data.frame(n=c(100, 80, 60, 55, 50, 102, 78, 61, 42, 18),
year=rep(2000:2004, 2),
cat=rep(c("a", "b"), each=5))
ggplot(d, aes(year, n, group=cat))+geom_line()+geom_point()+
facet_wrap(~cat, ncol=1)+
geom_smooth(method="lm")
我想设置一个函数以在适当的情况下应用多项式。我已经制定了一个功能:
lm.mod<-function(df){
m1<-lm(n~year, data=df)
m2<-lm(n~year+I(year^2), data=df)
ifelse(AIC(m1)<AIC(m2), "y~x", "y~poly(x, 2)")
}
但我无法应用它。有什么想法或更好的方法来解决这个问题吗?
【问题讨论】: