Select cells in the heatmap

I'm currently trying to set up the heatmap of the matrix and highlight specific cells based on two other matrices.

Example:

> 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 am creating a normal heatmap:

 heatmap(t(SOI), Rowv = NA, Colv = NA) 

Now I want to highlight cells that have 1 in above150 with a frame of the same color (for example, blue), while cells with 1 in below30 should get a red border. All matrices are equal in size, since they are related to each other. I know that I can add things to the heatmap after processing through add.expr, but so far I just managed to create complete ablations covering the entire heatmap => not what I'm looking for.

If anyone has any suggestions, I would be delighted.

+4
source share
1 answer

When add.expr is called, the graph is set so that the center of the cells is in unit integer values. Try adding add.expr = points (1: 5.1: 5). Now all you have to do is write a function that draws fields (help (rect)) with the colors you want in coordinates with full integers.

Try the following:

 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")}) 
+9
source

Source: https://habr.com/ru/post/1379276/


All Articles