【问题标题】:HeatMap: how to cluster only the rows and keep order of the heatmap's column labels as same as in the df?热图:如何仅对行进行聚类并保持热图列标签的顺序与 df 中的相同?
【发布时间】:2018-12-09 19:35:09
【问题描述】:

我想绘制一个热图并只对行进行聚类(即这个 tydf1 中的基因)。 另外,想要保持热图的列标签的顺序与 df 中的相同(即 tydf1)?

样本数据

df1 <- structure(list(Gene = c("AA", "PQ", "XY", "UBQ"), X_T0_R1 = c(1.46559502, 0.220140568, 0.304127515, 1.098842127), X_T0_R2 = c(1.087642983, 0.237500819, 0.319844338, 1.256624804), X_T0_R3 = c(1.424945196, 0.21066267, 0.256496284, 1.467120048), X_T1_R1 = c(1.289943948, 0.207778662, 0.277942721, 1.238400358), X_T1_R2 = c(1.376535013, 0.488774258, 0.362562315, 0.671502431), X_T1_R3 = c(1.833390311, 0.182798731, 0.332856558, 1.448757569), X_T2_R1 = c(1.450753714, 0.247576125, 0.274415259, 1.035410946), X_T2_R2 = c(1.3094609, 0.390028842, 0.352460646, 0.946426593), X_T2_R3 = c(0.5953716, 1.007079177, 1.912258811, 0.827119776), X_T3_R1 = c(0.7906009, 0.730242116, 1.235644748, 0.832287694), X_T3_R2 = c(1.215333041, 1.012914813, 1.086362205, 1.00918082), X_T3_R3 = c(1.069312467, 0.780421013, 1.002313082, 1.031761442), Y_T0_R1 = c(0.053317766, 3.316414959, 3.617213894, 0.788193798), Y_T0_R2 = c(0.506623748, 3.599442788, 1.734075583, 1.179462912), Y_T0_R3 = c(0.713670106, 2.516735845, 1.236204882, 1.075393433), Y_T1_R1 = c(0.740998252, 1.444496448, 1.077023349, 0.869258744), Y_T1_R2 = c(0.648231834, 0.097957459, 0.791438659, 0.428805547), Y_T1_R3 = c(0.780499252, 0.187840968, 0.820430227, 0.51636582), Y_T2_R1 = c(0.35344654, 1.190274584, 0.401845911, 1.223534348), Y_T2_R2 = c(0.220223951, 1.367784148, 0.362815405, 1.102117612), Y_T2_R3 = c(0.432856978, 1.403057729, 0.10802472, 1.304233845), Y_T3_R1 = c(0.234963735, 1.232129062, 0.072433381, 1.203096462), Y_T3_R2 = c(0.353770497, 0.885122768, 0.011662112, 1.188149743), Y_T3_R3 = c(0.396091395, 1.333921747, 0.192594116, 1.838029829), Z_T0_R1 = c(0.398000559, 1.286528398, 0.129147097, 1.452769794), Z_T0_R2 = c(0.384759325, 1.122251177, 0.119475721, 1.385513609), Z_T0_R3 = c(1.582230097, 0.697419716, 2.406671502, 0.477415567), Z_T1_R1 = c(1.136843842, 0.804552001, 2.13213228, 0.989075996), Z_T1_R2 = c(1.275683837, 1.227821594, 0.31900326, 0.835941568), Z_T1_R3 = c(0.963349308, 0.968589683, 1.706670339, 0.807060135), Z_T2_R1 = c(3.765036263, 0.477443352, 1.712841882, 0.469173869), Z_T2_R2 = c(1.901023385, 0.832736132, 2.223429427, 0.593558769), Z_T2_R3 = c(1.407713024, 0.911920317, 2.011259223, 0.692553388), Z_T3_R1 = c(0.988333629, 1.095130142, 1.648598854, 0.629915612), Z_T3_R2 = c(0.618606729, 0.497458337, 0.549147265, 1.249492088), Z_T3_R3 = c(0.429823986, 0.471389536, 0.977124788, 1.136635484)), row.names = c(NA, -4L ), class = c("data.table", "data.frame"))

使用的脚本

