0

I want to add R2 values to a graph I generated using ggplot2. Below is my code:

ggplot(mad[["Timeline"]], aes(x=Year, y=gen)) + geom_point(color="purple") + geom_smooth(aes(x=Year,y=gen),color="purple",method="glm", formula = y~x, method.args=list(family=gaussian(link="log"))) +
  geom_point(data=mad[["Timeline"]], mapping = aes(x=Year, y=pro), color="orange") + geom_smooth(aes(x=Year,y=pro),color="orange",method="glm",formula = y~x, method.args=list(family=gaussian(link="log"))) +
  geom_point(data=mad[["Timeline"]], mapping = aes(x=Year, y=met), color="blue") + geom_smooth(aes(x=Year,y=met),color="blue",method="glm", formula = y~x, method.args=list(family=gaussian(link="log")))

Just to try on one curve, I used

lm_eqn <- function(df){
  m <- lm(y ~ x), df);
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
                   list(a = format(unname(coef(m)[1]), digits = 2),
                        b = format(unname(coef(m)[2]), digits = 2),
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq));
}

with

ggplot(mad[["Timeline"]], aes(x=Year, y=gen)) + geom_point(color="purple") + geom_smooth(aes(x=Year,y=gen),color="purple",method="glm", formula = y~x, method.args=list(family=gaussian(link="log"))) + geom_text(label = lm_eq(mad[["Timeline"]]), parse = TRUE) +
  geom_point(data=mad[["Timeline"]], mapping = aes(x=Year, y=pro), color="orange") + geom_smooth(aes(x=Year,y=pro),color="orange",method="glm",formula = y~x, method.args=list(family=gaussian(link="log"))) +
  geom_point(data=mad[["Timeline"]], mapping = aes(x=Year, y=met), color="blue") + geom_smooth(aes(x=Year,y=met),color="blue",method="glm", formula = y~x, method.args=list(family=gaussian(link="log")))

but I got this error: Error in eval(predvars, data, env) : object 'y' not found.

I want to add the R2 values for the gen, pro, and met curves, but I am not sure how to proceed.

Ann
  • 21
  • 2
  • 1
    Have you tried [this solution](https://stackoverflow.com/a/35140066/8245406)? – Rui Barradas Jun 30 '20 at 15:28
  • @RuiBarradas This works, thank you! However, it only gives me one R2 value. How do I add an R2 value for each curve? – Ann Jun 30 '20 at 15:43
  • That post is very long, and I believe that the answer is near the bottom. See the last 4 plots. – Rui Barradas Jun 30 '20 at 15:50
  • 2
    Does this answer your question? [Add regression line equation and R^2 on graph](https://stackoverflow.com/questions/7549694/add-regression-line-equation-and-r2-on-graph) – OTStats Jun 30 '20 at 16:01
  • https://cran.r-project.org/web/packages/ggpmisc/vignettes/user-guide.html i think one of the plots shows how to have more than 1 R^2 .. i would suggest you pivot the data long – StupidWolf Jun 30 '20 at 18:16
  • Most of the post linked here are meant for regression models using lm() , which a OLS model. The glm you have called has a log-link, which is not usual. My point is, are you sure about this? If so, I think you need to calculate the R^2 separately. – StupidWolf Jun 30 '20 at 18:51

0 Answers0