[R] label chemical formulas

Prof Brian D Ripley ripley at stats.ox.ac.uk
Mon Aug 4 17:23:22 CEST 2003

On Mon, 4 Aug 2003, Roberto Canteri wrote:

[This is a repeat of a post earlier today.]

> Dear r-help,
> the prcomp matrix (x$rotation) I have, has the columns labels as chemical
> formulas (i.e: C2H3, 37Cl,...,C10H24N3O3).
> Plotting them as plain text I do the following with success:
> xlabs <- dimnames(y$rotation)[[1]]

That extracts the *row* names.

>  xlabs <- as.character(xlabs)

They are already character.  You want to convert them to expressions.
Here's one way to get you started.

> xx <- "C[2]*H[3]"
> parse(text=xx)
expression(C[2] * H[3])

It's probably easiest to plot the labels one at a time, but

xx <- c("C[2]*H[3]", "phantom()[37]*C[1]")
parse(text=paste(xx, collapse="\n"))


>  plot(y$rotation[,1],y$rotation[,2], ann=FALSE, type="p", col="red", pch=20)
>  text(y$rotation[,1],y$rotation[,2], labels=xlabs, cex=0.7, font=1, pos=4,
> offset=0.2)
>  I do not succed to plot the labels with subscripts and superscripts. I
> did many attempt trying the use of expression() or substitute() but
> without success. I can write the labels in the data matrix as needed
> (i.e.: C[2]*H[3]) to plot properly if I would know how.

> I peg your pardon if this is probably a silly problem, I am new user of R (I am
> using R 1.7.0 on win2k).

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