Skip to content

Instantly share code, notes, and snippets.

@jnhutchinson
Last active December 18, 2015 04:08
Show Gist options
  • Save jnhutchinson/5722687 to your computer and use it in GitHub Desktop.
Save jnhutchinson/5722687 to your computer and use it in GitHub Desktop.
## colors in colorpalette should be in hexadecimal format, without alpha shading i.e. #FF0000 (red)
## my favorite palette (color blind accessible)
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#000000")
## try to make the colorpalette at least as long as the number of categories
## alpha is a numerical value from 0 to 1
PCAplot <- function(eset=NULL, categories=NULL, title=NULL, colorpalette=NULL, alpha=1){
# get metadata
pd <- pData(eset)
# adjust alpha to hexadecimal format
alpha <- sprintf("%x", ceiling(alpha*255))
# add alpha to colors
colorpalette <- paste(colorpalette, alpha, sep="")
# extract expression values
eset <- exprs(eset)
# PCA transformations
myPca <- prcomp(t(eset))
tmpPCAData <- as.data.frame(myPca$x[,1:4])
colors <- colorpalette[factor(as.character(unlist(pd[,categories])))]
legend_values=unique(cbind(colors, as.character(pd[,categories])))
pairs(tmpPCAData, bg=colors, col="#606060", cex=2, pch=21, main=title, oma=c(8,5,5,14))
legend("right", cex=0.7, col="#606060", pt.bg=legend_values[,1], pt.cex=1.5, legend=legend_values[,2], pch=21, bty="n", x.intersp=1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment