Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code is right, except for two things. </p> <ul> <li><p>First, the function <code>lm_eqn</code> function does not undertand what is <code>y</code> and <code>x</code>. So, you'll have to pass the corresponding column names of <code>lai.se1</code> as follows:</p> <pre><code>lm_eqn = function(lai.se1){ m=lm(LAI ~ poly(DAS, 3), lai.se1) #3rd degree polynomial eq &lt;- 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)) } </code></pre></li> <li><p>2) Just the order has to be reversed a bit (due to <code>stat_smooth</code> as <code>annotate</code> just adds geoms to a plot. Otherwise, the <code>stat_smooth</code> output will be replaced by <code>annotate</code> ):</p> <pre><code>p &lt;- ggplot(lai.se1, aes(x = DAS, y = LAI, colour = DOS)) p &lt;- p + geom_errorbar(aes(ymin = LAI-sd, ymax = LAI+sd), colour = "black", size = .3, width = 1, position = position_dodge(.9)) p &lt;- p + stat_smooth(method = "lm", se = TRUE, fill = NA, formula = y ~ poly(x, 3, raw = TRUE)) p &lt;- 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 </code></pre></li> </ul> <p><strong>Edit:</strong> Proposed solution after OP's comment. How about this?</p> <pre><code>require(plyr) lm_eqn = daply(lai.se1, .(DOS), function(w) { m = lm(LAI ~ poly(DAS, 3), w) eq &lt;- 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 &lt;- ggplot(lai.se1, aes(x = DAS, y = LAI, colour = DOS)) p &lt;- p + geom_errorbar(aes(ymin = LAI-sd, ymax = LAI+sd), colour = "black", size = .3, width = 1, position = position_dodge(.9)) p &lt;- p + stat_smooth(method = "lm", se = TRUE, fill = NA, formula = y ~ poly(x, 3, raw = TRUE)) p &lt;- p + geom_point(size = 1, shape = 21, fill = FALSE) + theme_bw() p &lt;- p + annotate("text", x=0.5, y=5.5, label=lm_eqn[1], hjust=0, size=8, family="Times", face="italic", parse=TRUE) p &lt;- p + annotate("text", x=0.5, y=6.5, label=lm_eqn[2], hjust=0, size=8, family="Times", face="italic", parse=TRUE) p &lt;- p + annotate("text", x=0.5, y=7.5, label=lm_eqn[3], hjust=0, size=8, family="Times", face="italic", parse=TRUE) p </code></pre> <p>Basically, you generate all equations and then annotate that many times (you can probably loop them if you have more...)</p> <p><img src="https://i.stack.imgur.com/YFmZ9.jpg" alt="ggplot2_edit2"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload