更改 pheatmap 图背景颜色

Change pheatmap plot background colour

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

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功能结合起来

您可以更改 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 = ""))