[R] , how to express bar(zeta) in main title in boxplot

Peter Dalgaard pdalgd at gmail.com
Fri Jul 23 00:39:45 CEST 2010


Peter Ehlers wrote:
> On 2010-07-22 15:21, David Winsemius wrote:
>> On Jul 22, 2010, at 5:01 PM, Peter Ehlers wrote:
>>
>>> On 2010-07-22 14:40, David Winsemius wrote:
>>>> On Jul 22, 2010, at 4:24 PM, Peter Ehlers wrote:
>>>>
>>>>> On 2010-07-22 11:44, Marcus Liu wrote:
>>>>>> Hi everyone, I am plotting a boxplot with main title as main =
>>>>>> bquote(paste(.(ts.ind[s]), ": ", bar(zeta), " Boxplot from 2001 to
>>>>>> 2009", sep = "")) but it doesn't work. The program said they cannot
>>>>>> find the function "bar". Does anyone know how to do it correctly?
>>>>>> Thanks.
>>>>>>
>>>>> A reproducible example with the exact error message would
>>>>> be good. Anyway, it seems pretty clear what you want and
>>>>> one solution is to _not_ use 'main='. For base graphics,
>>>>> I usually prefer to add titles with the title() function
>>>>> which will work here.
>>>>>
>>>>> a <- pi
>>>>> boxplot(rnorm(200))
>>>>> title(bquote(paste(.(a), ": ", bar(zeta),
>>>>> " Boxplot from 2001 to 2009", sep = "")))
>>>>>
>>>>> It seems that setting main=<...> where <...> contains
>>>>> bquote() works with plot(), but not with boxplot().
>>>> The help page for boxplot does not document a "main" argument, nor is it
>>>> in the argument list for boxplot.default or its bxp function. The
>>>> documentation for the "..." argument does not suggest, to me anyway,
>>>> that main would passed on to other graphical functions.
>>>>
>>> Well, that's not quite so. From ?bxp:
>>>
>>> ".... and main, cex.main, col.main, sub, cex.sub, col.sub, xlab,
>>> ylab, cex.lab, and col.lab are passed to title"
>> I stand (or sit) corrected.
>>
>>> # To wit:
>>> y <- rnorm(200)
>>> g <- gl(2,100)
>>> boxplot(y ~ g, main="My title")
>>>
>>> # But I wasn't right, either:
>>> a <- pi
>>> boxplot(y ~ g, main=paste("hello", bquote(.(a)), "goodbye"))
>>> boxplot(y ~ g, main=expression("hello"~~bar(zeta)~~"goodbye"))
>>>
>>> I do usually prefer to use main="" and then supply the title
>>> separately. But I'll wager that either Gabor or Uwe or ...
>>> will make main= work for the OP.
>> Both are working on my machine.
>>
> 
> I thought I was only getting this to work with either the
> paste(..., bquote(), ...) form *or* the
> expression(... bar(zeta) ...) form,
> but I see that the paste() in the OP's request was not
> necessary and so his question is also answered by:
> 
>    boxplot(y ~ g, main =
>          bquote(.(a) ~~ "hello" ~~ bar(zeta) ~~ "goodbye"))
> 
> which really should have been obvious to me. Duh...
> 
> Curiously (?), plot() will accept this:
> 
>    plot(0, main = bquote(paste(.(a), "hello", bar(zeta), "goodbye")))
> 
> which had led me astray.
> 
> Now, why doesn't this work with lattice's bwplot?

How did lattice get into this? Anyways, it also works if you use
as.expression(bquote(.....)), which suggests that the culprit is that
objects of mode "call" are (sometimes) not protected from evaluation,
where mode "expression" is. I.e.

eval(substitute(I(x), list(x=quote(2+2))))

vs.

eval(substitute(I(x), list(x=expression(2+2))))


Nice example of a real-life case of this effect, hang on to it and we
can dig to the bottom of it some other time.



-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list