Skip to content

sub- and superscript in plot labels

6 messages · Brian Ripley, Gavin Simpson, Thomas Lumley

#
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})))

Strangely, I can do this:

plot(1:10, xlab = expression(paste("nm SO"[4]^2, " ", mu, "eq cm"^{-2}, 
" yr"^{-1})))

With almost the desired effect (except I need to add two characters to 
the superscript).

Can any one offer a solution?

Many thanks

Gavin
#
On Thu, 4 Nov 2004, Gavin Simpson wrote:

            
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.
#
Gavin Simpson wrote:
With further playing around, it would appear that you can sort of 
achieve what I was looking for by changing the ordering of the 
characters in the braces on the superscript:

plot(1:10, xlab = expression(paste("nm SO"[4]^{-2}, " ", mu, "eq
cm"^{-2}, " yr"^{-1})))

which works as required.

Is this intentional behaviour? My original question still stands, 
however, is there a way to have {2-} appear as the superscript?

The forgotten version info (sorry!):

 > version
          _
platform i686-pc-linux-gnu
arch     i686
os       linux-gnu
system   i686, linux-gnu
status   Patched
major    2
minor    0.0
year     2004
month    10
day      25
language R

Running on Fedora Core 2.

Many thanks

Gavin
#
Prof Brian Ripley wrote:
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.

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?

All the best,

Gavin
#
On Thu, 4 Nov 2004, Gavin Simpson wrote:

            
Yes, that is a valid formal expression, so R's parser is happy.
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.
#
On Thu, 4 Nov 2004, Gavin Simpson wrote:
phantom is not part of the R language, just part of the language that 
plotmath understands (like frac and scriptstyle).  The relevant help page 
is ?plotmath.

As to why it works: phantom() returns an empty box large enough to hold 
its argument.  When given no argument, it returns a very small empty box.

In this example the importance of phantom() is that 2-phantom() is parsed 
as a binary operation, indicating that the - character should be typeset 
as a minus sign.   Writing 2- is a parse error, and writing "2-" indicates 
that the - character is ordinary text and should be typeset as a 
hyphen.

 	-thomas