使用 pheatmap 包在热图中添加间隙

Add a gap in heatmap with pheatmap package

我使用以下代码制作了热图:

library(pheatmap)
library(dplyr)

data = data.frame(matrix(runif(10*10), ncol=10))
data$sample = rep(c("tumour", "normal"), 5)
data$subject.ID = paste('Subject', 1:10)
data = data %>% arrange(sample)

# for row annotation
my_sample_col = data %>% select(sample)
rownames(my_sample_col) = data$subject.ID
# data matrix
mat = as.matrix(data %>% select(-sample, -subject.ID))
rownames(mat) = data$subject.ID

pheatmap(mat,
         scale='row',
         annotation_row = my_sample_col,
         annotation_names_row=F,
         cluster_rows = FALSE,
         cluster_cols = FALSE,
         show_colnames = FALSE,
         show_rownames = FALSE)

我想在第 5 行和第 6 行之间放置一个空隙,以便根据我的行注释分隔热图。

pheatmap 函数中,参数 gaps_row 似乎可以完成这项工作。

vector of row indices that show shere to put gaps into heatmap. Used only if the rows are not clustered.

我不确定如何实施。有人可以帮我弄这个吗?非常感谢。

我建议使用 ComplexHeatmap 包 (website; Gu et al, 2016)。您可以使用 devtools::install_github("jokergoo/ComplexHeatmap").

安装它

它的功能更多,但你也必须投入更多的时间(例如,行注释和矩阵缩放)。

library(ComplexHeatmap)

# Create annotation for rows
my_sample_col_ano <- rowAnnotation(sample = my_sample_col$sample,
                                   show_annotation_name = FALSE)

# Scale original matrix row-wise
matS <- t(apply(mat, 1, scale))

# Plot heatmap
Heatmap(matS, 
        # Remove name from fill legend
        name = "",
        # Keep original row/col order
        row_order = rownames(matS), column_order = colnames(matS),
        # Add left annotation (legend with tumor/normal) 
        left_annotation = my_sample_col_ano,
        # ACTUAL SPLIT by sample group 
        row_split = my_sample_col$sample,
        show_row_names = FALSE, show_column_names = FALSE,
        show_row_dend = FALSE, show_column_dend = FALSE,
        row_title = NULL)

如果你想使用原始的 pheatmap 将参数传递给 gaps_row 等于你的组的大小(即正常):

pheatmap(mat,
         scale='row',
         gaps_row = 5,
         annotation_row = my_sample_col,
         annotation_names_row=F,
         cluster_rows = FALSE,
         cluster_cols = FALSE,
         show_colnames = FALSE,
         show_rownames = FALSE)

如果您可以使用多于两个的组而不是将数值硬编码为 gaps_row(即 gaps_row = 5),您可以传递此代码段 (head(as.numeric(cumsum(table(my_sample_col$sample))), -1)).