3

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

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

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

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

1 回答 1

4

对于“最佳叶子排序”,您可以使用库中的order方法seriationpheatmap接受clustering_callback论据。根据文档:

clustering_callback回调函数来修改集群。使用两个参数调用:原始 hclust 对象和用于聚类的矩阵。必须返回一个 hclust 对象。

所以你需要构造一个回调函数,它接受hclust对象和初始矩阵并返回优化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
         )
于 2019-02-19T09:47:27.707 回答