In R heatmap.2 I am supplying my own dendrogram and I want to annotate the columns with colors indicating the grouping of my samples.
My data is in DESeq2 objects, so I define a color mapping like this (rld is an rlog transformed DESeqDataSet)
sideCols=brewer.pal(12, "Set3")[ as.integer(colData(rld)[colnames(as.matrix(dist.rld)) ,"sample_group"]) ]
and clustering like this
dist.rld<-dist(t(assay(rld)))
then I draw the heatmap with this
heatmap.2( as.matrix(dist.rld), Rowv = as.dendrogram(hc), trace="none", scale="none", symm=T, RowSideColors=sideCols)
The problem is that the color vector for RowSideColors
needs to be in the order in which the rows will appear. How do I get that information out of the dendrogram as.dendrogram(hc)
?
I saw a comment here claiming that the order of the colors would be automatically adjusted according to the re-ordering of the rows/columns but I guess that will only happen if heatmap.2 does the clustering and not when you supply a ready-made dendrogram, right?
Thanks for your help!
================ EDIT: Problem solved ==========
Sorry, my bad - turns out that the problem was with my input data. This does actually work with the code I have posted above and the colors are ordered correctly.