I want to had a custom dendrogram to an heatmap. I know I can add like this:
dend = as.dendrogram(hclust(dist(gene_matrix), method = 'ward.D'))
dend = color_branches(dend, k = 4)
Heatmap(gene_matrix, name = "Altered",top_annotation= colAnn, cluster_columns = dend,
column_dend_reorder = FALSE)
However, even when I had column_dend_reorder, it automatically reorders the columns that are already ordered in the dendrogram.
When I remove the cluster_columns=dend it does not reorder the columns and create a dendrogram, yet the dendrogram created is not with the same method that I want and it not colored.
So want to remove the dendrogram from the heatmap, and add 'dend' as a top annotation.
How can I do that?
Thanks!
2 answers
I don't think that's possible. A dendrogram is not a per-column annotation, it's a representation of a hierarchically clustered distance matrix generated using all columns. You can access the dendrogram using Heatmap Decoration functions after drawing the heatmap, but you can't add a dendrogram as an annotation.
I think this is an XY problem - why do you need to access the dendrogram as an annotation? Do you need to reorder other column annotations / move the dendrogram?
Log in to answer this question.