简体   繁体   English

R绘制带有簇的热图,但隐藏树状图

[英]R draw heatmap with clusters, but hide dendrogram

By default, R's heatmap will cluster rows and columns:默认情况下,R 的heatmap将聚集行和列:

mtscaled = as.matrix(scale(mtcars))
heatmap(mtscaled, scale='none')

在此处输入图片说明

I can disable the clustering:我可以禁用集群:

heatmap(mtscaled, Colv=NA, Rowv=NA, scale='none')

And then the dendrogram goes away:然后树状图消失了:在此处输入图片说明

But now the data is not clustered anymore.但是现在数据不再聚类了。

I don't want the dendrograms to be shown, but I still want the rows and/or columns to be clustered.我不希望显示树状图,但我仍然希望将行和/或列聚集在一起。 How can I do this?我怎样才能做到这一点?

Example of what I want:我想要的例子:在此处输入图片说明

You can do this with pheatmap :你可以用pheatmap做到这一点

mtscaled <- as.matrix(scale(mtcars))
pheatmap::pheatmap(mtscaled, treeheight_row = 0, treeheight_col = 0)

See pheatmap output here:在此处查看 pheatmap 输出:

热图输出

library(gplots)
heatmap.2(mtscaled,dendrogram='none', Rowv=TRUE, Colv=TRUE,trace='none')

Rowv -is TRUE, which implies dendrogram is computed and reordered based on row means. Rowv -is TRUE,这意味着树状图是根据行均值计算和重新排序的。

Colv - columns should be treated identically to the rows. Colv - 列应与行一样对待。

在此处输入图片说明

For ComplexHeatmap, there are function parameters to remove the dendrograms:对于 ComplexHeatmap,有删除树状图的函数参数:

library(ComplexHeatmap)
Heatmap(as.matrix(iris[,1:4]), name = "mat", show_column_dend = FALSE, show_row_dend = FALSE)

I had similar issue with pheatmap, which has better visualisation and heatmap or heatmap.2.我对 pheatmap 有类似的问题,它具有更好的可视化和热图或 heatmap.2。 Though heatmap.2 is a choice for your solution, Here is the solution with pheatmap, by extracting the order of clustered data.虽然 heatmap.2 是您的解决方案的选择,这里是 pheatmap 的解决方案,通过提取聚类数据的顺序。

library(pheatmap)
mtscaled = as.matrix(scale(mtcars))
H = pheatmap(mtscaled)

Here is the output of pheatmap这是 pheatmap 的输出

pheatmap(mtscaled[H$tree_row$order,H$tree_col$order],cluster_rows = F,cluster_cols = F)

Here is the output of pheatmap after extracting the order of clusters这是提取聚类顺序后pheatmap的输出

Do the dendrogram twice using the basic R heatmap function.使用基本的 R 热图函数做两次树状图。 Take the output of the first run, which clusters but has mandatory drawing of the dendrogram and feed it into the heatmap function again.获取第一次运行的输出,该输出聚类但强制绘制树状图,然后再次将其输入热图函数。 This time, without clustering, and without drawing the dendrogram.这一次,没有聚类,也没有绘制树状图。

#generate a random symmetrical matrix with a little bit of structure, and make a heatmap #生成一个有一点结构的随机对称矩阵,并制作一个热图

M100s<-matrix(runif(10000),nrow=100)
M100s[2,]<-runif(100,min=0.1,max=0.2)
M100s[4,]<-runif(100,min=0.1,max=0.2)
M100s[6,]<-runif(100,min=0.1,max=0.2)
M100s[99,]<-runif(100,min=0.1,max=0.2)
M100s[37,]<-runif(100,min=0.1,max=0.2)
M100s[lower.tri(M100s)] <- t(M100s)[lower.tri(M100s)]
heatmap(M100s)

#save the output #保存输出

OutputH <- heatmap(M100s)

#run it again without clustering or the dendrogram #在没有聚类或树状图的情况下再次运行它

M100c <- M100s
M100c1 <- M100c[,OutputH$rowInd]
M100c2 <- M100c1[OutputH$colInd,]
heatmap(M100c2,Rowv = NA, Colv = NA, labRow = NA, labCol = NA)

You can rely on base R structures and consider following approach based on building the hclust trees by yourself.您可以依赖基础 R 结构并考虑以下基于自己构建 hclust 树的方法。

mtscaled = as.matrix(scale(mtcars))
row_order = hclust(dist(mtscaled))$order
column_order = hclust(dist(t(mtscaled)))$order
heatmap(mtscaled[row_order,column_order], Colv=NA, Rowv=NA, scale="none")

No need to install additional junk.无需安装额外的垃圾。

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

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