# Re: [R] plotmath question

From: Deepayan Sarkar <deepayan_at_stat.wisc.edu>
Date: Sat 19 Mar 2005 - 09:20:44 EST

On Friday 18 March 2005 15:41, Berton Gunter wrote:
> R listers:
>
> I have been foiled by plotmath!
>
> (in R 2.01,Windows 2000)
>
> The task: Plot a normal density and label the ticks as mu - 3 sigma, mu - 2
> sigma, ...., mu + 3 sigma, where the mu's and sigmas appear as Greek
> symbols, of course.
>
> The following code does this:
>
> x<-seq(-3,to=3,by=.01)
> y<-dnorm(x)
> plot(x,y,type='h',col='lightblue',axes=FALSE)
> lines(x,y,col='darkblue')
> axis(2)
> for(i in seq(-3,to=3))
> axis(1,at=i, lab=switch(sign(i)+2,
> eval(substitute(expression(mu-j*sigma),list(j=-i))),
> expression(mu),
> eval(substitute(expression(mu+j*sigma),list(j=i)))))
> box()
>
> However, I think the code in the for loop is ugly and probably means that
> I'm doing it wrong. In particular:
>
> 1) Is there a neat way to use one axis() call and a vector (of
> expressions?) for the lab=argument?

Yes, expression objects can be vectors. e.g.:

## use switch as above for better formatting lab = do.call(expression,

```              lapply(-3:3, function(i) {
bquote(mu + .(i) * sigma)
} ))
```

axis(1, at = -3:3, lab = lab)

> 2) The plotmath Help state that expressions can be used for axis labels, so
> I would have expected the above to work without the eval()call -- but it
> does not. Would someone kindly explain to me why not -- i.e., what I have
> misunderstood. That is, to be clear, why does the following not work:
>
> for(i in seq(-3,to=3))
> axis(1,at=i, lab=switch(sign(i)+2,
> substitute(expression(mu-j*sigma),list(j=i)),
> expression(mu),
> substitute(expression(mu+j*sigma),list(j=i))))

> is.expression(substitute(expression(mu-j*sigma),list(j=1)))
 FALSE
> is.expression(eval(substitute(expression(mu-j*sigma),list(j=1))))
 TRUE ?substitute says

Value:

The 'mode' of the result is generally '"call"' ...

which evidently have to be evaluated.

Hth,

Deepayan

R-help@stat.math.ethz.ch mailing list