Superbest
Superbest

Reputation: 26622

R draw heatmap with clusters, but hide dendrogram

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

Upvotes: 12

Views: 39257

Answers (6)

Martin Fridrich
Martin Fridrich

Reputation: 344

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.

Upvotes: 4

Jared Roach
Jared Roach

Reputation: 1

Do the dendrogram twice using the basic R heatmap function. Take the output of the first run, which clusters but has mandatory drawing of the dendrogram and feed it into the heatmap function again. This time, without clustering, and without drawing the dendrogram.

#generate a random symmetrical matrix with a little bit of structure, and make a heatmap

M100s<-matrix(runif(10000),nrow=100)
M100s[2,]<-runif(100,min=0.1,max=0.2)
M100s[4,]<-runif(100,min=0.1,max=0.2)
M100s[6,]<-runif(100,min=0.1,max=0.2)
M100s[99,]<-runif(100,min=0.1,max=0.2)
M100s[37,]<-runif(100,min=0.1,max=0.2)
M100s[lower.tri(M100s)] <- t(M100s)[lower.tri(M100s)]
heatmap(M100s)

#save the output

OutputH <- heatmap(M100s)

#run it again without clustering or the dendrogram

M100c <- M100s
M100c1 <- M100c[,OutputH$rowInd]
M100c2 <- M100c1[OutputH$colInd,]
heatmap(M100c2,Rowv = NA, Colv = NA, labRow = NA, labCol = NA)

Upvotes: 0

cannin
cannin

Reputation: 3325

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)

Upvotes: 2

Karel Fišer
Karel Fišer

Reputation: 161

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

Upvotes: 16

Nikesh Bajaj
Nikesh Bajaj

Reputation: 79

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

Upvotes: 1

Prasanna Nandakumar
Prasanna Nandakumar

Reputation: 4335

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

Upvotes: 10

Related Questions