简体   繁体   中英

How to flip hierarchical clustering nodes in pheatmap

I have the following code:

pheatmap::pheatmap(mtcars, clustering_method = "ward.D2")

Which produces the plot like below.

在此处输入图像描述

What I'd like to do is to do these:

  1. Move the mpg and qsec node to after wt .
  2. Move disp and hp node to the leftmost.

Is that a valid thing to do? If so how can I achieve that?

Update

Following Marco Sandri suggestion below and with a little bit of tweek. I came up with this code that does the job:

phtmap <- pheatmap::pheatmap(mtcars)
col_dend <- phtmap[[2]]
col_dend <- dendextend::rotate(col_dend, order = c("hp", "disp",   "vs", "am", "cyl", "drat", "gear", "carb","wt", "mpg","qsec") )
pheatmap(mtcars, cluster_cols=as.hclust(col_dend))

Maybe this is what you are looking for.

library(pheatmap)
library(seriation)
library(dendextend)

phtmap <- pheatmap(mtcars)
col_dend <- phtmap[[2]]
col_dend <- rotate(col_dend, order = rev(names(mtcars)[get_order(col_dend)]))

# The pheatmap with the same clustering of heatmaply
pheatmap(mtcars, cluster_cols=as.hclust(col_dend))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM