1

我用 ggplot2 绘制了以下数据,然后为每个组添加了 3 阶多项式线,咨询了帖子:将 3 阶多项式及其方程添加到 r 数据中的 ggplot:

>lai.se1    
    DOS DAS N   LAI sd  se  ci
    D1  31  24  1.5879167   0.42763230  0.08729008  0.18057328
    D1  84  24  4.3241667   0.32478644  0.06629675  0.13714529
    D1  113 24  3.7037500   0.34151596  0.06971165  0.14420954
    D1  132 24  2.9704167   0.33386380  0.06814966  0.14097832
    D1  160 24  0.1879167   0.09868611  0.02014422  0.04167149
    D2  35  24  1.7679167   0.18551876  0.03786886  0.07833770
    D2  82  24  3.7670833   0.38212767  0.07800148  0.16135836
    D2  108 24  3.4104167   0.31431747  0.06415978  0.13272463
    D2  126 24  2.7879167   0.35024189  0.07149283  0.14789418
    D2  146 24  0.1950000   0.08836682  0.01803780  0.03731404
    D3  37  24  1.3179167   0.16378616  0.03343271  0.06916083
    D3  83  24  3.5233333   0.29256982  0.05972057  0.12354140
    D3  94  24  3.1604167   0.28257326  0.05768002  0.11932022
    D3  113 24  2.4587500   0.44131535  0.09008312  0.18635113
    D3  134 24  0.2758333   0.09536733  0.01946677  0.04027009

p<-ggplot(lai.se1, aes(x=DAS, y=LAI, colour=DOS)) + 
  geom_errorbar(aes(ymin=LAI-sd, ymax=LAI+sd), colour ="black", size =.3, width=1,
                position=position_dodge(.9)) +
  geom_point(size=1, shape=21, fill=FALSE)+ theme_bw()
p + stat_smooth(method="lm", se=TRUE, fill=NA,             ## to add polynomial lines
                formula=y ~ poly(x, 3, raw=TRUE))

阴谋: 在此处输入图像描述

## Add equation in the plot
lm_eqn = function(lai.se1){
  m=lm(y ~ poly(x, 3), lai.se1)#3rd degree polynomial
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
                   list(a = format(coef(m)[1], digits = 2),
                        b = format(coef(m)[2], digits = 2),
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq))
}


p + annotate("text", x=0.5, y=15000, label=lm_eqn(lai.se1), hjust=0, size=8, 
             family="Times", face="italic", parse=TRUE)


Error:
Error in model.frame.default(formula = y ~ poly(x, 3), data = lai.se1,  : 
  variable lengths differ (found for 'poly(x, 3)')

但是当我尝试使用上述链接帖子中提到的类似函数来放置方程时,它给出了一个可变长度的错误。我不太擅长在 r 中编写新函数,需要您的帮助来解决它。请帮忙。

4

1 回答 1

1

您的代码是正确的,除了两件事。

  • 首先,函数lm_eqn函数不理解什么是yx。因此,您必须传递相应的列名,lai.se1如下所示:

    lm_eqn = function(lai.se1){
        m=lm(LAI ~ poly(DAS, 3), lai.se1) #3rd degree polynomial
        eq <- substitute(italic(LAI) == a + b %.% italic(DAS)*","~~italic(r)^2~"="~r2, 
                 list(a = format(coef(m)[1], digits = 2), 
                 b = format(coef(m)[2], digits = 2), 
                 r2 = format(summary(m)$r.squared, digits = 3)))
                 as.character(as.expression(eq))
    }
    
  • 2)只是顺序必须颠倒一下(因为stat_smooth只是annotate将几何图形添加到绘图中。否则,stat_smooth输出将被替换annotate):

    p <- ggplot(lai.se1, aes(x = DAS, y = LAI, colour = DOS))
    p <- p + geom_errorbar(aes(ymin = LAI-sd, ymax = LAI+sd), 
             colour = "black", size = .3, width = 1, 
             position = position_dodge(.9)) 
    p <- p + stat_smooth(method = "lm", se = TRUE, fill = NA, 
             formula = y ~ poly(x, 3, raw = TRUE))   
    p <- p + geom_point(size = 1, shape = 21, fill = FALSE) + theme_bw()
    p + annotate("text", x=0.5, y=7.5, label=lm_eqn(lai.se1), hjust=0, size=8, 
                 family="Times", face="italic", parse=TRUE)
    p
    

编辑: OP评论后提出的解决方案。这个怎么样?

require(plyr)
lm_eqn = daply(lai.se1, .(DOS), function(w) {
    m = lm(LAI ~ poly(DAS, 3), w)
    eq <- substitute(italic(LAI) == a + b %.% italic(DAS)*","~~italic(r)^2~"="~r2, 
             list(a = format(coef(m)[1], digits = 2), 
             b = format(coef(m)[2], digits = 2), 
             r2 = format(summary(m)$r.squared, digits = 3)))
             as.character(as.expression(eq))
    })


p <- ggplot(lai.se1, aes(x = DAS, y = LAI, colour = DOS))
p <- p + geom_errorbar(aes(ymin = LAI-sd, ymax = LAI+sd), 
         colour = "black", size = .3, width = 1, 
         position = position_dodge(.9)) 
p <- p + stat_smooth(method = "lm", se = TRUE, fill = NA, 
         formula = y ~ poly(x, 3, raw = TRUE))   
p <- p + geom_point(size = 1, shape = 21, fill = FALSE) + theme_bw()
p <- p + annotate("text", x=0.5, y=5.5, label=lm_eqn[1], hjust=0, size=8, 
             family="Times", face="italic", parse=TRUE)
p <- p + annotate("text", x=0.5, y=6.5, label=lm_eqn[2], hjust=0, size=8, 
             family="Times", face="italic", parse=TRUE)
p <- p + annotate("text", x=0.5, y=7.5, label=lm_eqn[3], hjust=0, size=8, 
             family="Times", face="italic", parse=TRUE)
p

基本上,你生成所有方程,然后多次注释(如果你有更多,你可以循环它们......)

ggplot2_edit2

于 2013-01-29T15:04:32.987 回答