gpt4 book ai didi

r - 热图中的行顺序?

转载 作者:行者123 更新时间:2023-12-02 20:12:08 24 4
gpt4 key购买 nike

采用以下代码:

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

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

最佳答案

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

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)

关于r - 热图中的行顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5320814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com