我正在探索R来分析我的基因表达数据。
我可以使用pheatmap包创建一个漂亮的热图。然而,当在不同颜色的框之间转换时,它会自动引入边框颜色,放大后我可以看到边框颜色。这是尽管添加了属性border_color = "NA"。有可能得到连续变化的热图吗?谢谢。
我的代码:
pheatmapM <-pheatmap(datExprM, border_color = "NA", breaks = NULL, color = colorRampPalette(c("navy", "black", "yellow"))(50), cluster_rows = TRUE, cluster_cols = TRUE, scale = "row")

如下图所示(缩放后),相邻的框被较浅的颜色隔开。

发布于 2021-01-27 07:52:40
您可以为每个单元格单独编辑边框颜色,这可用于解决此问题。确切的解决方案将取决于pheatmap对象的确切组成。
首先,获取存储在热图中的grobs (图形对象)的名称。您需要获取与热图的各个矩形(单元格)相对应的一个。对我来说,这是gTree类的第一个grob,但试着看看哪一个适合您。下面是我提取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是十六进制代码的矩阵。当我用存储在fill中的内容替换col时,就像这样...
pheatmapM$gtable$grobs[[idx_grob]]$children[[idx_rect]]$gp$col <- pheatmapM$gtable$grobs[[idx_grob]]$children[[idx_rect]]$gp$fill...that相当于消除了边框,因为它现在与填充的颜色相同。要确保单元格之间没有空隙,请同时增加边框厚度。
pheatmapM$gtable$grobs[[idx_grob]]$children[[idx_rect]]$gp$lwd <- 3据我估计,在您的特定idx_grob中识别正确的pheatmap或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)结果应该如下所示:

https://stackoverflow.com/questions/44318690
复制相似问题