[R] parse in text function of plots, confusing behaviour

Duncan Murdoch murdoch at stats.uwo.ca
Tue Sep 25 00:32:07 CEST 2007


On 24/09/2007 5:55 PM, Folkes, Michael wrote:
> HI all,
> I'm failing to understand why the following is happening.
> In this plot I rely on two text functions both using parse.  The second one works properly by writing a gamma symbol 5 times, the first one only works properly four times.  The only difference is that I add a string to the paste function of that which does work properly.  Why does it behave like this?
> thanks so much!
> Michael Folkes
>  
>  
> plot(1,1,type='n',ylim=c(-2,2))
> for(gam in seq(-1,.25,length=5)){
>  #doesn't repeat gamma symbol properly
>   text(.8,gam,parse(text=paste("gamma",gam,sep='')),cex=.75,adj=0)
>  
> # however this works if an additional string is included in the paste function
>   text(1,gam,parse(text=paste("gamma","~hi",gam,sep='')),cex=.75,adj=0)
> }


Take a look at what you're trying to plot:

 > for (gam in seq(-1, 0.25, length=5)) {
+   print(parse(text=paste("gamma",gam,sep='')))
+ }
expression(gamma-1)
attr(,"srcfile")
<text>
expression(gamma-0.6875)
attr(,"srcfile")
<text>
expression(gamma-0.375)
attr(,"srcfile")
<text>
expression(gamma-0.0625)
attr(,"srcfile")
<text>
expression(gamma0.25)
attr(,"srcfile")
<text>

In the first 4 cases, it's "gamma - value", while the last one is 
"gammavalue".

You probably want something like the bquote example on the ?plotmath 
page, e.g.

 > for (gam in seq(-1, 0.25, length=5)) {
+   text(1, gam, bquote(paste(gamma," ",.(gam))))
+ }

Duncan Murdoch



More information about the R-help mailing list