[R] using [i] to plot & label all vector elements

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jul 2 18:19:06 CEST 2003

On Wed, 2 Jul 2003, Robert Schick wrote:

> I'm using 1.6.2 on a win 2k box. (I know I'm due for an upgrade.)
> I've used brute force to label a series of vectors, and I'm wondering if
> there's a better way to do this. I wanted to create a biplot of the 2nd
> & 3rd PCs, and did this:

You seem to be using the 1st and 2nd.  What's wrong with biplot()'s 
princomp method (which can do any pair)?

> test <- edit(loadings(cv.prc.spr))
> test.1 <- test[,1]
> test.2 <- test[,2]
> test.3 <- test[,3]
> x0 <- 0
> y0 <- 0
> i <- order(test.1)
> x <- test.1[i]
> y <- test.2[i]
> z <- test.3[i]
> # and added the arrows to the pc plot with the following brute force
> labeling.
> arrows(x0, y0, x[i],y[i], length=0.1, lty=3)
> text(x[i],y[i],labels=c("srnv", "ann.precip", "area.gt500", "modc" ,
> "cscd","nwca", "range.ann.T", "mean.ann.T", "min.ann.T", "sedi", "gran",
> "aluv", "cwca1", "elev", "volc", "gcv", "max.ann.T", "peak.flow.mnth"),
> cex=.75)
> When I tried using labels=rownames(x[i]) I get NULL. Three questions: 
> 1. Why is that statement NULL?

x[i] is a single vector, not a matrix.  It is matrices which have 
rownames.  names() would have worked.

> 2. Is there a way to plot the labels without having to hard code them as
> above..


> 3. Even if I hard code them, there's something that seems to change in
> the order of that that is eluding me. I suspect it's a combination of
> [i] and order, but I don't understand it. Advice?

We can't see that from the details you have given.

Try working with the matrix directly:

test2 <- test[sort.list(test[, 1], ]
arrows(0, 0, test2[, 1], test2[, 2], length=0.1, lty=3)
text(test2[, 1:2], labels=rownames(test2), cex=0.75-

However, in this case there is no point in permuting the rows, so what 
exactly did you intend?

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