[R] Labelling individual points on 3D PCA scatterplot

Duncan Murdoch murdoch at stats.uwo.ca
Wed Oct 28 21:08:37 CET 2009

On 10/28/2009 11:21 AM, trz wrote:
> Hi There,
> I'm attempting to plot 10 values on a three-dimensional PCA with text labels
> next to each point. While i have no trouble doing this on 2D plots using the
> 'text' or 'textxy' function, I cannot find a function to do this on a 3D
> plot.
> I am using princomp for my PCA:
>>PCA<-princomp(eucdata, cor=TRUE)
>>PCA$scores [,1:3]    # the three principal components i want to plot
> Then i am using 'scatterplot3d' to plot my first 3 principal components:
>>scatterplot3d(PCA$scores [,1:3],xlab="Component 1 (26.9%)",main="My 3D
> PCA",ylab="Component 2 
> +     (17.9%)", zlab="Component 3
> (12.4%)",type="h",box=FALSE,pch=21,bg=color)
> And i get this:
> http://www.nabble.com/file/p26096592/myPCA.jpeg 
> 'text' and 'textxy' only accept 2D coordinates so they do not label my
> points in a way that makes sense.
> I'm open to other 3D plotting functions in R, i just think this one is easy
> to visually understand. In addition, If there is any way to move the grid up
> to z=0 please let me know. To be clear i am trying to make my PCA look more
> like this one i found in a journal article (Trejaut et al., 2005):
> http://www.nabble.com/file/p26096592/journal.pbio.0030247.g003.png 

scatterplot3d will return a function that can transform 3D coordinates 
to 2D so you can plot labels in the right place; see its man page.

Alternatively, you could use the rgl package.  Using the 
example(princomp) results, the call is similar to what you had, with 
some extra work for embellishment:

plot3d(pc.cr$scores [,1:3],xlab="Component 1",main="My 3D 
PCA",ylab="Component 2", zlab="Component 3", type="h", box=F, axes=F)
spheres3d(pc.cr$scores[,1:3], radius=0.1, col="pink")
grid3d(side="z", at=list(z=0))
text3d(pc.cr$scores[,1:3], text=rownames(pc.cr$scores), adj=1.3)

This looks too crowded, but maybe you have fewer points to plot.

Duncan Murdoch

More information about the R-help mailing list