通过在它们之间添加黑色列来分隔 ggplot 热图中的组

Separate groups in a ggplot heatmap by adding a black column between them

我有一个数据框:

gene_symbol <- c("sads", "sdsds", "sdasdad", "dfds", "sdsdd")
panel <- c("big", "big", "big", "gr", "sm")
variable <- c("x", "x", "y", "z", "y")
value <- c("normal", "over", "low", "normal", "low")
colors <- c("red", "green", "yellow", "red", "red")

HG44 <- data.frame(gene_symbol, panel, variable, value, colors)
HG44$gene_symbol <- factor(HG44$gene_symbol, levels = unique(HG44$gene_symbol))

我创建了一个热图:

library(ggplot2)
library(reshape2)

pp <- ggplot(HG44, aes(gene_symbol, variable)) + 
  geom_tile(aes(fill = value)) + 
  theme(axis.text.x = element_text(family = "Calibri", 
                                   size = 11, angle = 45, hjust = 1))

library(plotly)

ggplotly(pp)

如您所见,前 3 个基因符号分别属于 panel "big",另外 2 个分别属于另外 2 个 panel。我想以某种方式在不使用 facet.grid() 的情况下分离那些分组的基因符号。我正在考虑在每个组的末尾添加一个空的黑色列,将它们与其他组分开,并将每个 panel 的名称放在顶部或底部。

看来你可以使用geom_vline函数来分隔这些组。见下图:

gene_symbol <- c("sads", "sdsds", "sdasdad","dfds","sdsdd")
panel <- c("big", "big", "big", "gr", "sm")
variable <- c("x","x","y","z","y")
value <- c("normal", "over", "low", "normal", "low")
colors <- c("red", "green", "yellow", "red", "red")

HG44 <- data.frame(gene_symbol, panel, variable, value, colors)
HG44$gene_symbol <- factor(HG44$gene_symbol, levels = unique(HG44$gene_symbol))

library(ggplot2)
library(reshape2)

pp <- ggplot(HG44, aes(gene_symbol,variable)) + 
  geom_tile(aes(fill = value)) + 
  theme(axis.text.x = element_text(family = "Calibri", size = 11, angle = 45, hjust = 1)) +
  geom_vline(xintercept = c(3.5, 4.5), size = 2)

library(plotly)
ggplotly(pp)

输出: