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 plotThen i am using 'scatterplot3d' to plot my first 3 principal components:>scatterplot3d(PCA$scores [,1:3],xlab="Component 1 (26.9%)",main="My 3DPCA",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 Thanks in advance! Tom -- View this message in context: http://www.nabble.com/Labelling-individual-points-on-3D-PCA-scatterplot-tp26096592p26096592.html Sent from the R help mailing list archive at Nabble.com.
Duncan Murdoch
2009-Oct-28 20:08 UTC
[R] Labelling individual points on 3D PCA scatterplot
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.pngscatterplot3d 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
Hello?I am a Chinese student, and I got the same problem. I have learnd R for several weeks, I want to draw a PCA picture. I have data in excel format like this: ID d1 d2 d3 d4 A 1 2 3 4 B 1 2 3 4 C 1 2 3 4 D 1 2 3 4 E 1 2 3 4 How to load these data,and draw the PCA picture? -- View this message in context: http://r.789695.n4.nabble.com/Labelling-individual-points-on-3D-PCA-scatterplot-tp818431p4646914.html Sent from the R help mailing list archive at Nabble.com.