简体   繁体   English

外部树状图在 complexheatmap 中用于 cluster_rows 时不会保持相同的形成

[英]External dendrogram does not keep the same formation when using it for cluster_rows in complexheatmap

I am trying to create a heatmap with an external dendrogram using the ComplexHeatmap library.我正在尝试使用ComplexHeatmap库创建带有外部树状图的热图。

df <- data.frame(genes=c("G1","G2","G3","G4","G5","G6","G7","G8","G9","G10",
                         "G11","G1","G12","G4","G15","G6","G17","G8","G1","G2"),
                 rel=c("A111SD","G422ER","A112SA","B457EE","B33","N124A","F124A",
                       "G900GG","I332LP","I332LO",
                       "M332LP","A322TR","C14SA","B467ET","Z653","R124T","F334A",
                       "G901GZ","R330TP","L982LP"))
df_dist <- stringdist::stringdistmatrix(df$rel,useNames = T)
df_hclust <- hclust(df_dist)                 
plot(df_hclust)

df$Id <- seq.int(nrow(df))
df <- spread(df,genes,Id)
rownames(df) <- df$rel
df$rel<- NULL
df[!is.na(df)] <- 1
df[is.na(df)] <- 0

if (!require("BiocManager", quietly = TRUE))
  install.packages("BiocManager")

BiocManager::install("ComplexHeatmap")
library(ComplexHeatmap)

Heatmap(as.matrix(df),cluster_rows = df_hclust,cluster_columns = F)

The created heatmap has a different dendrogram from the one built using plot() .创建的热图与使用plot()构建的热图具有不同的树状图。

在此处输入图像描述 从绘图命令创建的绘图

the problem is that after all the transformations问题是,经过所有的转变

df$Id <- seq.int(nrow(df))
df <- spread(df,genes,Id)
rownames(df) <- df$rel
df$rel<- NULL
df[!is.na(df)] <- 1
df[is.na(df)] <- 0

the data.frame df has no longer the initial order. data.frame df 不再具有初始顺序。 The quick fix would be to add the following line to restore initial order:快速解决方法是添加以下行以恢复初始顺序:

df <- df[rownames(as.matrix(df_dist)),]

However, I suggest you to avoid reassigning to the same variable because it easily leads into these kinds of problems.但是,我建议您避免重新分配给同一个变量,因为它很容易导致这类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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