首页 > 解决方案 > 使用 pheatmap 包在热图中添加间隙

问题描述

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

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.

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

标签: rheatmappheatmap

解决方案


我建议使用ComplexHeatmap包(网站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))。

在此处输入图像描述


推荐阅读