使用 pheatmap 包在热图中添加黑色或边框线的间隙

Add a gap with black color or border line in heatmap with pheatmap package

下面是我的 R 代码。现在命令 gaps_col = c(2,4) 有两个白色间隙。我想将白色间隙更改为黑色或在间隙位置添加黑色边框线。我该怎么做?

另外,我也想更改colnames。我想在 P1 和 P2 中间用 P 替换 P1 和 P2。我想在 Q1 和 Q2 中间用 Q 替换 Q1 和 Q2。我想在T1和T2中间用T替换T1和T2。

library(pheatmap)   
set.seed(123)
df<-data.frame( matrix(sample(30), ncol = 5))
colnames(df)<-LETTERS[1:5]
subj<-c("P1", "P2","Q1", "Q2", "T1","T2")
rownames(df)<-subj
aka2 = data.frame(ID = factor(rep(c("Pat","Trea"), each=3)))
rownames(aka2)<-subj
aka3 = list(ID = c(Pat = "white", Trea="blue"))
pheatmap(t(scale(df)),
         annotation_col = aka2, 
         annotation_colors = aka3[1],
         annotation_legend = FALSE,
         gaps_col =  c(2,4),
         show_colnames = T, show_rownames = T, cluster_rows = F, 
         cluster_cols = F, legend = TRUE, 
         clustering_distance_rows = "euclidean", border_color = FALSE)

这似乎是一个巨大的 hack,也许有更好的方法,但似乎 pheatmap 不支持开箱即用的功能。我能够生成我认为你想要的东西,但每一层的确切尺寸和位置将取决于绘图的输出大小。基本上,我在图的后面画了一个黑色矩形,在图的底部画了你想要的字母注释,创建了没有列名的热图,调整了它的大小,并基本上将图层添加到图。

bg_color <- "black"
bg <- rasterGrob(bg_color,
                 x = 0.5,
                 y = 0.395,
                 width = unit(0.5, "npc"),
                 height = unit(0.82, "npc"),
                 interpolate = TRUE,
                 vjust = 0.4)

text_p <- richtext_grob("P",
                 x = 0.15,
                 y = 0.025)

text_q <- richtext_grob("Q",
                        x = 0.44,
                        y = 0.025)

text_t <- richtext_grob("T",
                        x = 0.73,
                        y = 0.025)

hm <- pheatmap(t(scale(df)),
         annotation_col = aka2, 
         annotation_colors = aka3[1],
         annotation_legend = FALSE,
         gaps_col =  c(2,4),
         show_colnames = F, show_rownames = T, cluster_rows = F, 
         cluster_cols = F, legend = TRUE, 
         clustering_distance_rows = "euclidean", border_color = FALSE)

hm$gtable$vp$height <- unit(0.9, "npc")

grid.newpage()
grid.draw(bg)
grid.draw(text_p)
grid.draw(text_q)
grid.draw(text_t)
print(hm, newpage = FALSE)

结果是: