热图中的行顺序?

发布于 2024-10-22 08:54:23 字数 201 浏览 6 评论 0原文

采取以下代码:

 heatmap(data.matrix(signals),col=colors,breaks=breaks,scale="none",Colv=NA,labRow=NA)

如何提取、预计算或重新计算生成的热图中的行顺序?有没有办法将 hclust(dist(signals)) 的输出注入到热图函数中?

Take the following code:

 heatmap(data.matrix(signals),col=colors,breaks=breaks,scale="none",Colv=NA,labRow=NA)

How can I extract, pre-calculate or re-calculate the order of the rows in the heatmap produced? Is there a way to inject the output of hclust(dist(signals)) into the heatmap function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

今天小雨转甜 2024-10-29 08:54:23

感谢杰西和保罗的反馈。我编写了以下排序函数,希望对其他人有用:

data        = data.matrix(data)
distance    = dist(data)
cluster     = hclust(distance, method="ward")
dendrogram  = as.dendrogram(cluster)
Rowv        = rowMeans(data, na.rm = T)
dendrogram  = reorder(dendrogram, Rowv)

## Produce the heatmap from the calculated dendrogram.
## Don't allow it to re-order rows because we have already re-ordered them above.

reorderfun = function(d,w) { d }
png("heatmap.png", res=150, height=22,width=17,units="in")

heatmap(data,col=colors,breaks=breaks,scale="none",Colv=NA,Rowv=dendrogram,labRow=NA, reorderfun=reorderfun)

dev.off()


## Re-order the original data using the computed dendrogram
rowInd = rev(order.dendrogram(dendrogram))
di = dim(data)
nc = di[2L]
nr = di[1L]
colInd = 1L:nc
data_ordered <- data[rowInd, colInd]
write.table(data_ordered, "rows.txt",quote=F, sep="\t",row.names=T, col.names=T)

Thanks for the feedback, Jesse and Paolo. I wrote the following ordering function which will hopefully be useful to others:

data        = data.matrix(data)
distance    = dist(data)
cluster     = hclust(distance, method="ward")
dendrogram  = as.dendrogram(cluster)
Rowv        = rowMeans(data, na.rm = T)
dendrogram  = reorder(dendrogram, Rowv)

## Produce the heatmap from the calculated dendrogram.
## Don't allow it to re-order rows because we have already re-ordered them above.

reorderfun = function(d,w) { d }
png("heatmap.png", res=150, height=22,width=17,units="in")

heatmap(data,col=colors,breaks=breaks,scale="none",Colv=NA,Rowv=dendrogram,labRow=NA, reorderfun=reorderfun)

dev.off()


## Re-order the original data using the computed dendrogram
rowInd = rev(order.dendrogram(dendrogram))
di = dim(data)
nc = di[2L]
nr = di[1L]
colInd = 1L:nc
data_ordered <- data[rowInd, colInd]
write.table(data_ordered, "rows.txt",quote=F, sep="\t",row.names=T, col.names=T)
肤浅与狂妄 2024-10-29 08:54:23

有多种选择。如果运行?heatmap,您将看到可以调整的各种参数。也许最简单的方法是设置 Rowv=NA ,它应该抑制行重新排序,然后传入矩阵,其中行已经按照您想要的顺序排列。但您也可以通过 Rowvhclustfun 等手动提供聚类函数或树状图...

There are a variety of options. If you run ?heatmap you'll see the various parameters you can tweak. Maybe the easiest is to set Rowv=NA which should suppress row reordering, and then pass in the matrix with the rows already in the order you want. But you can also manually provide a clustering function, or dendrograms, via Rowv and hclustfun etc...

毁我热情 2024-10-29 08:54:23

我相信这篇文章可能有用:

How does R heatmap order rows by default ?

以下面的矩阵为例:

set.seed(321)
m = matrix(nrow=7, ncol = 7, rnorm(49))
> m
           [,1]       [,2]       [,3]        [,4]       [,5]        [,6]      [,7]
[1,]  1.7049032  0.2331354 -1.1534395 -0.10706154 -1.1203274  0.11453945 0.2503958
[2,] -0.7120386  0.3391139 -0.8046717  0.98833540 -0.4746847 -2.22626331 0.2440872
[3,] -0.2779849 -0.5519147  0.4560691 -1.07223880 -1.5304122  1.63579034 0.7997382
[4,] -0.1196490  0.3477014  0.4203326 -0.75801528  0.4157148 -0.15932072 0.3414096
[5,] -0.1239606  1.4845918  0.5775845  0.09500072  0.6341979  0.02826746 0.2587177
[6,]  0.2681838  0.1883255  0.4463561 -2.33093117  1.2308474 -1.53665329 0.9538786
[7,]  0.7268415  2.4432598  0.9172555  0.41751598 -0.1545637  0.07815779 1.1364147

