Skip to content

Instantly share code, notes, and snippets.

@cplaisier
Created June 17, 2020 05:13
Show Gist options
  • Select an option

  • Save cplaisier/e27cad50ca04aa7ff7e4baefb54d451d to your computer and use it in GitHub Desktop.

Select an option

Save cplaisier/e27cad50ca04aa7ff7e4baefb54d451d to your computer and use it in GitHub Desktop.
PlotPCA For Anything - might need some tweaking
plotPCA_v2 <- function(x, intgroup = "condition", ntop = 500, col)
{
rv = rowVars(x)
select = order(rv, decreasing = TRUE)[seq_len(min(ntop, length(rv)))]
pca = prcomp(t(x[select, ]))
fac = factor(apply(as.data.frame(colData(x)[, intgroup, drop = FALSE]),
1, paste, collapse = " : "))
if (missing(col)) {
col = if (nlevels(fac) >= 3)
brewer.pal(nlevels(fac), "Paired")
else c("lightgreen", "dodgerblue")
}
imp = summary(pca)$importance[2,]
par(mar=c(1,1,1,1))
xyplot(PC2 ~ PC1, groups = fac, data = as.data.frame(pca$x),
pch = 16, cex = 2, aspect = "iso", col = col, main = draw.key(key = list(rect = list(col = col),
text = list(levels(fac)), rep = FALSE)), xlab = paste('PC1 (',signif(imp[[1]],2)*100,'%)',sep=''), ylab = paste('PC2 (',signif(imp[[2]],2)*100,'%)',sep=''))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment