向热图中的列添加黑色边框并添加 space 以分隔它们

Add a black border to columns in a heatmap and add space to separate them

我想为单行热图中的列添加黑色边框,并在列之间添加一些 space,如下所示:

我尝试在 breaksseq 的列之间添加 space 并为每个列添加黑色边框,但我做不到。

library(graphics)
new <- matrix(c(1.3,3,4,6,1.2,1,0.8))
image(new, axes=F)

在 base-R 和 ggplot2 中有几种方法可以做到这一点。您想要的图像看起来很像条形图。由于在您的示例中有不同值的条形图以相同颜色着色,因此我创建了一些 'bins'。对于数据框中的所有必要数据(对于两种解决方案),这更容易:

dat <- data.frame(new=c(1.3,3,4,6,1.2,1,0.8),
                  x=1:nrow(new),
                  y=1)
dat$bin <- cut(dat$new,breaks=seq(0,6,by=1.5),include.lowest=T,
               labels=F)

在基础 R 中:

#create a color palette
mycols <- c("red","orange","yellow","white")

barplot(dat$y, col=mycols[dat$bin], axes=F)

在 ggplot2 中:

p1 <- ggplot(dat, aes(x=x,y=y,fill=bin))+
  geom_bar(stat="identity",col="black",size=2) +
  scale_fill_gradient(low="red",high="white")+
  theme_minimal()+
  theme(legend.position="none",
        axis.line=element_blank(),
        axis.text=element_blank(),
        panel.grid=element_blank(),
        axis.title=element_blank())

试试 pheatmap 包,在那里你可以做:

new <- matrix(c(1.3,3,4,6,1.2,1,0.8))
pheatmap(t(new), cluster_rows = F, cluster_cols = F, border_color = "black", gaps_col = 1:6)