Highlight cells in heatmap

前端 未结 1 444
半阙折子戏
半阙折子戏 2021-01-13 22:57

I am currently trying to set up a heatmap of a matrix and highlight specific cells, based on two other matrices.

An example:

> SOI
         NAP_G0         


        
1条回答
  •  北海茫月
    2021-01-13 23:09

    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.

    Try this:

    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")})
    

    0 讨论(0)
提交回复
热议问题