【发布时间】:2020-07-18 18:41:42
【问题描述】:
交叉发布:https://www.biostars.org/p/450365/
我正在尝试使用pheatmap::pheatmap 创建一个包含 20 行和 10 列的矩阵的热图。为了对列进行聚类,我使用了在输入矩阵上运行 FactoMineR::HCPC 后获得的 hclust 对象。但是,当我使用 hclust 对象时,
这是我的代码:
library(tidyverse)
library(pheatmap)
library(FactoMineR)
# reproducible df of 20 rows and 10 columns
set.seed(100)
tmp <- matrix(rnorm(10000), nrow = 20, ncol = 10)
tmp <- as.data.frame(tmp)
colnames(tmp) <- paste0('col_', seq(1:ncol(tmp)))
rownames(tmp) <- paste0('row_', seq(1:nrow(tmp)))
# use FactoMineR HCPC for clustering data
res.pcahcpc <- FactoMineR::PCA(X = t(tmp), graph = F)
res.pcahcpc <- FactoMineR::HCPC(res.pcahcpc, nb.clust = 4, graph = F)
# get hclust object from FactoMineR::HCPC
pcahcpc.tree <- res.pcahcpc$call$t$tree
# hclust object
> pcahcpc.tree
Call:
flashClust::hclust(d = dissi, method = method, members = weight)
Cluster method : ward
Distance : euclidean
Number of objects: 10
# get cluster information for heatmap annotation
res.pcahcpc <- res.pcahcpc$data.clust # clusters
colnames(res.pcahcpc)[ncol(res.pcahcpc)] <- "PCA_HCPC"
res.pcahcpc <- res.pcahcpc[,'PCA_HCPC', drop = F]
# clusters
> head(res.pcahcpc)
PCA_HCPC
col_1 1
col_2 1
col_3 4
col_4 2
col_5 3
col_6 1
# create heatmap using PCA HCPC clustering
tmp %>%
pheatmap(cellwidth = 15, cellheight = 15,
annotation_col = res.pcahcpc,
cluster_cols = pcahcpc.tree)
运行上面的代码给了我下面的热图,这很奇怪,因为它根本没有通过 FactorMiner PCA HCPC 对列进行聚类。有人能解释一下为什么吗?
【问题讨论】:
-
图顶部的树状图似乎聚集了列,我错了吗?
-
@carlo_sguera 如果您查看相应的 PCA_HCPC 注释,它看起来不像根据 PCA_HCPC 树进行聚类。如果是这样,它应该显示组合在一起的颜色。
-
现在我明白你的意思了。在
res.pcahcpc <- FactoMineR::HCPC(res.pcahcpc, nb.clust = 4, graph = F)之后,plot(res.pcahcpc, choice = "tree")就可以了。但是,它与pheatmap的交互很奇怪,你是对的。我对pheatmap不熟悉,希望你能找到解决办法,祝你好运!