Pheatmap:重新排序树状图中的叶子

Pheatmap: Re-order leaves in dendogram

我使用 pheatmap 包基于层次聚类创建了一个带有相应树状图的热图。现在,我想更改树状图中叶子的顺序。最好使用 optimal leaves 方法。我四处搜索但没有找到任何关于如何改变实现这一目标的解决方案。

对于如何使用最佳叶子方法更改叶子顺序的建议,我将不胜感激。

这是我的随机数据示例代码:

mat <- matrix(rgamma(1000, shape = 1) * 5, ncol = 50)
p <- pheatmap(mat, 
         clustering_distance_cols = "manhattan",
         cluster_cols=TRUE,
         cluster_rows=FALSE
         )

对于 "optimal leaf ordering",您可以使用 seriation 库中的 order 方法。 pheatmap 接受 clustering_callback 参数。根据文档:

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.

所以你需要构造接受hclust对象和初始矩阵和returns优化hclust对象的回调函数。

这是一个代码:

library(pheatmap)
library(seriation)

cl_cb <- function(hcl, mat){
    # Recalculate manhattan distances for reorder method
    dists <- dist(mat, method = "manhattan")

    # Perform reordering according to OLO method
    hclust_olo <- reorder(hcl, dists)
    return(hclust_olo)
}

mat <- matrix(rgamma(1000, shape = 1) * 5, ncol = 50)
p <- pheatmap(mat, 
         clustering_distance_cols = "manhattan",
         cluster_cols=TRUE,
         cluster_rows=FALSE,
         clustering_callback = cl_cb
         )