Bland–Altman 一致性图
查看两种测量方法的偏倚与差值范围。

准备怎样的数据?
每行一个对象,两种方法各测量一次。
输入字段:id、method_a、method_b
完整 R 代码
所需包:ggplot2、jsonlite。首次使用时,先运行代码注释中的安装命令。
# Bland–Altman 一致性图:两种测量方法的差值与一致性界限
# 项目原创脚本,MIT 许可;模拟数据 CC0-1.0;图形 CC BY 4.0。
# 在全新 R 会话运行。每次运行会更新下方 output_dir 中的本示例文件。
# 图形标签使用英文以避免不同电脑缺少中文字体;可在参数和 labs() 中修改。
# 1. 依赖:首次使用时手动运行以下安装命令,脚本不会自动安装。
# install.packages(c("ggplot2", "jsonlite"))
required_packages <- c("ggplot2", "jsonlite")
missing_packages <- required_packages[!vapply(required_packages, requireNamespace, logical(1), quietly = TRUE)]
if (length(missing_packages)) stop("Please install: ", paste(missing_packages, collapse = ", "))
library(ggplot2)
# 2. 可修改参数:颜色、字体、导出尺寸和路径。输出路径相对于当前工作目录。
set.seed(20261014)
colors <- c("#007F96", "#B66A46")
font_family <- "sans"
figure_width <- 8
figure_height <- 5.5
output_dir <- file.path(getwd(), "rfig-bland-altman-output")
dir.create(output_dir, recursive = TRUE, showWarnings = FALSE)
theme_set(theme_minimal(base_size = 12, base_family = font_family) +
theme(plot.background = element_rect(fill = "white", color = NA), panel.grid.minor = element_blank(),
plot.title = element_text(face = "bold", size = 15), legend.position = "bottom",
plot.margin = margin(14, 18, 14, 14)))
# 3. 示例数据:每行一个对象,两种方法各测量一次。 保持两方法按受试者正确配对,检查单位相同,并预先确定临床可接受差值。
# 使用自己的 CSV 时,将以下数据生成段替换为:
# data <- read.csv("your-data.csv", stringsAsFactors = FALSE)
# 再按本例要求设置分类顺序、检查字段、单位及缺失值。
truth <- rnorm(70, 50, 8)
data <- data.frame(id = 1:70, method_a = truth + rnorm(70, 0, 2), method_b = truth + 1.5 + rnorm(70, 0, 2))
stopifnot(!anyNA(data))
# 4. 统计计算与绘图。差值定义为 B−A;横轴为两方法均值。偏倚为平均差,一致性界限为平均差 ± 1.96×差值 SD,近似覆盖 95% 差值;不是偏倚或界限的 95% CI。需检查差值近似正态、异方差及比例偏倚;可接受范围由专业背景预先确定。
data$average <- (data$method_a + data$method_b) / 2
data$difference <- data$method_b - data$method_a
bias <- mean(data$difference); lower <- bias - 1.96 * sd(data$difference); upper <- bias + 1.96 * sd(data$difference)
lines <- data.frame(y = c(lower, bias, upper), label = c(sprintf("Lower LoA %.2f", lower), sprintf("Bias %.2f", bias), sprintf("Upper LoA %.2f", upper)), type = c("LoA", "Bias", "LoA"))
p <- ggplot(data, aes(average, difference)) + geom_point(color = colors[1], alpha = .7, size = 2.2) +
geom_hline(data = lines, aes(yintercept = y, linetype = type), color = colors[2]) +
geom_text(data = lines, aes(x = Inf, y = y, label = label), inherit.aes = FALSE, hjust = 1.05, vjust = -0.5, size = 3.3) +
scale_linetype_manual(values = c(Bias = "solid", LoA = "dashed")) + guides(linetype = "none") +
scale_y_continuous(expand = expansion(mult = c(.12, .18))) + labs(x = "Mean of methods A and B (demo units)", y = "Difference B - A (demo units)", title = "Agreement between two measurement methods")
metrics <- list(bias = bias, lower = lower, upper = upper)
print(p)
# 5. 导出:CSV 与绘图所用 data 完全一致,preview 与科研图片均来自同一个 p。
write.csv(data, file.path(output_dir, "data.csv"), row.names = FALSE, fileEncoding = "UTF-8")
ggsave(file.path(output_dir, "preview.png"), p, width = figure_width, height = figure_height, dpi = 160, bg = "white")
ggsave(file.path(output_dir, "figure.png"), p, width = figure_width, height = figure_height, dpi = 300, bg = "white")
ggsave(file.path(output_dir, "figure.pdf"), p, width = figure_width, height = figure_height, device = grDevices::pdf, bg = "white")
ggsave(file.path(output_dir, "figure.tiff"), p, width = figure_width, height = figure_height, dpi = 300, compression = "lzw", bg = "white")
jsonlite::write_json(metrics, file.path(output_dir, "metrics.json"), auto_unbox = TRUE, digits = 16, pretty = TRUE)运行与修改
- 安装 R 和 RStudio,复制完整代码到新的 R 脚本文件,或打开下载的 .R 文件。
- 首次使用时,运行脚本顶部注释中的
install.packages()命令。 - 运行整个脚本。演示数据已包含在代码中,无需先下载 CSV。
- 图片和 CSV 保存在当前工作目录的
rfig-bland-altman-output子目录。再次运行会更新其中的同名示例文件。
替换自己的数据
保持两方法按受试者正确配对,检查单位相同,并预先确定临床可接受差值。
将脚本“示例数据”段替换为 data <- read.csv("your-data.csv", stringsAsFactors = FALSE),再按数据字典检查列名、类型、分类顺序及单位。演示 CSV 可用作结构参考。
调整外观与导出
在参数段修改颜色、字体、图片宽高和输出目录;在 labs() 中修改标题和轴标签。脚本提供 PDF、300 dpi PNG 和 LZW 压缩 TIFF 导出。演示图使用英文标签以减少字体依赖。
数据字典
下载示例 CSV每行一个对象,两种方法各测量一次。 演示数据不含缺失值,使用固定种子 20261014。
| 列名 | 类型 | 单位 | 含义 | 角色 |
|---|---|---|---|---|
id | 整数 | 无 | 受试者编号 | 标识 |
method_a | 数值 | 演示单位 | 方法 A 的测量值 | 配对测量 |
method_b | 数值 | 演示单位 | 方法 B 的测量值 | 配对测量 |
average | 数值 | 演示单位 | (A+B)/2,由脚本计算 | X |
difference | 数值 | 演示单位 | B−A,由脚本计算 | Y |
统计说明与适用限制
差值定义为 B−A;横轴为两方法均值。偏倚为平均差,一致性界限为平均差 ± 1.96×差值 SD,近似覆盖 95% 差值;不是偏倚或界限的 95% CI。需检查差值近似正态、异方差及比例偏倚;可接受范围由专业背景预先确定。
本例用于展示绘图方法。应用于真实研究前,请结合研究设计、数据质量和模型假设选择统计方法。