[R] Color-coded Biplot of Varimax rotated factors from PCA based factor analysis

PIKAL Petr petr.pikal at precheza.cz
Thu Aug 16 15:12:44 CEST 2012


Hi

Without some data it is difficult to say where is difference. However from code below it seems to me that it is an adaptation from my attempt to produce colour coded points on biplot.

http://tolstoy.newcastle.edu.au/R/e4/help/08/08/20012.html

fit<-princomp(iris[,1:4], cor=T) 
biplot(fit, xlabs=rep("", nrow(iris))) 
rrr<-apply(fit$scores[,1:2],2, range) 
par(usr=as.vector(rrr)) 
points(fit$scores[,1:2], col=rainbow(4)[(as.numeric(iris[,5]))], pch=20)

This results in presenting points coloured according to some rules but the points are slightly shifted from those made by biplot itself. So AFAIK you either can use biplot and no colouring or use the code to present faked coloured points slightly shifted.

Compare with just

biplot(fit)
points(fit$scores[,1:2], col=rainbow(4)[(as.numeric(iris[,5]))], pch=20)

But maybe something changed from 2008 and now biplot with coloured point is available somewhere.

Regards
Petr



> 
> I'm using R for PCA and? factor analysis. I want to create biplots of
> varimax rotated factors that color-code points by their classification.
> My research is on streams that are urban and rural. So, I want to color
> code them by this classification. If you just do a biplot from prcomp
> or princomp, you cannot add this color. So, I have used some code
> developed by a graduate student in our statistics department here at NC
> State University. However, when you compare the two biplots, the
> observed points are not in the same location. The variable vectors
> match up, but not the points. I'm not sure why. The code is below.
> Please help.
> 
> 
> label=data[,"Urban.Rural"]
> indexU<-which(label=="U")
> indexR<-which(label=="R")
> 
> collab<-rep(0,length(data[,1]))
> collab[indexU]<-"Blue"
> collab[indexR]<-"Green"
> 
> library(psych)
> 
> fit <- principal(mydata, nfactors = num.fac, rotate="varimax", scores =
> TRUE)
> 
> z1 <- sum(fit2$loadings[,1]^2)  ### need to scale scores and loadings
> by these factors
> z2 <- sum(fit2$loadings[,2]^2)
> 
> biplot(fit$scores[,c(1,2)]/c(z1, z2), loadings(fit)[,c(1,2)]*c(z1, z2),
> xlabs=rep("", length(collab)),  col = c("black", "orange"))
> legend(x="bottomright", legend=c("Urban","Rural"),
> text.col=c("Blue","Green"), bg="Grey90")
> 
> ### scale the plot parameters
> rrr<-apply(fit$scores[,1:2],2, range)
> (abs(rrr)+.1)*sign(rrr)
> par(usr=as.vector(rrr))
> 
> ### now include the colored points
> points(fit$scores[,c(1,2)], col=collab, pch=20)
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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