[R] expressions and paste

Paul Murrell p.murrell at auckland.ac.nz
Thu Nov 11 20:31:10 CET 2004


Hi


Thomas Lumley wrote:
> On Thu, 11 Nov 2004, Emily Baldock wrote:
> 
>> I have written a function to plot data which will be used for various 
>> different chemistries.
>> A simplified version is:
>> plot_data <- function(risk,levels,chem,sd2,measure){
>>  plot(risk, levels,main=paste ("per", sd2, measure, "\n in usual", chem))
>> }
>> The problem is with the title.
>> This works fine if the variable "chem" is just text, but if it is an 
>> expression then obviously it won't work.
>> I have experimented with various things and I am at a complete loss 
>> for how to insert an expression into the middle of
>> a title.
> 
> 
> With
>  sd2<-10
>  measure<-quote(mu*g*m^{-3})
>  chem<-quote(H[2]*SO[4])
> You can use
>  plot(1,1,main=bquote("per "*.(sd2)*.(measure)*" in usual "*.(chem)))
> or
>  plot(1,1,main=substitute("per "*sd2*measure*" in usual "*chem,
>    list(sd2=sd2,measure=measure,chem=chem)) )
> 
> You don't get the newlines, someone else will have to work this out.


Expressions (for plotting text) don't handle newlines.  A workaround 
would be to do the title "by hand" by calling mtext to place two 
separate expressions on separate lines.  Something like ...

sd2<-10
measure<-quote(mu*g*m^{-3})
chem<-quote(H[2]*SO[4])
plot_data <- function(risk,levels,chem,sd2,measure){
   plot(1,1)
   mtext(substitute("per "*sd2*measure,
                    list(sd2=sd2,measure=measure)),
         side=3, line=2)
   mtext(substitute("in usual "*chem,
                    list(chem=chem)),
         side=3, line=1)
}
plot_data(1, 2, chem, sd2, measure)

Paul
-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/




More information about the R-help mailing list