您可以为每个单元格单独编辑边框颜色,这可用于解决此问题。确切的解决方案将取决于您的 pheatmap 对象的确切组成。
首先,获取存储在热图中的 grobs(图形对象)的名称。您将需要获取与热图的各个矩形(单元格)相对应的那个。对我来说,这是gTree 的第一个类,但试着看看哪一个适合你。这就是我提取 grob 名称的方式:
grob_classes <- purrr::map(pheatmapM$gtable$grobs, class)
idx_grob <- which(purrr::map_lgl(grob_classes, function(cl) 'gTree' %in% cl))[1]
接下来,我们需要找到一个矩形 grob,它是我们刚刚找到的 gTree grob 的子对象,如下所示:
grob_names <- names(pheatmapM$gtable$grobs[[idx_grob]]$children)
idx_rect <- grob_names[grep('rect', grob_names)][1]
现在,您可以从这个嵌套的 grob 中提取图形参数:fill(确定每个单元格的实际填充颜色)和col(确定边框颜色)。默认情况下,col 是单个值,而 fill 是十六进制代码矩阵。当我将col 替换为fill 中存储的内容时,就像这样...
pheatmapM$gtable$grobs[[idx_grob]]$children[[idx_rect]]$gp$col <- pheatmapM$gtable$grobs[[idx_grob]]$children[[idx_rect]]$gp$fill
...这相当于消除了边框,因为它现在与填充颜色相同。为了额外确保单元格之间没有间隙,请增加边框厚度。
pheatmapM$gtable$grobs[[idx_grob]]$children[[idx_rect]]$gp$lwd <- 3
据我估计,在您的特定pheatmap 中识别正确的idx_grob 或idx_rect 可能会出现潜在问题。出于这个原因,我提供了一个可重现的示例(在 macOS 上运行 R 4.0.3 和 pheatmap 1.0.12),它实现了您可以用来开始的所需结果(没有边界以及单元格之间没有间隙):
set.seed(1)
## Generate sample data
x <- table(round(rnorm(500, 100, 2)), round(rnorm(500, 100, 2)))
ph <- pheatmap::pheatmap(x, cluster_cols = FALSE, cluster_rows = FALSE)
## Extract the right grob
grob_classes <- purrr::map(ph$gtable$grobs, class)
idx_grob <- which(purrr::map_lgl(grob_classes, function(cl) 'gTree' %in% cl))[1]
grob_names <- names(ph$gtable$grobs[[idx_grob]]$children)
idx_rect <- grob_names[grep('rect', grob_names)][1]
## Remove borders around cells
ph$gtable$grobs[[idx_grob]]$children[[idx_rect]]$gp$col <- ph$gtable$grobs[[idx_grob]]$children[[idx_rect]]$gp$fill
ph$gtable$grobs[[idx_grob]]$children[[idx_rect]]$gp$lwd <- 3
## Plot result
graphics::plot.new()
print(ph)
结果应该是这样的: