Hello, I am recently starting to use pheatmap since it can draw more decent heatmap (personal opinion) in comparison with heatmap.2 or heatplot.. however, I want to add a color side bar (on top of the heatmap) as I did in heatmap.2 with different color assigned to each group of sample and re-ordered by the clustering in heatmap. I know in heatmap.2 I can use "ColSideColors", but in pheatmap I could use "annotation"; but I didn't figure out how to get it work, please help!! many thanks!!
the data format like this, and I want the genes under 4 different conditions assigned the same color but re-ordered in heatmap by clustering:
data format: https://www.dropbox.com/s/qc8zjpgcimo71mu/sample.jpg
1 answer
Use annotation_col, as exemplified here: A: DESeq2 pheatmap returns Error in check.length("fill") : 'gpar' element 'fill' mu
The rownames of your object passed to annotation_col should be the same as the colnames of your object that is being clustered.
A reproducible example:
#Create random data
data <- replicate(20, rnorm(50))
rownames(data) <- paste("Gene", c(1:nrow(data)))
colnames(data) <- paste("Sample", c(1:ncol(data)))
metadata <- data.frame(
c(rep("case", ncol(data)/2), rep("control", ncol(data)/2)),
c(rep("cond1", ncol(data)/4), rep("cond2", ncol(data)/4), rep("cond3", ncol(data)/4), rep("cond4", ncol(data)/4)),
row.names=colnames(data))
colnames(metadata) <- c("casecontrol","condition")
metadata
casecontrol condition
Sample 1 case cond1
Sample 2 case cond1
Sample 3 case cond1
Sample 4 case cond1
Sample 5 case cond1
Sample 6 case cond2
Sample 7 case cond2
Sample 8 case cond2
Sample 9 case cond2
Sample 10 case cond2
Sample 11 control cond3
Sample 12 control cond3
Sample 13 control cond3
Sample 14 control cond3
Sample 15 control cond3
Sample 16 control cond4
Sample 17 control cond4
Sample 18 control cond4
Sample 19 control cond4
Sample 20 control cond4
out <- pheatmap(data,
show_rownames=T, cluster_cols=T, cluster_rows=T, scale="row",
cex=1, clustering_distance_rows="euclidean", cex=1,
clustering_distance_cols="euclidean", clustering_method="complete", border_color=FALSE,
annotation_col=metadata)
Kevin
Log in to answer this question.

I do this, but it's not pretty. I use layout() and image() to make my own image plots.
thanks! I will try it!!
This post might be relevant: How Do I Draw A Heatmap In R With Both A Color Key And Multiple Color Side Bars?