告别杂乱!用ComplexHeatmap包搞定多热图组合排版(R语言实战)
2026/6/4 2:36:22 网站建设 项目流程

告别杂乱!用ComplexHeatmap包搞定多热图组合排版(R语言实战)

在生物信息学和数据分析领域,热图(Heatmap)是最常用的可视化工具之一。然而,当我们需要同时展示多个相关热图时——比如基因表达矩阵、统计显著性值和样本分类标签——往往会遇到排版混乱、对齐困难、注释条不统一等问题。这些问题不仅影响图表的美观性,更会降低信息的传达效率。

ComplexHeatmap作为R语言中最强大的热图绘制包之一,其多热图组合功能可以像搭积木一样,通过简单的+%v%操作符实现专业级的出版级图表排版。本文将带你从零开始,掌握这套工具的核心技巧。

1. 环境准备与基础概念

在开始之前,确保你已经安装了ComplexHeatmap包及其依赖:

if (!require("BiocManager")) install.packages("BiocManager") BiocManager::install("ComplexHeatmap") library(ComplexHeatmap)

1.1 理解热图组合的基本原理

ComplexHeatmap的多热图组合基于以下几个关键概念:

  • 主热图(Main Heatmap):组合中的基准热图,其他热图的行顺序、聚类和分割都将与之对齐
  • 水平组合(+操作符):多个热图水平排列,要求所有热图具有相同的行
  • 垂直组合(%v%操作符):多个热图垂直排列,要求所有热图具有相同的列

提示:默认情况下,组合中的第一个热图会被视为主热图,但你也可以通过main_heatmap参数手动指定。

1.2 创建示例数据集

让我们创建三个示例矩阵来演示多热图组合:

set.seed(123) # 主表达矩阵(12行10列) mat1 <- matrix(rnorm(120, mean = 0, sd = 2), nrow = 12, ncol = 10) rownames(mat1) <- paste0("Gene_", LETTERS[1:12]) colnames(mat1) <- paste0("Sample_", 1:10) # 辅助数值矩阵(如p值或fold change) mat2 <- matrix(runif(120, min = 0, max = 1), nrow = 12, ncol = 10) rownames(mat2) <- rownames(mat1) colnames(mat2) <- colnames(mat1) # 分类标签(单列字符矩阵) gene_types <- sample(c("Protein-coding", "lncRNA", "Pseudogene"), 12, replace = TRUE, prob = c(0.7, 0.2, 0.1)) names(gene_types) <- rownames(mat1)

2. 基础组合与排版控制

2.1 最简单的水平组合

使用+操作符可以轻松实现三个热图的水平组合:

# 定义颜色映射 col_fun1 <- colorRamp2(c(-4, 0, 4), c("blue", "white", "red")) col_fun2 <- colorRamp2(c(0, 0.5, 1), c("white", "yellow", "red")) # 创建单个热图对象 ht1 <- Heatmap(mat1, name = "Expression", col = col_fun1, row_title = "Genes", column_title = "Samples") ht2 <- Heatmap(mat2, name = "P-value", col = col_fun2) ht3 <- Heatmap(gene_types, name = "Type") # 组合绘制 ht1 + ht2 + ht3

2.2 精确控制热图尺寸

在实际应用中,我们经常需要调整各个热图的相对宽度:

ht1 <- Heatmap(mat1, name = "Expression", width = unit(8, "cm")) ht2 <- Heatmap(mat2, name = "P-value", width = unit(4, "cm")) ht3 <- Heatmap(gene_types, name = "Type", width = unit(1, "cm")) # 设置热图间距 draw(ht1 + ht2 + ht3, ht_gap = unit(c(2, 5), "mm"))

注意:width参数可以使用绝对单位(如"cm"、"inch"),也可以使用相对数值。间距ht_gap可以接受单个值(统一间距)或向量(分别设置各热图间的间距)。

2.3 垂直组合与混合排版

对于需要垂直排列的热图,使用%v%操作符:

# 转置矩阵用于垂直排列 mat1_t <- t(mat1) mat2_t <- t(mat2) ht1 <- Heatmap(mat1_t, name = "Expression", col = col_fun1) ht2 <- Heatmap(mat2_t, name = "P-value", col = col_fun2) # 垂直组合 ht1 %v% ht2

更复杂的混合排版可以通过组合两种操作符实现:

top_complex <- ht1 + rowAnnotation(foo = anno_barplot(1:10)) bottom_complex <- ht2 + Heatmap(gene_types, name = "Type") top_complex %v% bottom_complex

3. 高级排版技巧

3.1 行列聚类与分割的同步控制

当组合多个热图时,行聚类和分割会自动与主热图同步:

# 主热图进行行聚类和分割 ht_main <- Heatmap(mat1, name = "Expression", row_km = 2, # 将行分为2簇 cluster_rows = TRUE) # 辅助热图不设置聚类 ht_aux <- Heatmap(mat2, name = "P-value", cluster_rows = FALSE) # 组合后,辅助热图的行顺序与主热图一致 ht_main + ht_aux

3.2 注释条的统一管理

ComplexHeatmap会自动调整组合热图中注释条的高度和样式:

# 添加顶部注释 ha <- HeatmapAnnotation( Group = sample(c("Control", "Treatment"), 10, replace = TRUE), Score = anno_barplot(runif(10)) ) # 添加行注释 ra <- rowAnnotation( Length = anno_points(rnorm(12)), GC = runif(12) ) ht1 <- Heatmap(mat1, name = "Expression", top_annotation = ha) ht2 <- Heatmap(mat2, name = "P-value") ht3 <- Heatmap(gene_types, name = "Type", left_annotation = ra) # 组合后注释条自动对齐 ht_list <- ht1 + ht2 + ht3 draw(ht_list)

3.3 子集提取与复杂重组

可以从组合热图中提取特定行列的子集:

# 创建复杂组合 ht_full <- ht1 + ht2 + ht3 # 提取前5行,只显示Expression和Type热图 ht_subset <- ht_full[1:5, c("Expression", "Type")] draw(ht_subset)

4. 实战案例:基因表达分析可视化

让我们通过一个完整的基因表达分析案例,整合前面学到的所有技巧。

4.1 数据准备与预处理

# 模拟差异表达分析结果 de_genes <- data.frame( Gene = rownames(mat1), logFC = rnorm(12, mean = c(rep(1, 6), rep(-1, 6))), pvalue = c(runif(6, 0, 0.01), runif(6, 0.05, 1)), FDR = p.adjust(c(runif(6, 0, 0.01), runif(6, 0.05, 1)), method = "BH") ) # 添加显著性标记 de_genes$Significant <- ifelse(de_genes$FDR < 0.05, "Yes", "No") # 创建行注释 row_anno <- rowAnnotation( LogFC = anno_barplot(de_genes$logFC, gp = gpar(fill = ifelse(de_genes$logFC > 0, "red", "green"))), Significance = de_genes$Significant, col = list(Significance = c("Yes" = "black", "No" = "lightgray")) )

4.2 构建完整可视化

# 主表达热图 ht_exp <- Heatmap(mat1, name = "Z-score", col = colorRamp2(c(-2, 0, 2), c("blue", "white", "red")), row_title = "Differentially Expressed Genes", column_title = "Expression Profile", row_names_side = "left", show_row_names = TRUE) # p值热图 ht_pval <- Heatmap(-log10(de_genes$pvalue), name = "-log10(p)", col = colorRamp2(c(0, 2), c("white", "purple")), width = unit(5, "mm")) # FDR热图 ht_fdr <- Heatmap(-log10(de_genes$FDR), name = "-log10(FDR)", col = colorRamp2(c(0, 2), c("white", "darkred")), width = unit(5, "mm")) # 组合绘制 final_plot <- ht_exp + row_anno + ht_pval + ht_fdr draw(final_plot, column_title = "Differential Expression Analysis Results", column_title_gp = gpar(fontsize = 14, fontface = "bold"), ht_gap = unit(c(2, 1, 1), "mm"))

4.3 输出高质量图表

为了发表级图表输出,建议使用pdf()png()设备:

pdf("DE_analysis_heatmap.pdf", width = 10, height = 8) draw(final_plot) dev.off()

或者直接使用ComplexHeatmap提供的交互式功能:

InteractiveComplexHeatmap::makeInteractiveComplexHeatmap()

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询