Skip to content

Instantly share code, notes, and snippets.

@bmpvieira
Created April 10, 2012 20:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bmpvieira/2354104 to your computer and use it in GitHub Desktop.
Save bmpvieira/2354104 to your computer and use it in GitHub Desktop.
Short PCA example with FactoMineR and ggplot2 in R
#Copyright (c) 2012 Bruno Vieira [http://bmpvieira.com]
#License: CC BY
#Short PCA example with FactoMineR and ggplot2 in R
library(FactoMineR)
library(ggplot2)
data = read.table("data.tsv", head=T, row.names=1)
pca = PCA(data[,2:ncol(data)], scale.unit=T, graph=F)
PC1 = pca$ind$coord[,1]
PC2 = pca$ind$coord[,2]
plotdata = data.frame(cbind(group=data[,1],PC1,PC2))
plotdata$group = factor(plotdata$group)
plot = ggplot(plotdata, aes(PC1, PC2)) +
geom_text(aes(label=rownames(plotdata)), size=2.5, hjust=0.5, vjust=-0.5) +
geom_point(aes(colour=group))
print(plot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment