BioF3 FigCode · SCI 绘图代码集
GO 条形图(GO Bar Plot)
分类:功能富集 | 依赖包:ggplot2
解决的生物学问题
差异基因在生物过程、分子功能和细胞组分三个维度分别富集了哪些功能?
应用场景
- GO 三大类别全景展示
- 对比 BP/MF/CC 富集差异
- 文章 figure 综合功能注释
- 多组差异基因功能对比
输入数据格式
enrichGO 结果(分别跑 BP/MF/CC 后合并)
关键参数
- showCategory 每类展示数(5-10)
- ont 本体类型(ALL / BP / MF / CC)
- pvalueCutoff 阈值
- facet 是否分面展示
- color 着色变量(p.adjust / Count)
快速开始
1. 直接在线运行
打开 FigCode 在线绘图,点击本工具卡片的"在线绘图"按钮即可用内置示例数据出图,零环境配置。
2. 本地复现
下载脚本和示例数据,本地 RStudio 运行:
# 下载
curl -O https://<your-site>/figcode/scripts/go-bar.R
curl -O https://<your-site>/figcode/data/go-bar.csv
3. 安装依赖
# CRAN 包
install.packages(c("ggplot2"))
# Bioconductor 包(如需)
# BiocManager::install(c())
完整代码
library(ggplot2)
# --- Demo data ---
set.seed(42)
bp_terms <- c("Cell division", "DNA repair", "Apoptotic process", "Signal transduction", "Immune response")
mf_terms <- c("Protein binding", "ATP binding", "Kinase activity", "DNA binding", "Receptor activity")
cc_terms <- c("Nucleus", "Cytoplasm", "Membrane", "Mitochondrion", "Extracellular")
go_df <- data.frame(
Description = c(bp_terms, mf_terms, cc_terms),
Count = sample(5:35, 15),
p.adjust = runif(15, 0.0001, 0.04),
ONTOLOGY = rep(c("BP", "MF", "CC"), each = 5)
)
# --- Plot ---
ggplot(go_df, aes(x = reorder(Description, Count), y = Count, fill = p.adjust)) +
geom_col(width = 0.7) +
coord_flip() +
facet_grid(ONTOLOGY ~ ., scales = "free_y", space = "free_y") +
scale_fill_gradient(low = "#dc2626", high = "#93c5fd") +
labs(x = NULL, y = "Gene Count", title = "GO Enrichment (BP/MF/CC)", fill = "p.adjust") +
theme_classic() +
theme(strip.text = element_text(face = "bold"))
替换为自己的数据
脚本中以 # --- Demo data --- 标注的段落是示例数据生成代码。替换为自己的数据时,保持列名一致即可:
- 输入格式:enrichGO 结果(分别跑 BP/MF/CC 后合并)
- 使用
read.csv()/readRDS()读取本地文件
延伸阅读
- 相关教程:GO 条形图 完整流程
- 出现 bug?欢迎在 FigCode 页面 点击对应卡片,在评论区留言。