# [R] Labelling individual points on 3D PCA scatterplot

trz rzeszut at mcmaster.ca
Wed Oct 28 21:51:33 CET 2009

```Thank you Duncan! That is very helpful, after posting this i did figure out
how to convert the 2d coordinates into 3d. I was initially hesitant about
using rgl because i thought it looked messy. With the modifications you
suggested for the plot, it actually looks very clear now in rgl! And it
rotates :-D

Thanks again!

Tom Rzeszutek
MSc. Candidate
McMaster University

Duncan Murdoch-2 wrote:
>
> 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)
> grid3d(side="z", at=list(z=0))
>
> This looks too crowded, but maybe you have fewer points to plot.
>
> Duncan Murdoch
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

--
View this message in context: http://www.nabble.com/Labelling-individual-points-on-3D-PCA-scatterplot-tp26096592p26101737.html
Sent from the R help mailing list archive at Nabble.com.

```