Skip to content

Instantly share code, notes, and snippets.

@nachocab
Last active December 18, 2015 08:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nachocab/6a1dbddbe09e3805a172 to your computer and use it in GitHub Desktop.
Save nachocab/6a1dbddbe09e3805a172 to your computer and use it in GitHub Desktop.
# generate data
set.seed(107)
num_genes <- 100
num_groups <- 3
num_conditions_per_group <- 10
palette <- c(A = "#41ab5d", B = "#0e64c9", C = "#db1212")
group_names <- rep(LETTERS[1:num_groups], each=num_conditions_per_group)
data <- matrix(rnorm(num_genes * num_groups * num_conditions_per_group), nrow = num_genes)
rownames(data) <- outer(letters, letters, 'paste0')[1:num_genes]
colnames(data) <- paste0(group_names, 1:num_conditions_per_group)
data[(1:40) + 20, group_names == "B"] <- data[(1:40) + 20, group_names == "B"] + 1
# calculate PCA
pca <- prcomp(t(data), center = TRUE, scale. = TRUE)
variance <- (pca$sd^2/sum(pca$sd^2)*100)[1:2]
# calculate MDS
library(limma)
mds <- plotMDS(data)
# aux function
decimals <- function(x, n = 2){
format(round(x, n), nsmall = n, trim = TRUE)
}
# install clickme
devtools::install_github("nachocab/clickme")
library(clickme)
# plotting PCA
clickme("points", pca$x[,1], pca$x[,2],
names = colnames(data),
color_groups = group_names,
x_title = paste0("Dim 1 (", decimals(variance[1]), " % Var)"),
y_title = paste0("Dim 2 (", decimals(variance[2]), " % Var)"),
rotate_x_labels = TRUE,
padding = list(bottom=100),
title = paste0("PCA (", decimals(sum(variance)), " % Var)"),
palette = palette)
# plotting MDS
clickme("points", mds$x, mds$y,
names = colnames(data),
color_groups = group_names,
x_title = "Dim 1",
y_title = "Dim 2",
rotate_x_labels = TRUE,
padding = list(bottom=100),
title = "MDS",
palette = palette)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment