using hclust object from FactoMineR::HCPC to force clustering in pheatmap
2
0
Entering edit mode
4.4 years ago
komal.rathi ★ 4.1k

Cross posted: https://stackoverflow.com/questions/62972420/hclust-object-from-factorminer-does-not-cluster-dendrogram-in-pheatmap

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

Below is my reproducible 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 and as you can see, it is not clustering the columns by FactoMineR PCA HCPC at all (the top annotation colors should've been grouped). How do I use hclust object from FactoMineR::HCPC to force clustering in pheatmap?

Heatmap

RNA-Seq factorminer PCA HCPC • 4.5k views
ADD COMMENT
3
Entering edit mode
4.4 years ago
komal.rathi ★ 4.1k

Someone posted an answer to this on stackoverflow which I am sharing here: https://stackoverflow.com/a/63017471/1844024

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

enter image description here

ADD COMMENT
0
Entering edit mode
4.4 years ago

You need to pass a valid hclust object to cluster_cols and I don't think FactoMineR produces one. Try using as.hclust() or use another heatmap function with a more convenient interface.

ADD COMMENT
0
Entering edit mode

The pcahcpc.tree seems to be a valid hclust object:

> class(pcahcpc.tree)
[1] "hclust"
ADD REPLY

Login before adding your answer.

Traffic: 2073 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6