您可以使用参数RowvColv覆盖行和列的顺序。您可以使用这些树状图来覆盖顺序。例如,您可以使用函数 hclust 计算顺序,然后将其作为树状图传递给 heatmap

 rhcr <- hclust(dist(m))
 chrc <- hclust(dist(t(m)))
 heatmap(m,Rowv = as.dendrogram(rhcr),
           Colv = as.dendrogram(rhcr))

 > rhcr$order
 [1] 1 3 6 2 7 4 5
 > chrc$order
 [1] 6 4 5 1 2 3 7

给出:

Hclust heatmap

默认的heatmap函数使用了一个额外的步骤,但是,通过参数reorderfun = function(d, w) reorder(d, w ),它根据行/列均值尽可能地对树状图进行重新排序。您可以通过此附加步骤重现默认顺序。因此,要获得与 heatmap 相同的顺序,您可以执行以下操作:

rddr <- reorder(as.dendrogram(rhcr),rowMeans(m))
cddr <- reorder(as.dendrogram(chcr),colMeans(m))

> as.hclust(rddr)$order
[1] 3 1 6 2 4 5 7
> as.hclust(cddr)$order
[1] 6 4 5 1 2 3 7

输出与简单的 heatmap(m) 相同:

其 sstatic.net/3EPYS.png" rel="nofollow noreferrer">默认热图

在此示例中,列恰好没有重新排序,但行却重新排序。最后,为了简单地检索顺序,您可以将热图分配给变量并获取输出。

> p <- heatmap(m)
> p$rowInd
[1] 3 1 6 2 4 5 7
> p$colInd
[1] 6 4 5 1 2 3 7

I believe this post might be useful:

How does R heatmap order rows by default?

Take the following matrix for example:

set.seed(321)
m = matrix(nrow=7, ncol = 7, rnorm(49))
> m
           [,1]       [,2]       [,3]        [,4]       [,5]        [,6]      [,7]
[1,]  1.7049032  0.2331354 -1.1534395 -0.10706154 -1.1203274  0.11453945 0.2503958
[2,] -0.7120386  0.3391139 -0.8046717  0.98833540 -0.4746847 -2.22626331 0.2440872
[3,] -0.2779849 -0.5519147  0.4560691 -1.07223880 -1.5304122  1.63579034 0.7997382
[4,] -0.1196490  0.3477014  0.4203326 -0.75801528  0.4157148 -0.15932072 0.3414096
[5,] -0.1239606  1.4845918  0.5775845  0.09500072  0.6341979  0.02826746 0.2587177
[6,]  0.2681838  0.1883255  0.4463561 -2.33093117  1.2308474 -1.53665329 0.9538786
[7,]  0.7268415  2.4432598  0.9172555  0.41751598 -0.1545637  0.07815779 1.1364147

You can override the order of the rows and columns with the parameters Rowv and Colv. You can override the order with these as dendrograms. For instance, you can calculate an order using the function hclust, then pass that to heatmap as a dendrogram:

 rhcr <- hclust(dist(m))
 chrc <- hclust(dist(t(m)))
 heatmap(m,Rowv = as.dendrogram(rhcr),
           Colv = as.dendrogram(rhcr))

 > rhcr$order
 [1] 1 3 6 2 7 4 5
 > chrc$order
 [1] 6 4 5 1 2 3 7

Gives:

Hclust heatmap

The default heatmap function uses one additional step, however, through the parameter reorderfun = function(d, w) reorder(d, w), which reorders the dendrogram as much as possible bases on row/column mean. you can reproduce the default order with this additional step. So to get the same ordering as heatmap, you can do:

rddr <- reorder(as.dendrogram(rhcr),rowMeans(m))
cddr <- reorder(as.dendrogram(chcr),colMeans(m))

> as.hclust(rddr)$order
[1] 3 1 6 2 4 5 7
> as.hclust(cddr)$order
[1] 6 4 5 1 2 3 7

Which gives the same output as simply heatmap(m):

Default heatmap

In this example the columns happen to not get reordered, but the rows do. Finally, to simply retrieve the order you can assign the heatmap to a variable and get the output.

> p <- heatmap(m)
> p$rowInd
[1] 3 1 6 2 4 5 7
> p$colInd
[1] 6 4 5 1 2 3 7
不奢求什么 2024-10-29 08:54:23

我同意杰西的观点。对于您的问题,请查看热图函数的 Rowvdistfunhclustfun 参数。
如需更多选择,请使用 gplots 包中的函数 heatmap.2Heatplus 包中的 heatmap_pluspheatmap 包中的 pheatmap 可能会有一些用处。

I agree with Jesse. For your problem take a look at the Rowv, distfun and hclustfunarguments of the heatmap function.
For more choices the functions heatmap.2 in the gplots package, heatmap_plus in the Heatplus package and pheatmap in the pheatmap package could be of some use.

(り薆情海 2024-10-29 08:54:23

pheatmap 将允许您指定用于进行聚类的方法,接受与 hclust 相同的参数。

pheatmap will allow you to specify the method that it uses to do the clustering, accepting the same arguments as hclust.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文