3

I am plotting a set of 15 samples clustered in three groups A, B, C, and the heatmap orders them such as C, A, B. (I have read this is due to that it plots on the right the cluster with the strongest similarity). I would like to order the clusters so the leaves of the cluster are seen as A, B, C (therefore reorganising the order of the cluster branches. Is there a function that can help me do this?

The code I have used:

library(pheatmap)

pheatmap(mat, annotation_col = anno, 
    color = colorRampPalette(c("blue", "white", "red"))(50), show_rownames = F)

Image output from code below

(cluster_cols=FALSE would not cluster the samples at all, but that is not what I want)

I have also found on another forum this, but I am unsure how to change the function code and if it would work for me:

clustering_callback callback function to modify the clustering. Is called with two parameters: original hclust object and the matrix used for clustering. Must return a hclust object.

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
Ecg
  • 908
  • 1
  • 10
  • 28

3 Answers3

2

Hi I am not sure if that is of any help for you but when you check?pheatmap and scroll down to examples the last snippet of code actually does give that example.

# Modify ordering of the clusters using clustering callback option
callback = function(hc, mat){
    sv = svd(t(mat))$v[,1]
    dend = reorder(as.dendrogram(hc), wts = sv)
    as.hclust(dend)
}

pheatmap(test, clustering_callback = callback)

I tried it on my heatmap and the previously defined function actually sorted the clusters exactly the way I needed them. Although I have to admit (as I am new to R) I don't fully understand what the defined callback function does.

Maybe you can also write a function with the dendsortpackage as I know you can reorder the branches of a dendrogram with it.

han5000
  • 99
  • 7
  • Hi @han50000 the order of the columns would be as sample number, not as dendrogram, is there a function for that? as.numeric? I have no experience writing functions Thanks – Ecg Apr 08 '20 at 07:51
2

In this case, luckily clustering of the columns coincides with sample number order, (which is similar to dendrogram) so I added cluster_cols = FALSE and solved the issue of re-clustering the columns (and avoided writing the callback function.

pheatmap(mat, 
         annotation_col = anno, 
         fontsize_row = 2, 
         show_rownames = T, 
         cutree_rows = 3, 
         cluster_cols = FALSE)
Ecg
  • 908
  • 1
  • 10
  • 28
1
# install.packages("dendsort")
library(dendsort)

sort_hclust <- function(...) as.hclust(dendsort(as.dendrogram(...)))

cluster_cols=sort_hclust(hclust(dist(mat)))
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
keryruo
  • 741
  • 5
  • 4
  • Hi @keryruo the order of the columns would be as sample number, not as dendrogram, is there a function for that? Thanks – Ecg Apr 08 '20 at 07:36
  • 1
    Hi Ecg, you want to extract sample names the same order as it in column of pheatmap? out <- pheatmap(mat,cluster_cols=T); colnames(mat[,out$tree_col[["order"]]]) – keryruo Nov 16 '20 at 06:54