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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sun 24 Jul 2005 - 02:09:49 EST

Here are a few tweaks. We form a matrix and use round and format to automatically get the right number of decimals and widths right. We have added extra digits to show that they do not affect the result. Also the paste in the R^2 line was eliminated.

nn <- matrix(c(-10.661, 1.961,                    # intercept
                 3.221, 0.131,                    # slope
                 0.971, NA), 3, 2, byrow = TRUE) # Rsquared
nnf <- apply(round(nn,2), 2, format)

plot(1:5)

L <- list("Intercept",

         "Slope",
         bquote(R^2),
         bquote(.(nnf[1,1]) %+-% .(nnf[1,2])),
         bquote(.(nnf[2,1]) %+-% .(nnf[2,2])),
         bquote(.(nnf[3,1])))

par(family = "mono")

legend("topleft", #inset=-1,

      legend = do.call("expression", L),
      bg='white',
      ncol = 2,
      pch=c('','','',':',':',':'),
      x.intersp = 0.4,
      title="Yay! Thank You!"
      )




On 7/23/05, Dan Bolser <dmb@mrc-dunn.cam.ac.uk> wrote:
> On Fri, 22 Jul 2005, Marc Schwartz (via MN) wrote:
>
> >Ok guys,
> >
> >So I played around with this a bit, going back to Dan's original
> >requirements and using Thomas' do.call() approach with legend(). Gabor's
> >approach using sapply() will also work here. I have the following:
> >
> ># Note the leading spaces here for alignment in the table
> ># This could be automated with formatC() or sprintf()
> >my.slope.1 <- " 3.22"
> >my.slope.2 <- "0.13"
> >my.inter.1 <- "-10.66"
> >my.inter.2 <- "1.96"
> >my.Rsqua <- " 0.97"
> >
> >plot(1:5)
> >
> >L <- list("Intercept:",
> > "Slope:",
> > bquote(paste(R^2, ":")),
> > bquote(.(my.inter.1) %+-% .(my.inter.2)),
> > bquote(.(my.slope.1) %+-% .(my.slope.2)),
> > bquote(.(my.Rsqua)))
> >
> >par(family = "mono")
> >
> >legend("topleft", legend = do.call("expression", L), ncol = 2)
> >
> >
> >
> >Note however, that while using the mono font helps with vertical
> >alignment of numbers, the +/- sign still comes out in the default font,
> >which is bold[er] than the text.
> >
> >If one uses the default font, which is variable spaced, it is
> >problematic to get the proper alignment for the numbers. I even tried
> >using phantom(), but that didn't quite get it, since the spacing is
> >variable, as opposed to LaTeX's mono numeric spacing with default fonts.
> >
> >Also, note that I am only using two columns, rather than three, since
> >trying to place the ":" as a middle column results in spacing that is
> >too wide, given that the text.width argument is a scalar and is set to
> >the maximum width of the character vectors.
> >
> >Note also that even with mono spaced fonts, the exponent in R^2 is still
> >horizontally smaller than the other characters. Thus, spacing on that
> >line may also be affected depending upon what else one might attempt.
> >
> >Not sure where else to go from here.
>
> Ahhhhhh... So lovely! Thank you all so much!
>
> I made a couple of tweeks to improve the overall appearance, using
> "x.intersp = 0.1" tightens up the overall appearance, and using
> "pch=c('','','',':',':',':')" adds the (aligned!) colons.
>
> Here is the beauty...
>
>
>
> my.slope.1 <- " 3.22"
> my.slope.2 <- "0.13"
> my.inter.1 <- " -10.66"
> my.inter.2 <- "1.96"
> my.Rsqua <- " 0.97"
>
> plot(1:5)
>
> L <- list("Intercept",
> "Slope ",
> bquote(paste(R^2)),
> bquote(.(my.inter.1) %+-% .(my.inter.2)),
> bquote(.(my.slope.1) %+-% .(my.slope.2)),
> bquote(.(my.Rsqua)))
>
> par(family = "mono")
>
> legend("topleft", #inset=-1,
> legend = do.call("expression", L),
> bg='white',
> ncol = 2,
> pch=c('','','',':',':',':'),
> x.intersp = 0.1,
> title="Yay! Thank You!"
> )
>
>
> However (the final gripe ;) it seems 'inset=' dosn't work. Setting this to
> anything (including the default) seems to surpress the legend without
> error. But hey!
>
> Thanks again,
>
>
>
>
>
>
>
> >
> >HTH,
> >
> >Marc Schwartz
> >
> >On Fri, 2005-07-22 at 14:01 -0400, Gabor Grothendieck wrote:
> >> You are right. One would have to use do.call as you did
> >> or the sapply method of one of my previous posts:
> >>
> >> a <- 7
> >> plot(1)
> >> L <- list(bquote(alpha==.(a)),bquote(alpha^2+1==.(a^2+1)))
> >> legend("topleft",legend=sapply(L, as.expression))
> >>
> >>
> >> On 7/22/05, Thomas Lumley <tlumley@u.washington.edu> wrote:
> >> > On Fri, 22 Jul 2005, Gabor Grothendieck wrote:
> >> > >
> >> > > I think legend accepts a list argument directly so that could be
> >> > > simplified to just:
> >> > >
> >> > > a<-7
> >> > > plot(1)
> >> > > L <- list(bquote(alpha==.(a)),bquote(alpha^2+1==.(a^2+1)))
> >> > > legend("topleft",legend=L)
> >> >
> >> > Except that it wouldn't then work: the mathematical stuff comes out as
> >> > text.
> >> >
> >> > > The same comment seems to apply to my prior suggestion about
> >> > > as.expression(bquote(...)), namely that one can just write the
> >> > > following as text also supports a list argument:
> >> >
> >> > And this doesn't work either: you end up with %+-% rather than the
> >> > plus-or-minus symbol.
> >> >
> >> > The reason I gave the do.call() version is that I had tried these simpler
> >> > versions and they didn't work.
> >> >
> >> > -thomas
> >> >
> >
>
>



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 Sun Jul 24 02:16:04 2005

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