Skip to content

googlegroupsformatter

bhive01 edited this page Dec 7, 2010 · 14 revisions
require(pcaMethods)
require(ggplot2)

x = c(runif(50,min=0,max=1),runif(50,min=0.5,max=3))
dim(x) = c(10,10)

PCA<- pca(x, method="nipals", scale="uv", nPcs=3, center=T, completeObs=T, cv="q2")
Q2<-Q2(PCA)
Q2
PCA@R2
#Need to find the point at which Q2 no longer increases
#bio data R2~= 0.3 and Q2 <= R2 +/- 0.2 is fine. 
PCA@scores
PCA@loadings

#loadings are matrix, need data.frame for ggplot
loadings<-as.data.frame(PCA@loadings)
#carry over names of rows
loadings$Names<-rownames(loadings)

#scores=matrix too
scores<-data.frame(PCA@scores)

#pure number scaling not very good but works... for now
#rescaling Loadings to scores
loadings[[1]]<-rescale(loadings[[1]], to = c(min(scores[[1]]),max(scores[[1]])))
loadings[[2]]<-rescale(loadings[[2]], to = c(min(scores[[2]]),max(scores[[2]])))
loadings[[3]]<-rescale(loadings[[3]], to = c(min(scores[[3]]),max(scores[[3]])))

ggplot()+
geom_point(data=scores, aes(x=PC1, y=PC2))+
geom_segment(data=loadings, aes(x=0, y=0, xend=PC1, yend=PC2)
, arrow=arrow(length=unit(0.2,"cm")), alpha=0.25)+
geom_text(data=loadings, aes(x=PC1, y=PC2, label=Names), alpha=0.5, size=3)+
scale_colour_discrete("Variety")+
scale_x_continuous("Principal Component 1")+
scale_y_continuous("Principal Component 2")+
theme_bw()

Note: The ggplot2 wiki is no longer maintained, please use the ggplot2 website instead!

Clone this wiki locally