【问题标题】:R - heatmap.2: reorder rows and columnsR - heatmap.2:重新排序行和列
【发布时间】:2017-08-15 19:23:08
【问题描述】:

我使用 gplots 包的 heatmap.2 生成了一张热图:

library(gplots)
abc <-read.csv(file="abc.txt", header=T, sep="\t", dec=".")
abcm<-as.matrix(abc)
def <-read.csv(file="def.txt", header=T, sep="\t", dec=".")
defm<-as.matrix(def)
mean <-read.csv(file="mean.txt", header=T, sep="\t", dec=".")
meanm<-as.matrix(mean)
distance.row = dist(as.matrix(def), method = "euclidean")
cluster.row = hclust(distance.row, method = "average")
distance.col = dist(t(as.matrix(abc)), method = "euclidean")
cluster.col = hclust(distance.col, method = "average")
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)

heatmap.2(meanm, trace="none", dendrogram="both", Rowv=as.dendrogram(cluster.row), Colv=as.dendrogram(cluster.col), margins = c(7,7), col=my_palette)

为每个树状图使用两种不同的聚类方法。

现在我想重新排列对象,使绿色方块形成对角线。我该怎么做?

编辑:我的示例输入在这里

structure(c(1, 0.6798, 0.6604, 0.7101, 0.6771, 0.6725, 0.6696, 
0.6548, 0.676, 0.6811, 0.6798, 1, 0.656, 0.6763, 0.8163, 0.781, 
0.7811, 0.6503, 0.7811, 0.855, 0.6604, 0.656, 1, 0.6532, 0.6498, 
0.6459, 0.6455, 0.7532, 0.6521, 0.6536, 0.7101, 0.6763, 0.6532, 
1, 0.672, 0.669, 0.6669, 0.6517, 0.6748, 0.6786, 0.6771, 0.8163, 
0.6498, 0.672, 1, 0.7828, 0.7838, 0.6441, 0.7736, 0.8227, 0.6725, 
0.781, 0.6459, 0.669, 0.7828, 1, 0.8361, 0.6447, 0.7574, 0.7796, 
0.6696, 0.7811, 0.6455, 0.6669, 0.7838, 0.8361, 1, 0.638, 0.7566, 
0.7772, 0.6548, 0.6503, 0.7532, 0.6517, 0.6441, 0.6447, 0.638, 
1, 0.6563, 0.6459, 0.676, 0.7811, 0.6521, 0.6748, 0.7736, 0.7574, 
0.7566, 0.6563, 1, 0.7778, 0.6811, 0.855, 0.6536, 0.6786, 0.8227, 
0.7796, 0.7772, 0.6459, 0.7778, 1), .Dim = c(10L, 10L), .Dimnames = list(
    c("sp1", "sp2", "sp3", "sp4", "sp5", "sp6", "sp7", "sp8", 
    "sp9", "sp10"), c("sp1", "sp2", "sp3", "sp4", "sp5", "sp6", 
    "sp7", "sp8", "sp9", "sp10")))

【问题讨论】:

  • 你能包含样本数据吗?最简单的方法可能是将dput() 与您的数据子集一起使用。
  • @LukeC 查看我的编辑!
  • 谢谢@rororo-我认为为了完整性,您需要包含cluster.rowcluster.col,以便用户可以像您所做的那样实际绘制热图。但是,我可能会误解你——你想重新排列你的行和列,以便得到对角线。那不会破坏你的树状图吗?对于 sp 4、1、8 和 3,它不一定重要(只需翻转那个分支),但对于 sp 9、6 和 7,我看不到允许对角线的树状图配置。有关详细信息,请参阅 heatmap.2RowvColv 参数。如果我不理解你,我很抱歉!
  • 哦,我完全忽略了这一点! sp9 的聚类方式不同:/ 但是,我将如何翻转分支,以便至少 大多数 项对齐?
  • 也许可以试试reorder.dendrogram- 查看该功能帮助中的示例,它看起来可能满足您的需求。

标签: r heatmap gplots


【解决方案1】:

函数reorder.dendrogram 翻转分支。

reorder(as.dendrogram(cluster.col), 10:1)
reorder(as.dendrogram(cluster.row), 10:1)

用于

heatmap.2(meanm, trace="none", dendrogram="both",
Rowv=reorder(as.dendrogram(cluster.row), 10:1),
Colv=reorder(as.dendrogram(cluster.col), 10:1),
margins = c(7,7), breaks=breaks,col=hm.colors, na.color="white",
main="mean(AAI+ANI)", xlab="AAI clustering", ylab="ANI clustering", srtCol=45)

【讨论】:

    猜你喜欢
    • 2017-05-27
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多