2

i have generated a graph from this question:

plot multiple columns on the same graph in R

which include multiple variables on the same graph along with their regression lines.

i also have the equation and the R2, but i don't know how to place it on the graph.

   ddply( d, "variable", function(u) { 
   r <- lm(value ~ Xax, data=u); 
    c(coef(r), r.squared=summary(r)$r.squared) } )

which is also found in the same question

now i want to generate the regression equation and R2 value and place it on the graph for each variable.(e.g place the equation and R2 on the left or the right of each linear line)

how this can be done??

thank you

Community
  • 1
  • 1
ifreak
  • 1,726
  • 4
  • 27
  • 45
  • 1
    [This](http://stackoverflow.com/q/7549694/324364) should get you started (and is arguably a duplicate). Placing each eqn "near" each line will be difficult to do sensibly in an automated fashion. You will have more luck specifying the coordinates directly. – joran Mar 05 '12 at 15:50
  • thank you, but how to get the error out of this equation?? is it the R2 or another variable?? – ifreak Mar 05 '12 at 16:37
  • Your question only mentioned the coefficients and the R^2 value. I don't know what you mean by "error". – joran Mar 05 '12 at 16:40
  • the linear regression error(standard error). is the same as R2, or does it have another variable in this function(lm)?? – ifreak Mar 05 '12 at 16:42
  • "linear regression error" could refer to several things, but it is definitely not R^2. Perhaps you want to take the average (or sum?) of the squared residuals? You can see how to access them by reading `?lm`. – joran Mar 05 '12 at 16:46
  • i want the standard error. e.g: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.32129 0.73070 -0.44 0.66 epiNeur 0.68200 0.06353 10.74 <2e-16 *** – ifreak Mar 05 '12 at 16:48
  • SO is a site for asking+answering _single_ questions at a time; it is not a "help desk" that provides extended support in the comments. – joran Mar 05 '12 at 16:51
  • ok, sorry, i'll just ask this in a new question. thank you – ifreak Mar 05 '12 at 16:52

1 Answers1

2

Here is one possibility, using @Vincent's code. It works with the latest release of ggplot2 (v. 0.9) and the R-forge version of directlabels (v. 2.5). I also have tested the code with ggplot2 0.8.9 and directlabels 2.4. (The version of directlabels released on CRAN won't work with ggplot2 0.9, though.)

The idea is basically to replace your labels A, B, C, G with the regression equations. Of course, you could store the latter in a different manner but I think this would sensibly complicate the plotting expression, so let's keep that as simple as possible. Assuming we already have @Vincent's melted variable d,

> head(d)
    Xax variable value
1  0.22        A 0.451
2  0.34        A 0.491
3  0.54        A 0.389
4  0.34        A 0.425
5  0.53        A 0.457
6  0.12        A 0.436

let's replace variable labels with the equations you computed:

library(plyr)
lm.stats <- ddply(d, "variable", 
                  function(u) { 
                    r <- lm(value ~ Xax, data=u)
                    c(coef(r), r.squared=summary(r)$r.squared) 
                  })
my.formatter <- function(x, digits=2) {
  x <- round(x, digits=digits)
  out <- paste(x[1], ifelse(x[2]>0, "+", ""), x[2], "x", sep="")
  out <- paste(out, " (R2=", x[3], ")", sep="")
  return(out)
}
d$variablef <- d$variable
levels(d$variablef) <- apply(lm.stats[2:4], 1, my.formatter)

The little helper function, my.formatter, is in charge of assembling the different statistics you computed with ddply. Note that I made a copy of variable in case we need this latter. And here is the plotting stuff:

p <- ggplot(d, aes(Xax,value, col=variablef)) + 
       geom_point() + 
       stat_smooth(method=lm) 
library(directlabels)
direct.label(p)

I should note that you can also have annotated curves with the labcurve() function from the Hmisc package. I can also imagine simpler solutions using ggplot or lattice, namely just write the regression equations along the regression lines, with proper orientation and a slight shift on the x-axis to avoid overlapping, but that might not necessarily be very portable if your dataset happens to change.

enter image description here

Community
  • 1
  • 1
chl
  • 27,771
  • 5
  • 51
  • 71
  • thank you for your answer, i tried it but i've got an error: Error in direct.label.ggplot(p) : could not find function "guides" – ifreak Mar 06 '12 at 09:02
  • @ifreak What versions of ggplot2 and directlabels are you using? The above was tested with ggplot2 0.8.9 and directlabels 2.4 (R 2.14.1 on x86_64-apple-darwin9.8.0/x86_64). – chl Mar 06 '12 at 11:31
  • i have ggplot2 0.9.8 and directlabels i have the latest version which i downloaded today. and R 2.11 on fedora – ifreak Mar 06 '12 at 11:43
  • @ifreak As I said, it won't work with the latest release of ggplot2 (0.9) which you seem to be using (I don't know about such versioning, *0.9.8*, so maybe I'm wrong and you're using the 0.8.9 version though `guides` were introduced in 0.9, AFAIK). Can you try to grab the src 0.8.9 package from CRAN and install it on your machine? – chl Mar 06 '12 at 12:15
  • sorry, i just found that i have this version: ggplot2_0.8.9 and it's not working – ifreak Mar 06 '12 at 12:18
  • There seems to be a [new version](http://r-forge.r-project.org/R/?group_id=481) of `directlabels` on R-forge but I cannot access it at the moment. Maybe worth to try if it works better with this one (and maybe `ggplot 0.9`)? The error about `guides` looks strange to me; google did not help, though I suspect it has caused some problems during compilation in the past. – chl Mar 06 '12 at 14:30