library(dplyr) 
library(stringr) 
library(tidyr) 
gdf1 <- gather(df1, "group", "Expression", -Gene) 
gdf1$tgroup <- apply(str_split_fixed(gdf1$group, "_", 3)[, c(1, 2)], 
                     1, paste, collapse ="_")

library(dplyr) 
tydf1 <- gdf1 %>% 
  group_by(Gene, tgroup) %>% 
  summarize(expression_mean = mean(Expression)) %>% 
  spread(., tgroup, expression_mean)

正在使用#1 热图脚本

library(tidyverse)
tydf1 <- tydf1 %>% 
  as.data.frame() %>% 
  column_to_rownames(var=colnames(tydf1)[1]) 

library(gplots) 
library(vegan) 
randup.m <- as.matrix(tydf1) 
scaleRYG <- colorRampPalette(c("red","yellow","darkgreen"), 
                             space = "rgb")(30) 
data.dist <- vegdist(randup.m, method = "euclidean") 
row.clus <- hclust(data.dist, "aver") 
heatmap.2(randup.m, Rowv = as.dendrogram(row.clus), 
          dendrogram = "row", col = scaleRYG, margins = c(7,10), 
          density.info = "none", trace = "none", lhei = c(2,6), 
          colsep = 1:3, sepcolor = "black", sepwidth = c(0.001,0.0001), 
          xlab = "Identifier", ylab = "Rows")

正在使用#2 热图脚本

df2 <- as.matrix(tydf1[, -1]) 
heatmap(df2)

另外,我想添加一个颜色键。

【问题讨论】:

    标签: r cluster-analysis heatmap hierarchical-clustering


    【解决方案1】:

    我仍然不清楚所需的输出是什么。有一些注意事项:

    • 您不需要使用vegdist() 来计算您的hclust() 调用的距离矩阵。因为如果你检查all(vegdist(randup.m, method = "euclidian") == dist(randup.m)),它会返回TRUE
    • 在您的heatmap.2() 调用中指定Colv = F 将防止列重新排序(默认为TRUE);
    • 也许最好按行缩放数据(请参阅未注释的行);
    • 您对heatmap.2() 的调用会返回带有颜色键的热图。

    总结一下 - 在您的第一个脚本中,您只是错过了 Colv = F 参数,经过一些调整后,它看起来像这样:

    heatmap.2(randup.m, 
              Rowv = as.dendrogram(row.clus),
              Colv = F,
              dendrogram = "row", 
              #scale = "row", 
              col = scaleRYG,
              density.info = "none",
              trace = "none",
              srtCol = -45,
              adjCol = c(.1, .5),
              xlab = "Identifier",
              ylab = "Rows"
              )
    

    但我仍然不确定 - 这是您需要的吗?

    【讨论】:

    • 我有几个实验条件。因此我想保留没有聚类的列以与其他热图进行比较。 #非常感谢utubun。非常感谢您的宝贵时间和热心帮助。
    • 是的,我了解列聚类的问题。只是考虑距离......你的距离矩阵是基因表达水平之间的距离。但也许表达水平本身并不是一个很好的距离衡量标准?因为在两个基因都被上调但一个高度表达而另一个不是的情况下,您将它们分配到不同的簇,这是您不应该的。如果您尝试dist2 &lt;- as.dist(1 - as.matrix(cor(t(randup.m), method = "pearson"), dimnames = dimnames(randup.m))),您会看到不同。但是不要相信我,自己做一些研究。
    • 请问,为什么我的颜色键没有出现。我也试过 key=TRUE, keysize=1.0, key.xlab = "Ln(Peak Area / T0)", key.ylab="", key.title=""
    • 可能是因为在您调用heatmap.2() 时,您使用lhei = c(2,6) 尝试将其更改为lhei = c(3, 6)。如果您在控制台 (Error in .External.graphics...) 中看到错误消息,请尝试 dev.off() 并再次调用 heatmap.2(...)
    • 我想得到你的帮助,在我的热图中,列按字母顺序重新排列(这里是“X”、“Y”和“Z”组,但我的工作 df 名称在“WT”中,“A1”,“A2”,在热图中这个重新排列为“A1”,“A2”,“WT”这个顺序)。我怎样才能阻止这种情况?
    猜你喜欢
    • 2011-02-28
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2021-08-26
    相关资源
    最近更新 更多