[R] Question about 'text' (add lm summary to a plot)

From: Dan Bolser <dmb_at_mrc-dunn.cam.ac.uk>
Date: Thu 21 Jul 2005 - 20:20:24 EST

I would like to annotate my plot with a little box containing the slope, intercept and R^2 of a lm on the data.

I would like it to look like...

 +----------------------------+

| Slope : 3.45 +- 0.34 |
| Intercept : -10.43 +- 1.42 |
| R^2 : 0.78 |
+----------------------------+

However I can't make anything this neat, and I can't find out how to combine this with symbols for R^2 / +- (plus minus).

Below is my best attempt (which is franky quite pour). Can anyone improve on the below?

Specifically,

aligned text and numbers,
aligned decimal places,
symbol for R^2 in the text (expression(R^2) seems to fail with 'paste') and +-

Cheers,
Dan.

dat.lm <- lm(dat$AVG_CH_PAIRS ~ dat$CHAINS)

abline(coef(dat.lm),lty=2,lwd=1.5)

dat.lm.sum <- summary(dat.lm)
dat.lm.sum

attributes(dat.lm.sum)

my.text.1 <-

  paste("Slope : ",     round(dat.lm.sum$coefficients[2],2),
        "+/-",          round(dat.lm.sum$coefficients[4],2))

my.text.2 <-
  paste("Intercept : ", round(dat.lm.sum$coefficients[1],2),

        "+/-", round(dat.lm.sum$coefficients[3],2))

my.text.3 <-
  paste("R^2 : ", round(dat.lm.sum$r.squared,2))

my.text.1
my.text.2
my.text.3


## Add legend
text(x=3,

     y=300,
     paste(my.text.1,
           my.text.2,
           my.text.3,
           sep="\n"),
     adj=c(0,0),
     cex=1)

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jul 21 20:24:43 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:53 EST