r - 使用 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.
我不确定如何实现。有人可以帮我弄这个吗?非常感谢。
解决方案
我建议使用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)
)。
推荐阅读
- java - Spring boot 一个组件需要一个找不到的“javax.persistence.EntityManagerFactory”类型的bean
- javascript - 如何将第三方 cookie 传递到相应的第三方域?
- python-3.x - 使用python索引音频以获取每个单词的时间戳
- concurrency - 具有并发控制和 ACID 属性的数据库分片
- oracle - 根据 Oracle PL/SQL 中的日期验证保存记录
- sql - 在雪花中合并表的不同方法的性能
- reactjs - onClick 方法处理调用错误 - 未定义 no-undef
- java - Spring Data Redis 生菜连接问题
- react-native - 如何在 React Native 中制作类似 Amazon App 的下拉过滤器?
- javascript - 如何在不刷新页面的情况下发送表单提交数据?