Skip to content

Instantly share code, notes, and snippets.

@yannickwurm
Created July 14, 2015 15:47
Show Gist options
  • Save yannickwurm/13e1f2df535b1ff10cb4 to your computer and use it in GitHub Desktop.
Save yannickwurm/13e1f2df535b1ff10cb4 to your computer and use it in GitHub Desktop.
library(ggplot2)
plotPCs <- function(data=pcaOnIndividualQueenSamples, pcs=c(1,2), outputToScreen=TRUE) {
print(paste("running on:", deparse(substitute(pcaOnIndividualQueenSamples))))
library(ggplot2)
if (length(pcs) != 2) {
warning('must know which PCs to plot - give me 2!')
}
# if (outputToScreen) {
# yw$createDisplayDeviceIfNoneExists()
# }
## determine % variance
variance <- (data$sdev)^2
percentageExplainedVariance <- 100 * round(variance/sum(variance), 5)
##plot
p <- qplot( x=data$x[,pcs[1]]
, xlab=paste("pc:", pcs[1], ' % variance:', percentageExplainedVariance[ pcs[1]])
, y=data$x[,pcs[2]]
, ylab=paste("pc:", pcs[2], ' % variance:', percentageExplainedVariance[ pcs[2]])
, label = data$colony
, face = "bold"
, size = 3
, alpha=0.95
, geom = 'text'
, colour= data$category
)
p +opts(legend.position="none") + scale_colour_brewer(palette="Set1")
}
pca$x <- pca$loadings #pca$pcaResult@loadings
pca$category <- gsub(pattern = ".*shifted" , replacement="shiftedtoP", x = rownames(pca$x))
pca$category <- gsub(pattern = "_.*" , replacement="", x = pca$category)
pca$colony <- gsub(pattern = "(P|M)_([0-9]*)_.*" , replacement="\\1\\2", x = rownames(pca$x))
plotPCs(data=pca, pcs=1:2) #2:3
plotPCs(data=pca, pcs=2:3) #2:3 PC2
plotPCs(data=pca, pcs=3:4) #2:3
plotPCs(data=pca, pcs=c(2,7)) # PC7 also stands out...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment