Barbara Doll
2012-Aug-15 18:32 UTC
[R] color-coding of biplot points for varimax rotated factors (from PCA)
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) -- Barbara Doll NC Sea Grant Box 8605, NC State University Raleigh NC 27695-8605 919-515-5287 919-515-7095 fax bdoll at ncsu.edu