【问题标题】:Change pheatmap plot background colour更改 pheatmap 绘图背景颜色
【发布时间】:2019-10-14 12:25:40
【问题描述】:

有没有办法可以将 R 中 pheatmap 的绘图背景从白色更改为黑色

library(pheatmap)
# Create test matrix
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
# Draw heatmapspheatmap(test)
pheatmap(test)

我试过了,但没有成功

 pheatmap(test , bg="black")

或者有没有办法将pheatmaptheme(plot.background = element_rect(colour = 'black', fill = 'black'))ggplot2功能结合起来

【问题讨论】:

    标签: r heatmap pheatmap


    【解决方案1】:

    您可以更改pheatmap生成的对象中grobs的颜色:

    p <- pheatmap(test)
    
    library(grid)
    # Set white color for dendrogram lines and for text
    for (k in c(1,2,4,5)) {
      p$gtable$grobs[[k]]$gp <- gpar(col="white")
    }
    p$gtable$grobs[[6]]$children[[2]]$gp <- gpar(col="white", fontsize=10)
    # Draw a black box behind the heatmap as background
    grid.draw(rectGrob(gp=gpar(fill="black", lwd=0)))
    # Draw the heatmap
    grid.draw(p)
    

    编辑。正如@Keniajin 所建议的,更短的解决方案是:

    grid.draw(rectGrob(gp=gpar(fill="black", lwd=0)))
    grid.draw(p)
    grid.gedit("layout", gp = gpar(col = "white", text = ""))
    

    【讨论】:

    • 感谢 Marco,对于那些不喜欢循环的人来说,这行得通grid.draw(rectGrob(gp=gpar(fill="black", lwd=0))) grid.draw(p) grid.gedit("layout", gp = gpar(col = "white", text = ""))
    • @Keniajin 嗨。非常感谢你的好建议。我编辑了我的答案,添加了你的代码。