在ggplot中添加3个三阶多项式方程

时间:2013-01-29 13:19:58

标签: r ggplot2 equation

我已经使用ggplot2绘制了以下数据,然后为每个组添加了3阶多项式线来查阅帖子:Adding a 3rd order polynomial and its equation to a ggplot in r 数据:

>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))

简介: enter image description here

## 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中编写新函数,需要你的帮助来解决它。请帮忙。

1 个答案:

答案 0 :(得分: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_smoothannotate,只需将geoms添加到绘图中。否则,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