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

Dan Bolser dmb at mrc-dunn.cam.ac.uk
Thu Jul 21 12:20:24 CEST 2005

```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

text(x=3,
y=300,
paste(my.text.1,
my.text.2,
my.text.3,
sep="\n"),