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

Dan Bolser dmb at mrc-dunn.cam.ac.uk
Sat Jul 23 17:11:49 CEST 2005


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




More information about the R-help mailing list