BioF3 FigCode · SCI 绘图代码集

GSEA 曲线(GSEA Running Score)

导出日期:2026年6月27日

分类:功能富集 | 依赖包:ggplot2

解决的生物学问题

目标基因集的成员是否倾向于富集在排序列表的顶部或底部?

应用场景

输入数据格式

gseGO / gseKEGG 结果对象 + geneSetID

关键参数

快速开始

1. 直接在线运行

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

2. 本地复现

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

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

3. 安装依赖

# CRAN 包
install.packages(c("ggplot2"))

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

完整代码

library(ggplot2)

# --- Demo: simulate GSEA running score ---
set.seed(42)
n <- 500
ranks <- sort(rnorm(n, 0, 2), decreasing = TRUE)
hits <- sort(sample(1:n, 30))

# Calculate running enrichment score
es <- numeric(n)
hit_indicator <- rep(0, n)
hit_indicator[hits] <- 1
nr <- sum(abs(ranks[hits]))
miss_penalty <- 1 / (n - length(hits))

running <- 0
for (i in 1:n) {
  if (hit_indicator[i] == 1) {
    running <- running + abs(ranks[i]) / nr
  } else {
    running <- running - miss_penalty
  }
  es[i] <- running
}

df_es <- data.frame(Position = 1:n, ES = es)
df_hits <- data.frame(Position = hits, y = 0)

# --- Plot ---
p1 <- ggplot(df_es, aes(x = Position, y = ES)) +
  geom_line(color = "#059669", linewidth = 1.2) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "grey50") +
  geom_segment(data = df_hits, aes(x = Position, xend = Position, y = -0.02, yend = 0.02),
               color = "#dc2626", linewidth = 0.3) +
  labs(x = "Rank", y = "Enrichment Score", title = "GSEA Running Score") +
  theme_classic()
print(p1)

替换为自己的数据

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

延伸阅读