[R] substitute/paste question for using Greek in plot titles

Roger D. Peng rpeng at jhsph.edu
Thu Nov 11 05:08:23 CET 2004


This is not a bug.  The object passed to `main' should be either character or an 
expression (documented in ?title).  substitute() returns neither a character 
object nor an expression -- it return a call object.  This is documented in 
?substitute:

      Substituting and quoting often causes confusion when the argument
      is 'expression(...)'. The result is a call to the 'expression'
      constructor function and needs to be evaluated with 'eval' to give
      the actual expression object.

What you want, I think, is

label <- substitute(expression(paste("A vaue for ",phi," = ",phival)), 
list(phival=phi.1))
plot(0 ~ 0, main = eval(label))

-roger

Sundar Dorai-Raj wrote:
> 
> 
> Peter Dunn wrote:
> 
>> Hi all
>>
>> I am having troubles making sense of why code (1)
>> below fails but code (2) below works.
>>
>> Code (1):
>>
>>  > phi.1 <- 1
>>  > plot(0 ~ 0,
>> + main=substitute(paste("A vaue for ",phi," = ",phival), 
>> list(phival=phi.1)) )
>>
>> Error in paste("The two deviances for ", phi, " = ", 2) :
>>         Object "phi" not found
>>
>> But this works:
>>
>> Code (2):
>>  > plot(0,0,
>> + main=substitute(paste("A value for ",phi," = ",phival), 
>> list(phival=phi.1)) )
>>  >
>>
>> It appears that if the plot command takes the formula style entry,
>> the substitue/paste fails.
>>
>> Is this documented as a feature (I couldn't find it if that is the
>> case), or is it a bug?  If it is a feature, it is a subtle difference
>> between (1) and (2) that has potential to be quite frustrating!
>>
>> Perhaps I should just upgrade to version 2.0.0, though I can't see
>> anything in the Release Notes that might cause this.
>>
>> Thanks.
>>
>> P.
>>
>>  > version
>>          _
>> platform i686-pc-linux-gnu
>> arch     i686
>> os       linux-gnu
>> system   i686, linux-gnu
>> status
>> major    1
>> minor    9.1
>> year     2004
>> month    06
>> day      21
>> language R
>>
>>
> 
> Peter,
> 
> Because in the first couple of lines of plot.formula we see this:
> 
> dots <- m$...
> dots <- lapply(dots, eval, data, parent.frame())
> 
> which for your case is equivalent to:
> 
> expr <- substitute(paste("A vaue for ",phi," = ",phival), 
> list(phival=phi.1))
> eval(expr)
> 
> which returns an error saying "phi" cannot be found which is the correct 
> behaviour of eval. I'll let others comment on whether or not this is a 
> bug in plot.formula but you can always get around it by calling title:
> 
> plot(0 ~ 0)
> title(main = expr)
> 
> which is exactly what your second example is doing in plot.default.
> 
> HTH,
> 
> --sundar
> 
> ______________________________________________
> R-help at 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
> 

-- 
Roger D. Peng
http://www.biostat.jhsph.edu/~rpeng/




More information about the R-help mailing list