Cross posted: https://www.biostars.org/p/450365/
I am trying to create a heatmap of a matrix with 20 rows and 10 columns using pheatmap::pheatmap
. To cluster the columns, I am using an hclust
object obtained after running FactoMineR::HCPC
on the input matrix. However, when I use the hclust object, the
This is my code:
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)
Running the above code gives me the heatmap below which is weird because it is not clustering the columns by FactorMiner PCA HCPC at all. Can someone explain why so?
To get the correct dendrogram, you need first to reorder the columns of tmp
according to the information given in pcahcpc.tree$labels
.
idx <- as.numeric(gsub("col_","", pcahcpc.tree$labels))
pheatmap(tmp[, idx], cellwidth=15, cellheight=12,
annotation_col = res.pcahcpc,
cluster_cols = pcahcpc.tree)
The pheatmap
dendogram for columns now is the same as the dendrogram given by stats:::plot.hclust
plot(pcahcpc.tree)