Skip to content

Instantly share code, notes, and snippets.

@jasper1918
Last active March 14, 2017 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasper1918/cc5551bb03c69c37c41536d8fa7fe978 to your computer and use it in GitHub Desktop.
Save jasper1918/cc5551bb03c69c37c41536d8fa7fe978 to your computer and use it in GitHub Desktop.
mypca
mypca<-function(x, mytitle="", ...)
{
par(mfrow=c(1,1))
this_pc <- prcomp(x, scale=FALSE)
this_pc_coord <- this_pc$x
pc1pct=round(summary(this_pc)$importance[2,1]*100,2)
pc2pct=round(summary(this_pc)$importance[2,2]*100,2)
par(mar = c(8, 5, 3, 8), xpd=T)
plot(this_pc_coord[,1], this_pc_coord[,2], pch = 19, cex=2,
xlab=paste0("PC1(",pc1pct,"%)"),ylab=paste0("PC2(",pc2pct,"%)"), bty="l", xpd=T,
main=mytitle)
abline(h=0, v=0, lty = 2, xpd=F, lwd=.1, col="gray70")
text(this_pc_coord[,1], this_pc_coord[,2], labels=rownames(this_pc_coord),
cex=1, pos = 4)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment