差异基因分析结果可视化进阶:用ggplot2美化你的DESeq2火山图和热图

张开发
2026/6/8 4:05:38 15 分钟阅读
差异基因分析结果可视化进阶:用ggplot2美化你的DESeq2火山图和热图
差异基因分析结果可视化进阶用ggplot2打造发表级DESeq2图表在生物信息学研究中差异基因分析是揭示生物学机制的关键步骤而优秀的可视化则是让数据说话的重要工具。许多科研人员虽然掌握了DESeq2的基础分析流程却常常在结果呈现环节遇到瓶颈——默认生成的图表往往缺乏专业美感和信息密度难以满足高水平期刊的发表要求。本文将聚焦于如何利用R语言中的ggplot2包将原始的DESeq2分析结果转化为具有学术美感的可视化作品。1. 火山图的美学升级与功能强化火山图是差异基因分析中最常用的可视化工具之一它能够直观展示基因表达变化的幅度log2FC与统计显著性-log10 p-value的关系。但默认的火山图往往存在信息过载、重点不突出等问题。1.1 基础火山图的优化改造让我们从一个标准的DESeq2结果数据框开始首先进行数据预处理# 加载必要的包 library(ggplot2) library(ggrepel) # 读取DESeq2结果并添加分类标签 deseq_res - read.csv(DESeq2_results.csv, stringsAsFactors FALSE) deseq_res$sig - ifelse(is.na(deseq_res$padj), Not significant, ifelse(deseq_res$padj 0.05 abs(deseq_res$log2FoldChange) 1, ifelse(deseq_res$log2FoldChange 0, Up-regulated, Down-regulated), Not significant))接下来我们可以创建一个基础但已经优化的火山图base_volcano - ggplot(deseq_res, aes(x log2FoldChange, y -log10(padj), color sig)) geom_point(alpha 0.6, size 2) scale_color_manual(values c(Down-regulated #377eb8, Not significant #999999, Up-regulated #e41a1c)) geom_vline(xintercept c(-1, 1), linetype dashed, color grey50) geom_hline(yintercept -log10(0.05), linetype dashed, color grey50) labs(x log2 Fold Change, y -log10 Adjusted p-value) theme_minimal(base_size 12) theme(legend.position top, legend.title element_blank(), panel.grid.minor element_blank())1.2 高级定制技巧为了使火山图更具信息量和发表质量我们可以添加以下改进关键基因标注突出显示最重要的差异表达基因动态点大小根据基因表达量调整点的大小交互式阈值线便于探索不同阈值下的结果# 选择top 10差异最显著的基因进行标注 top_genes - deseq_res[order(deseq_res$padj), ][1:10, ] enhanced_volcano - base_volcano geom_point(data subset(deseq_res, sig ! Not significant), aes(size baseMean), alpha 0.8) scale_size_continuous(range c(1, 5), breaks c(100, 1000, 10000), name Mean expression) geom_text_repel(data top_genes, aes(label gene_name), size 3, box.padding 0.5, max.overlaps 50) ggtitle(Differential gene expression analysis) theme(plot.title element_text(hjust 0.5, face bold))2. 热图的进阶表达策略热图是展示基因表达模式的另一重要工具优秀的聚类热图能够直观呈现样本间的相似性和基因表达模式。2.1 差异基因热图的基础构建首先我们需要准备数据选择差异最显著的基因进行热图展示# 选择差异显著的基因 sig_genes - subset(deseq_res, padj 0.05 abs(log2FoldChange) 1) norm_counts - counts(dds, normalized TRUE) sig_norm_counts - norm_counts[rownames(norm_counts) %in% sig_genes$gene_id, ] # 数据标准化Z-score heatmap_data - t(scale(t(sig_norm_counts)))使用pheatmap包创建基础热图library(pheatmap) pheatmap(heatmap_data, color colorRampPalette(c(navy, white, firebrick3))(50), show_rownames FALSE, clustering_distance_rows euclidean, clustering_distance_cols euclidean, clustering_method complete, border_color NA, fontsize 8, main Heatmap of differentially expressed genes)2.2 热图的高级定制为了提升热图的信息量和美观度我们可以添加基因功能注释改进颜色方案优化布局和标签# 假设我们有GO注释数据 go_annotation - read.csv(gene_go_annotation.csv) # 合并注释信息 row_annotation - data.frame( GO_Term go_annotation$term[match(rownames(heatmap_data), go_annotation$gene_id)] ) rownames(row_annotation) - rownames(heatmap_data) # 创建高级热图 advanced_heatmap - pheatmap( heatmap_data, annotation_row row_annotation, color viridis::viridis(50), show_rownames TRUE, show_colnames TRUE, cluster_rows TRUE, cluster_cols TRUE, fontsize_row 6, fontsize_col 8, angle_col 45, treeheight_row 0, treeheight_col 0, main Annotated heatmap of differentially expressed genes, annotation_colors list(GO_Term c(metabolic process #1b9e77, signal transduction #d95f02, immune response #7570b3)) )3. 功能富集结果的可视化创新差异基因分析后功能富集分析如GO、KEGG是解读生物学意义的关键步骤。传统的条形图和气泡图虽然常见但我们可以做得更好。3.1 富集气泡图的升级气泡图是展示富集结果的经典方式我们可以通过ggplot2进行多维度展示# 假设我们有富集分析结果 enrichment - read.csv(go_enrichment.csv) ggplot(enrichment, aes(x GeneRatio, y reorder(Term, GeneRatio))) geom_point(aes(size Count, color -log10(p.adjust))) scale_color_gradient(low blue, high red, name -log10 Adjusted p-value) scale_size_continuous(range c(3, 8), name Gene count) labs(x Gene Ratio, y GO Term) theme_bw(base_size 12) theme(panel.grid.major element_line(color grey90), axis.text.y element_text(size 10))3.2 富集网络图的构建网络图可以更好地展示功能术语之间的关系library(ggraph) library(tidygraph) # 创建术语相似性网络 term_similarity - 1 - as.matrix(dist(enrichment[, c(p.adjust, GeneRatio)])) diag(term_similarity) - 0 graph - graph_from_adjacency_matrix(term_similarity 0.7, mode undirected) # 添加节点属性 V(graph)$name - enrichment$Term V(graph)$size - enrichment$Count V(graph)$color - -log10(enrichment$p.adjust) # 绘制网络图 ggraph(graph, layout fr) geom_edge_link(alpha 0.2) geom_node_point(aes(size size, color color), alpha 0.8) geom_node_text(aes(label name), repel TRUE, size 3) scale_color_gradient(low blue, high red, name -log10 Adjusted p-value) scale_size_continuous(range c(3, 10), name Gene count) theme_void() ggtitle(GO term similarity network)4. 多图组合与主题统一在科研论文中保持图表风格的一致性至关重要。ggplot2的theme系统可以帮助我们实现这一目标。4.1 创建自定义主题首先定义一个统一的主题my_theme - theme_minimal(base_size 12) theme( plot.title element_text(size 14, face bold, hjust 0.5), axis.title element_text(size 12), axis.text element_text(size 10), legend.title element_text(size 10), legend.text element_text(size 9), panel.grid.major element_line(color grey90), panel.grid.minor element_blank(), panel.border element_rect(color grey30, fill NA, size 0.5), strip.background element_rect(fill grey90, color NA), strip.text element_text(face bold) )4.2 多图组合展示使用patchwork包将多个图表组合在一起library(patchwork) # 假设我们有四个图表对象volcano, heatmap, bubble, network combined_plot - (volcano_plot | heatmap_plot) / (bubble_plot | network_plot) plot_annotation(tag_levels A) my_theme # 导出高质量图片 ggsave(combined_plots.png, combined_plot, width 12, height 10, dpi 300)4.3 颜色方案的统一管理创建统一的颜色方案并应用到所有图表my_colors - list( sig c(Up-regulated #e41a1c, Down-regulated #377eb8, Not significant #999999), go c(metabolic process #1b9e77, signal transduction #d95f02, immune response #7570b3), gradient colorRampPalette(c(#f7f7f7, #d6604d, #b2182b))(100) ) # 应用到各个图表 volcano_plot - volcano_plot scale_color_manual(values my_colors$sig) bubble_plot - bubble_plot scale_color_gradientn(colors my_colors$gradient)

更多文章