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)
Christoph Buser
2005-Jul-21 13:18 UTC
[R] Question about 'text' (add lm summary to a plot)
Dear Dan I can only help you with your third problem, expression and paste. You can use: plot(1:5,1:5, type = "n") text(2,4,expression(paste("Slope : ", 3.45%+-%0.34, sep = "")), pos = 4) text(2,3.8,expression(paste("Intercept : ", -10.43%+-%1.42)), pos = 4) text(2,3.6,expression(paste(R^2,": ", "0.78", sep = "")), pos = 4) I do not have an elegant solution for the alignment. Regards, Christoph Buser -------------------------------------------------------------- Christoph Buser <buser at stat.math.ethz.ch> Seminar fuer Statistik, LEO C13 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-44-632-4673 fax: 632-1228 http://stat.ethz.ch/~buser/ -------------------------------------------------------------- Dan Bolser writes: > > 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 at 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