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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri 22 Jul 2005 - 07:15:30 EST

Use bquote instead of expression, e.g.

trees.lm <- lm(Volume ~ Girth, trees)
trees.sm <- summary(trees.lm)
trees.co <- round(trees.sm$coefficients,2)
trees.rsq <- round(trees.sm$r.squared,2)

plot(Volume ~ Girth, trees)

text(10,60, bquote(Intercept : .(trees.co[1,1])%+-%.(trees.co[1,2])), pos = 4)
text(10,57, bquote(Slope : .(trees.co[2,1])%+-%.(trees.co[2,2])), pos = 4)
text(10,54,bquote(R^2 : .(trees.rsq)), pos = 4)


On 7/21/05, Dan Bolser <dmb@mrc-dunn.cam.ac.uk> wrote:
> On Thu, 21 Jul 2005, Christoph Buser wrote:
>
> >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)

> >
>
> Cheers for this.
>
> I was trying to get it to work, but the problem is that I need to replace
> the values above with variables, from the following code...
>
>
> dat.lm <- lm(dat$AVG_CH_PAIRS ~ dat$CHAINS)
> dat.lm.sum <- summary(dat.lm)
>
> my.slope.1 <- round(dat.lm.sum$coefficients[2],2)
> my.slope.2 <- round(dat.lm.sum$coefficients[4],2)
>
> my.inter.1 <- round(dat.lm.sum$coefficients[1],2)
> my.inter.2 <- round(dat.lm.sum$coefficients[3],2)
>
> my.Rsqua.1 <- round(dat.lm.sum$r.squared,2)
>
>
> Anything I try results in either the words 'paste("Slope:", my.slope.1,
> %+-%my.slope.2,sep="")' being written to the plot, or just
> 'my.slope.1+-my.slope2' (where the +- is correctly written).
>
> I want to script it up and write all three lines to the plot with
> 'sep="\n"', rather than deciding three different heights.
>
>
> >I do not have an elegant solution for the alignment.

>
> Thanks very much for what you gave, its a good start for me to figure out
> how I am supposed to be telling R what to do!
>
> Any way to just get fixed width fonts with text? (for the alignment
> problem)
>
>
> Cheers,
> Dan.
>
> >
> >Regards,
> >
> >Christoph Buser
> >
> >--------------------------------------------------------------
> >Christoph Buser <buser@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@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
> >
>
> ______________________________________________
> 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
>



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 Fri Jul 22 07:19:44 2005

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