突出显示热图中的单元格 [英] Highlight cells in heatmap
问题描述
我目前正在尝试基于其他两个矩阵建立矩阵的热图并突出显示特定的单元格。
I am currently trying to set up a heatmap of a matrix and highlight specific cells, based on two other matrices.
示例:
> SOI
NAP_G021 NAP_G033 NAP_G039 NAP_G120 NAP_G122
2315101 59.69418 27.26002 69.94698 35.22521 38.63995
2315102 104.15294 76.70379 114.72999 97.35930 79.46014
2315104 164.32822 61.83898 140.99388 63.25482 105.48041
2315105 32.15792 21.03730 26.89965 36.25943 40.46321
2315103 74.67434 82.49875 133.89709 93.17211 35.53019
> above150
NAP_G021 NAP_G033 NAP_G039 NAP_G120 NAP_G122
2315101 0 0 0 0 0
2315102 0 0 0 0 0
2315104 1 0 0 0 0
2315105 0 0 0 0 0
2315103 0 0 0 0 0
> below30
NAP_G021 NAP_G033 NAP_G039 NAP_G120 NAP_G122
2315101 0 1 0 0 0
2315102 0 0 0 0 0
2315104 0 0 0 0 0
2315105 0 1 1 0 0
2315103 0 0 0 0 0
现在,我创建一个普通的热图:
Now I create a normal heatmap:
heatmap(t(SOI), Rowv = NA, Colv = NA)
现在我要做的是突出显示单元格,该单元格在 above150
中有一个一种颜色(例如蓝色)的框架,而在低于30
中为1的单元格应该得到一个红色框架。当然,所有矩阵在相关时大小相等。我知道我可以在通过add.expr处理后将事物添加到热图,但是到目前为止,我只是设法创建了覆盖整个热图的完整ablines =>不是我想要的。
Now what I want to do is highlight the cells, that have a 1 in above150
with a frame of one colour (e.g. blue), whilst the cells with a 1 in below30
should get a red frame. Of couse all matrices are equal sized as they are related. I know that I can add things to the heatmap after processing via add.expr, but so far I just managed to create full ablines that span the whole heatmap => not what I'm looking for.
如果有人有任何建议,我会很高兴。
If anybody has any suggestions I would be delighted.
推荐答案
当add.expr被称为绘图时设置为使单元格的中心处于单位整数值。尝试查看add.expr = points(1:5,1:5)。现在,您所需要做的就是编写一个函数,该函数以所需的颜色在半整数坐标处绘制框(帮助(矩形))。
When add.expr is called the plot is set so that the centre of the cells is at unit integer values. Try add.expr=points(1:5,1:5) to see. Now all you need to do is write a function that draws boxes (help(rect)) with the colours you need, at the half-integer coordinates.
请尝试以下操作:
set.seed(310366)
nx=5
ny=6
SOI=matrix(rnorm(nx*ny,100,50),nx,ny)
colnames(SOI)=paste("NAP_G0",sort(as.integer(runif(ny,10,99))),sep="")
rownames(SOI)=sample(2315101:(2315101+nx-1))
above150 = SOI>150
below30=SOI<30
makeRects <- function(tfMat,border){
cAbove = expand.grid(1:nx,1:ny)[tfMat,]
xl=cAbove[,1]-0.49
yb=cAbove[,2]-0.49
xr=cAbove[,1]+0.49
yt=cAbove[,2]+0.49
rect(xl,yb,xr,yt,border=border,lwd=3)
}
heatmap(t(SOI),Rowv = NA, Colv=NA, add.expr = {
makeRects(above150,"red");makeRects(below30,"blue")})
这篇关于突出显示热图中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!