R draw heatmap with clusters, but hide dendrogram

26,637

Solution 1

library(gplots)
heatmap.2(mtscaled,dendrogram='none', Rowv=TRUE, Colv=TRUE,trace='none')

Rowv -is TRUE, which implies dendrogram is computed and reordered based on row means.

Colv - columns should be treated identically to the rows.

enter image description here

Solution 2

You can do this with pheatmap:

mtscaled <- as.matrix(scale(mtcars))
pheatmap::pheatmap(mtscaled, treeheight_row = 0, treeheight_col = 0)

See pheatmap output here:

pheatmap output

Solution 3

For ComplexHeatmap, there are function parameters to remove the dendrograms:

library(ComplexHeatmap)
Heatmap(as.matrix(iris[,1:4]), name = "mat", show_column_dend = FALSE, show_row_dend = FALSE)

Solution 4

You can rely on base R structures and consider following approach based on building the hclust trees by yourself.

mtscaled = as.matrix(scale(mtcars))
row_order = hclust(dist(mtscaled))$order
column_order = hclust(dist(t(mtscaled)))$order
heatmap(mtscaled[row_order,column_order], Colv=NA, Rowv=NA, scale="none")

No need to install additional junk.

Solution 5

I had similar issue with pheatmap, which has better visualisation and heatmap or heatmap.2. Though heatmap.2 is a choice for your solution, Here is the solution with pheatmap, by extracting the order of clustered data.

library(pheatmap)
mtscaled = as.matrix(scale(mtcars))
H = pheatmap(mtscaled)

Here is the output of pheatmap

pheatmap(mtscaled[H$tree_row$order,H$tree_col$order],cluster_rows = F,cluster_cols = F)

Here is the output of pheatmap after extracting the order of clusters

Share:
26,637
Superbest
Author by

Superbest

Just another spaghetti chef.

Updated on July 12, 2022

Comments

  • Superbest
    Superbest almost 2 years

    By default, R's heatmap will cluster rows and columns:

    mtscaled = as.matrix(scale(mtcars))
    heatmap(mtscaled, scale='none')
    

    enter image description here

    I can disable the clustering:

    heatmap(mtscaled, Colv=NA, Rowv=NA, scale='none')
    

    And then the dendrogram goes away:enter image description here

    But now the data is not clustered anymore.

    I don't want the dendrograms to be shown, but I still want the rows and/or columns to be clustered. How can I do this?

    Example of what I want:enter image description here