You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何绘制分Ontology类别的GO富集分析Dotplot(ggplot/ClusterProfiler)

解决GO富集分析Dotplot分面后Y轴显示全部术语的问题

问题原因

使用facet_grid时所有子图显示全部Ontology术语,是因为Description作为全局因子,其所有水平会被应用到每个分面的Y轴上。要实现每个分面仅显示对应类别的术语,需要让每个类别的Y轴标签独立。


方法一:原生ggplot自定义处理

通过创建唯一的Y轴标识,配合scales="free_y"实现分面Y轴独立,再还原显示原始术语标签:

library(dplyr)
library(ggplot2)

# 预处理数据:创建唯一Y轴ID,确保每个术语与类别绑定
top_enriched <- top_enriched %>%
  mutate(y_id = paste(Category, Description, sep = "||")) %>%
  arrange(Category, desc(GeneRatio)) # 按类别和GeneRatio排序,保证术语顺序

# 绘图
ggplot(top_enriched, aes(x = GeneRatio, y = y_id, color = p.adjust, size = Count)) +
  geom_point() +
  # 开启Y轴自由缩放,每个分面仅显示本组术语
  facet_grid(Category ~ ., scales = "free_y", space = "free_y") +
  scale_color_gradient(low = "blue", high = "red") +
  # 还原Y轴标签为原始术语名称
  scale_y_discrete(labels = function(x) sapply(strsplit(x, "\\|\\|"), function(y) y[2])) +
  labs(y = "", x = "Gene Ratio") +
  theme(strip.text.y = element_text(angle = 0)) # 调整分面标签方向,提升可读性

方法二:利用clusterProfiler原生dotplot功能

如果你的富集分析是用clusterProfiler完成的,直接使用其内置的dotplot函数,通过split参数分面,自动实现每个类别的术语独立显示:

library(clusterProfiler)

# 将示例数据转换为enrichResult对象(实际分析可直接使用富集结果)
ego <- new("enrichResult",
           result = top_enriched %>%
             select(ID = Description, Description, GeneRatio, p.adjust, Count, Category),
           ontology = "GO",
           organism = "human")

# 绘制分面Dotplot
dotplot(ego, split = "Category", scales = "free_y", font.size = 10) +
  scale_color_gradient(low = "blue", high = "red") +
  theme(strip.text.y = element_text(angle = 0))

方法对比

  • 方法一:基于原生ggplot,自定义空间大,适合需要高度定制绘图样式的场景。
  • 方法二:利用clusterProfiler内置功能,代码更简洁,适配clusterProfiler的富集分析工作流。

内容的提问来源于stack exchange,提问作者Saleh Sarwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 05:43:15