library(reshape2,warn.conflicts = FALSE, quietly = TRUE) library(ggplot2,warn.conflicts = FALSE, quietly = TRUE) library(plotly,warn.conflicts = FALSE, quietly = TRUE)
12/27/2017
library(reshape2,warn.conflicts = FALSE, quietly = TRUE) library(ggplot2,warn.conflicts = FALSE, quietly = TRUE) library(plotly,warn.conflicts = FALSE, quietly = TRUE)
We are going to use a simple toy data set for this example.
-Groups A and B, 10 samples each
-100 genes upregulated in each group
-Expression data already converted into z-scores
-Genes and samples already ordered as they need to be.
set.seed(1392)
upregulated_zscores <- rnorm(1000,mean=1,sd=0.1)
downregulated_zscores <- rnorm(1000,mean=-1,sd=0.1)
groupA_matrix <- matrix(c(upregulated_zscores,downregulated_zscores),
nrow=200,ncol=10,byrow=TRUE,
dimnames=list(paste0("Gene",1:200),paste0("A",1:10)))
groupB_matrix <- groupA_matrix[c(101:200,1:100),]
colnames(groupB_matrix) <- paste0("B",1:10)
gene_exp <- cbind(groupA_matrix,groupB_matrix)
Be sure gene and sample names are kept in same order as in original matrix by using the factor command on the long format data frame columns.
gene_exp_long <- melt(data.frame(Gene = rownames(gene_exp),gene_exp),
id.vars = "Gene")
colnames(gene_exp_long) <- c("Gene","Sample","Expression_z.score")
gene_exp_long$Sample <- factor(gene_exp_long$Sample,
levels = colnames(gene_exp))
gene_exp_long$Gene <- factor(gene_exp_long$Gene,
levels = rev(rownames(gene_exp)))
heatmap_exp <- ggplot(gene_exp_long,aes(Sample,Gene)) +
geom_tile(aes(fill = Expression_z.score),color="white") +
scale_fill_gradient(low = "white", high = "steelblue") +
xlab("Samples") +
ylab("Genes") +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
ggplotly(heatmap_exp)