This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Is it possible to add a dendrogram as a top annotation using ComplexHeatmap ?

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!

dendrogram r heatmap complexheatmap

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?

this chapter of the ComplexHeatmap reference makes it look like you can add a custom dendrogram so long as you have an hclust object. The key looks to be using cluster_columns = as.dendrogram(hclust_obj)

Log in to answer this question.