【问题标题】:Add a gap in heatmap with pheatmap package使用 pheatmap 包在热图中添加间隙
【发布时间】:2018-12-29 20:53:38
【问题描述】:

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

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.

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

【问题讨论】:

    标签: r heatmap pheatmap


    【解决方案1】:

    我建议使用ComplexHeatmap 包(websiteGu 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),则可以传递此sn-p(head(as.numeric(cumsum(table(my_sample_col$sample))), -1))。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多