[R] Color of points in a 3d plot of a PCA

Duncan Murdoch murdoch at stats.uwo.ca
Fri Apr 17 16:54:44 CEST 2009


On 4/17/2009 10:43 AM, Alejandro González wrote:
> Dear all,
> I'm trying to perform a principal components analysis of a sample of 
> individuals, and to plot it in 3D, assigning different colors according 
> to the population each individual belongs to. Given that the matrix I 
> have to use for the PCA cannot contain cualitative variables (here, the 
> population of origin), I have no idea how can I do this in R. ANy help?
> Thanks in advance

As long as the order of rows in the matrix is unchanged when you 
calculate the pc scores, you should be able to use a separate dataframe 
or vector to give information about each row, and to work out a colour 
for plotting it.

For example:


X <- matrix(rnorm(1000), 100, 10)
# Make up a fake population based on the first column
popn <- round(X[,1])
colour <- popn - min(popn) + 1
pc <- princomp(X)
library(rgl)
plot3d(pc$scores[,1:3], col=colour)

Duncan Murdoch

> Alejandro González
> Departamento de Biodiversidad y Conservación
> Real Jardín Botánico
> Consejo Superior de Investigaciones Científicas
> 
> Claudio Moyano, 1
> 28014 Madrid, Spain
> 
> Tel +0034 914203017
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list