热图怎么提取图中聚类后的行基因名称
我们在画热图的时候,行基因的顺序怎么轻松拿到呢? 如果基因数量少可以直接看出来,但是如果有几百个,还用眼睛看,眼睛估计会看瞎,有没有更方便的方法获取?
在谷歌上寻求方法:
https://github.com/jokergoo/ComplexHeatmap/issues/136
这个帖子里给出了答案。
library(ComplexHeatmap)
library(circlize)
set.seed(123) ## 随机种子设置好
mat = matrix(rnorm(80, 2), 8, 10)
mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
rownames(mat) = letters[1:12]
colnames(mat) = letters[1:10]
HM <- Heatmap(mat, km=3) #Make a heatmap, and have 3 clusters
HM <- draw(HM) #Show the heatmap
# r.dend <- row_dend(HM) #Extract row dendrogram ## 不知道是干嘛的
rcl.list <- row_order(HM)
clu_df <- lapply(names(rcl.list), function(i){
out <- data.frame(GeneID = rownames(mat[rcl.list[[i]],]),
Cluster = paste0("cluster", i),
stringsAsFactors = FALSE)
return(out)
})
## 现在就可以拿到按照热图里行的聚类排序的信息了
do.call(rbind, clu_df)
## 和图中是一一对应的
# GeneID Cluster
# 1 j cluster1
# 2 i cluster1
# 3 k cluster1
# 4 l cluster1
# 5 a cluster2
# 6 h cluster2
# 7 c cluster3
# 8 f cluster3
# 9 d cluster3
# 10 e cluster3
# 11 b cluster3
# 12 g cluster3
## 对于列就更简单了:
col.list <- column_order(HM)
colnames(mat[,col.list])
# "e" "b" "g" "f" "j" "a" "i" "h" "d" "c"