BioF3 FigCode · SCI 绘图代码集

通路网络图(Gene-Term Network)

导出日期:2026年6月27日

分类:网络与互作 | 依赖包:ggplot2、igraph、ggraph

解决的生物学问题

富集通路之间是否共享关键基因?哪些基因是多条通路的枢纽节点?

应用场景

输入数据格式

enrichGO 结果 + fold change 向量

关键参数

快速开始

1. 直接在线运行

打开 FigCode 在线绘图,点击本工具卡片的"在线绘图"按钮即可用内置示例数据出图,零环境配置。

2. 本地复现

下载脚本和示例数据,本地 RStudio 运行:

# 下载
curl -O https://<your-site>/figcode/scripts/cnetplot.R
curl -O https://<your-site>/figcode/data/cnetplot.csv

3. 安装依赖

# CRAN 包
install.packages(c("ggplot2", "igraph", "ggraph"))

# Bioconductor 包(如需)
# BiocManager::install(c())

完整代码

library(ggplot2)
library(igraph)
library(ggraph)

# --- Demo: simulate gene-term network ---
set.seed(42)
terms <- c("Cell Cycle", "DNA Repair", "Apoptosis", "Immune Response")
genes_per_term <- list(
  c("CDK1","CDK2","CCNB1","CCNA2","CDC20"),
  c("BRCA1","RAD51","ATM","CDK2","CCNA2"),
  c("TP53","BAX","BCL2","CASP3","ATM"),
  c("IL6","TNF","IFNG","STAT1","TP53")
)

edges <- data.frame(from = character(), to = character(), stringsAsFactors = FALSE)
for (i in seq_along(terms)) {
  edges <- rbind(edges, data.frame(from = terms[i], to = genes_per_term[[i]]))
}
g <- graph_from_data_frame(edges, directed = FALSE)
V(g)$type <- ifelse(V(g)$name %in% terms, "term", "gene")
V(g)$size <- ifelse(V(g)$type == "term", 8, 3)

fc <- setNames(rnorm(length(unique(edges$to)), 0, 2), unique(edges$to))
V(g)$fc <- ifelse(V(g)$type == "gene", fc[V(g)$name], 0)

# --- Plot ---
ggraph(g, layout = "fr") +
  geom_edge_link(alpha = 0.3) +
  geom_node_point(aes(size = size, color = fc)) +
  geom_node_text(aes(label = name), repel = TRUE, size = 3) +
  scale_color_gradient2(low = "#2563eb", mid = "grey90", high = "#dc2626", midpoint = 0) +
  scale_size_continuous(range = c(2, 10), guide = "none") +
  labs(title = "Gene-Term Network", color = "log2FC") +
  theme_void()

替换为自己的数据

脚本中以 # --- Demo data --- 标注的段落是示例数据生成代码。替换为自己的数据时,保持列名一致即可:

延伸阅读