简体   繁体   中英

How to insert multiple vertical and horizontal lines in pheatmap in R?

I am trying to insert vertical and horizontal lines at desired co-ordinates inside a heatmap produced by pheatmap in R .

mat <- matrix(rnorm(200*200),200,200)
pheatmap(mat,treeheight_row = 0, treeheight_col = 0,
         col= colorRampPalette(c("gray", "white", "lightcoral"))((50)))
grid.lines(x=c(50,100,150),y=c(50,100,150)) 

I would like to insert 3 horizontal lines at y = c(50,100,150) and 3 vertical lines at x = c(50,100,150) . Using 'grid.lines' this way (?) does not help.

Here is a solution based on grobs.

library(grid)
nr <- nrow(mat)
nc <- ncol(mat)
grds <- c(50, 100, 150)
downViewport("matrix.4-3-4-3")
for (k in grds) {
  grid.lines(x=c(0,1), y=k/nc, gp=gpar(col="black", lwd=2))
  grid.lines(x=k/nr, y=c(0,1), gp=gpar(col="black", lwd=2))
}
popViewport()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM