BioF3 FigCode · SCI 绘图代码集

分位数图(QQ Plot)

导出日期:2026年6月27日

分类:分布可视化 | 依赖包:qqman

解决的生物学问题

我的检验 p 值在 null 假设下应该均匀分布,实际有偏离吗?模型残差正态吗?

应用场景

输入数据格式

一维数值向量(p 值列表 / 残差)

关键参数

快速开始

1. 直接在线运行

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

2. 本地复现

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

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

3. 安装依赖

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

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

完整代码

library(qqman)

# --- Demo: simulate p-values with mild inflation ---
set.seed(42)
n <- 10000
p_null <- runif(n)
p_inflated <- pmin(runif(n) * 0.95, 1)   # mild inflation

# True GWAS-like with a few hits + inflation
p_real <- c(p_inflated, runif(20, 1e-12, 1e-7))

# --- QQ plot of p-values ---
par(mfrow = c(1, 2))
qq(p_null, main = "Null (well-calibrated)", cex = 0.5)
qq(p_real, main = "Mild inflation + real hits", cex = 0.5)

# Compute genomic inflation factor lambda
chisq <- qchisq(1 - p_real, 1)
lambda <- median(chisq) / qchisq(0.5, 1)
mtext(sprintf("lambda = %.3f", lambda),
      side = 3, line = -1.4, adj = 0.95, cex = 0.9)

替换为自己的数据

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

延伸阅读