【问题标题】:No border color in "pheatmap" in RR中的“pheatmap”中没有边框颜色
【发布时间】:2017-06-01 23:41:17
【问题描述】:

我正在探索 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")

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

【问题讨论】:

  • 试试border_color = NA 不加引号?
  • 已经试过了,不行。
  • border_color = NAborder_color = "NA" 对我来说都很好
  • 实际上它可以在 Windows 计算机上运行,​​但不能在我的 Mac 上运行。很奇怪。
  • 我遇到了完全相同的问题。你有想过吗?我什至正在编辑 svg 文件,由于某种原因,svg 渲染器仍然显示边框,并将 stroke 属性设置为 none。

标签: r pheatmap


【解决方案1】:

您可以为每个单元格单独编辑边框颜色,这可用于解决此问题。确切的解决方案将取决于您的 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_grobidx_rect 可能会出现潜在问题。出于这个原因,我提供了一个可重现的示例(在 macOS 上运行 R 4.0.3pheatmap 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)

结果应该是这样的:

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多