-1

I'm trying to make a lot of graphs using ggplot2 script, and add some text (Lm equation and r2 value, using this function) for each graph.

The issue is that my x and y coordinates will be different between each graph.

With 'plot' function, you can convert 'plot' coords to 'figure' coords using cnvr.coord function, but in ggplot2 (grid base package), isn't functionally.

below and example (where "p" is a preexistent ggplot2 object) :

p <- p + geom_text(aes(X, Y, label = lm_eqn(lm(as.numeric(a$value) ~ as.numeric(a$date), a))))

Community
  • 1
  • 1
  • You probably want to use `annotate` for something like this, not `geom_text`, [see here](http://docs.ggplot2.org/0.9.3/annotate.html). If this doesn't solve your problem, you should try to make your question reproducible, [see here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Gregor Thomas Sep 04 '13 at 18:17
  • As far as figure coords, you may just need to calculate where it goes based on the range of your data, but someone clever might come along with a better idea. – Gregor Thomas Sep 04 '13 at 18:20
  • The use of `annotate` keeps the problem. You need be explicit with the coords X and Y for each graph. For other side, the calculation of coords based on the range of data (which are quite different each), the coords will be different for each data sets. The best solution, I think, would be graphic device related. Thanks for your comments – Eduardo Bustos Sep 04 '13 at 18:56

1 Answers1

1

I agree with shujaa. You can simply calculate where the function goes based on the range of your data. Using your link above, I've created an example:

library(ggplot2)
df1 <- data.frame(x = c(1:100))
df1$y <- 2 + 3 * df1$x + rnorm(100, sd = 40)
df1$grp <- rep("Group 1",100)
df2 <- data.frame(x = c(1:100))
df2$y <- 10 -.5 * df2$x + rnorm(100, sd = 100)
df2$grp <- rep("Group 2",100)
df3 <- data.frame(x = c(1:100))
df3$y <- -5 + .2 * df3$x + rnorm(100, sd = 10)
df3$grp <- rep("Group 3",100)
df4 <- data.frame(x = c(1:100))
df4$y <- 2 - 3 * df4$x + rnorm(100, sd = 40)
df4$grp <- rep("Group 4",100)

df <- list(df1,df2,df3,df4)

lm_eqn = function(df) {
  m = lm(y ~ x, df);
  l <- list(a = format(coef(m)[1], digits = 2),
      b = format(abs(coef(m)[2]), digits = 2),
      r2 = format(summary(m)$r.squared, digits = 3));

  if (coef(m)[2] >= 0)  {
    eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,l)
  } else {
    eq <- substitute(italic(y) == a - b %.% italic(x)*","~~italic(r)^2~"="~r2,l)    
  }

  as.character(as.expression(eq));                 
}
pdf("I:/test.pdf")
for (i in 1:4) {
text.x <- ifelse(lm(df[[i]]$y~1+df[[i]]$x)$coef[2]>0,min(df[[i]]$x),max(df[[i]]$x))
text.y <- max(df[[i]]$y)
text.hjust <- ifelse(lm(df[[i]]$y~1+df[[i]]$x)$coef[2]>0,0,1)
p <- ggplot(data = df[[i]], aes(x = x, y = y)) + 
            geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
            geom_point()

p1 = p + geom_text(aes(x = text.x, y = text.y, label = lm_eqn(df[[i]])), parse = TRUE,hjust=text.hjust)
print(p1)
}
dev.off()
Community
  • 1
  • 1
Mark Nielsen
  • 991
  • 2
  • 10
  • 28