[R] sub- and superscript in plot labels

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Nov 4 16:03:11 CET 2004


On Thu, 4 Nov 2004, Gavin Simpson wrote:

> Prof Brian Ripley wrote:
> > On Thu, 4 Nov 2004, Gavin Simpson wrote:
> > 
> > 
> >>Dear List,
> >>
> >>I need to add a subscript and a superscript to some of the ions in the 
> >>labels on some plots.
> >>
> >>I have got to here but now I'm stuck:
> >>
> >>plot(1:10, xlab = expression(paste("nm SO"[4], " ", mu, "eq cm"^{-2}, " 
> >>yr"^{-1})))
> >>
> >>Which gives almost  what I require. No matter what I tried, however, I 
> >>could not get bot a sub script *and* a superscript attached to the SO in 
> >>the label.
> >>
> >>In LaTeX I would just do $SO_4^{2-}$ but taking that to R produces a 
> >>syntax error:
> >>
> >>plot(1:10, xlab = expression(paste("nm SO"[4]^{2-}, " ", mu, "eq 
> >>cm"^{-2}, " yr"^{-1})))
> > 
> > 
> > The problem is 2-.  That's not an R expression.  Using "2-" might give
> > what you want, but it will use a hyphen rather than a minus. Otherwise
> > 
> > plot(1:10, xlab = expression(paste("nm SO"[4]^{2-phantom()})))
> > 
> > will give a minus.
> > 
> 
> Many thanks to Brian Ripley and Bendix Carstensen for your replies.
> 
> Both of the above options produce what I was after. One quick follow-up 
> question regarding the use of phantom(). Looking at ?plotmath phantom 
> leaves space for a character passed as an argument to phantom(), but 
> does not plot it.
> 
> In the example above we are leaving space for "nothing". I don't 
> understand why this is a valid R expression. I guess phantom() is 
> returning something that makes 2-<returned_val> a valid expression, but 
> I couldn't find the help for ?phantom so I couldn't check on this in the 
> documentation.

Yes, that is a valid formal expression, so R's parser is happy.

> Also as an aside, phantom() appears, visually, to be a function, but it 
> is not visible to the user as a function. i.e. typing phantom at the 
> prompt yields: Error: Object "phantom" not found. getAnywhere(phantom) 
> yields nothing either. What is phantom() in R parlance?

It's part of the language of formal expressions that plotmath accepts.  
It is not part of R per se.  Think of it as a private function to 
plotmath's internal code (and there are quite a few others in plotmath).

It is an analogue of TeX's \hphantom and \vphantom, and like them used as 
a placeholder.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list