2017-05-12 7 views
0

リスト内に複数のモデルをプロットしようとしています。しかし、プロット中に式を標準のy〜x表記に変更できないので、エラーが発生します。これは、例を通して十分に説明されるであろう。どのように私はプロットのためのモデルを使用するのですか?stat_smooth関数をプロットするときにリスト内のモデルを使用できません

xvar=1:100 
yvar=(1:100+(1:100)^2) 
df=data.frame(xvar,yvar) 
## this works fine 
ggplot(df, aes(x=xvar, y=yvar)) + geom_point(size = 1) + geom_smooth(data = df, method = "lm", aes(x=xvar,y=yvar), formula = as.formula(y ~ x), size = 1, se = FALSE, colour = "yellow") 

models=list(
    lm(yvar~xvar, data = df), 
    lm(yvar~I(xvar^2), data = df) 
) 

ggplot(df, aes(x = xvar, y = yvar)) + geom_point(size = 1) + geom_smooth(data = df, method = "lm", aes(x=xvar,y=yvar), formula = as.formula(models[[1]]), size = 1, se = FALSE, colour = "yellow") 

Warning messages: 
1: 'newdata' had 80 rows but variables found have 100 rows 
2: Computation failed in `stat_smooth()`: 
arguments imply differing number of rows: 80, 100 

答えて

0

これで、必要なものはありますか?

library(ggplot2) 

xvar=1:100 
yvar=(1:100+(1:100)^2) 
df=data.frame(xvar,yvar) 
## this works fine 
ggplot(df, aes(x=xvar, y=yvar)) + geom_point(size = 1) + geom_smooth(data = df, method = "lm", aes(x=xvar,y=yvar), formula = as.formula(y ~ x), size = 1, se = FALSE, colour = "yellow") 

#name the models 
models=list(
    m1 = lm(yvar~xvar, data = df), 
    m2 = lm(yvar~I(xvar^2), data = df) 
) 
#use the name of the first model and then a formula 
ggplot(df, aes(x = xvar, y = yvar)) + geom_point(size = 1) + geom_smooth(data = models[[c("m1","model")]], method = "lm", aes(x=xvar,y=yvar), formula = as.formula(y ~ x), size = 1, se = FALSE, colour = "yellow") 
+0

私はこれが2番目のモデルではうまくいかないと思います。 –

関連する問題