按行/颜色索引从热图中突出显示子集单元格 [英] Highlight Subset Cells From Heatmap By Row/Col Index
问题描述
我正在尝试以视觉方式检查和提取大型热图的子集.例如,我想大致提取群集的行/列索引,例如我在下面圈出的行/列索引:
按照
它必须与以下行有关:
coords = expand.grid(1:nx,1:ny)[cells,]#填充参数...坐标= expand.grid(1:5,1:4)[选择,]
任何人都可以解释这里发生了什么吗?我不确定为什么我的子集即使与另一个问题中的子集都一样也无法正常工作.
非常接近.我认为您在makeRects()函数中输入了错误.在我手中,它进行了一些更改.
#函数,用于在选择单元格周围制作选择矩形makeRects<-函数(单元格){坐标= expand.grid(ny:1,1:nx)[cells]xl = coords [,2] -0.49yb = coords [,1] -0.49xr = coords [,2] +0.49yt = coords [,1] +0.49rect(xl,yb,xr,yt,border ="black",lwd = 3)}#根据选择用矩形重新制作热图#使用已经计算出的热图矩阵,而不重新聚集heatmap.2(hmat,main ="Heatmap-Select 3rd Row",key = T,trace ="none",dendrogram ="none",Rowv = F,Collv = F,add.expr = {makeRects(selection)})
I'm trying to visually inspect, and extract, subsets of large heatmaps. For example, I'd like to roughly extract the row/col indices for clusters like the one I circled below:
Following the advice from here, I hope to achieve this by creating rectangles around subsets of cells by index and repeat until I've highlighted areas close enough to what I want.
Using some simpler data, I tried this:
library(gplots)
set.seed(100)
# Input data 4x5 matrix
nx <- 5
ny <- 4
dat <- matrix(runif(20, 1, 10), nrow=ny, ncol=nx)
# Get hierarchically clustered heatmap matrix
hm <- heatmap.2(dat, main="Test HM", key=T, trace="none")
hmat <- dat[rev(hm$rowInd), hm$colInd]
# Logical matrix with the same dimensions as our data
# indicating which cells I want to subset
selection <- matrix(rep(F,20), nrow=4)
# For example: the third row
selection[3,] <- T
#selection <- dat>7 # Typical subsets like this don't work either
# Function for making selection rectangles around selection cells
makeRects <- function(cells){
coords = expand.grid(1:nx,1:ny)[cells,]
xl=coords[,1]-0.49
yb=coords[,2]-0.49
xr=coords[,1]+0.49
yt=coords[,2]+0.49
rect(xl,yb,xr,yt,border="black",lwd=3)
}
# Re-make heatmap with rectangles based on the selection
# Use the already computed heatmap matrix and don't recluster
heatmap.2(hmat, main="Heatmap - Select 3rd Row", key=T, trace="none",
dendrogram="none", Rowv=F, Colv=F,
add.expr={makeRects(selection)})
This does not work. Here is the result. Instead of the third row being highlighted, we see a strange pattern:
It must have to do with this line:
coords = expand.grid(1:nx,1:ny)[cells,]
# with parameters filled...
coords = expand.grid(1:5,1:4)[selection,]
Can anyone explain what's going on here? I'm not sure why my subset isn't working even though it is similar to the one in the other question.
Very close. I think you made a typo in the makeRects() function. In my hands, it works with a few changes.
# Function for making selection rectangles around selection cells
makeRects <- function(cells){
coords = expand.grid(ny:1, 1:nx)[cells,]
xl=coords[,2]-0.49
yb=coords[,1]-0.49
xr=coords[,2]+0.49
yt=coords[,1]+0.49
rect(xl,yb,xr,yt,border="black",lwd=3)
}
# Re-make heatmap with rectangles based on the selection
# Use the already computed heatmap matrix and don't recluster
heatmap.2(hmat, main="Heatmap - Select 3rd Row", key=T, trace="none",
dendrogram="none", Rowv=F, Colv=F,
add.expr={makeRects(selection)})
这篇关于按行/颜色索引从热图中突出显示子集单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!