来自 Factorminer 的 Hclust 对象不会在 Pheatmap 中聚类树状图
Hclust object from Factorminer does not cluster dendrogram in Pheatmap
交叉发布: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 对列进行聚类。有人可以解释为什么吗?
要获得正确的树状图,您需要先根据 pcahcpc.tree$labels
中给出的信息对 tmp
的列进行重新排序。
idx <- as.numeric(gsub("col_","", pcahcpc.tree$labels))
pheatmap(tmp[, idx], cellwidth=15, cellheight=12,
annotation_col = res.pcahcpc,
cluster_cols = pcahcpc.tree)
列的 pheatmap
树状图现在与 stats:::plot.hclust
给出的树状图相同
plot(pcahcpc.tree)
交叉发布: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 对列进行聚类。有人可以解释为什么吗?
要获得正确的树状图,您需要先根据 pcahcpc.tree$labels
中给出的信息对 tmp
的列进行重新排序。
idx <- as.numeric(gsub("col_","", pcahcpc.tree$labels))
pheatmap(tmp[, idx], cellwidth=15, cellheight=12,
annotation_col = res.pcahcpc,
cluster_cols = pcahcpc.tree)
列的 pheatmap
树状图现在与 stats:::plot.hclust
plot(pcahcpc.tree)