如何用R绘制非劣效性分析图?附样本数据与目标示例
非劣效性图绘制(R实现)
先明确你的核心参数:
- 非劣效界值
delta = -2(判定标准:A与B的均值差 ≥ -2时,A非劣于B) - 均值差(A-B):-0.7,95%置信区间:[-2.1, 0.8]
下面提供两种实用的绘制方案:
方案1:用ggplot2手动绘制(高度可控)
先把分析结果整理成数据框,再用ggplot2构建图形:
library(ggplot2) # 整理分析结果 result_data <- data.frame( comparison = "A vs B", mean_diff = -0.7, ci_low = -2.1, ci_high = 0.8, non_inferiority_delta = -2 ) # 绘制非劣效图 ggplot(result_data, aes(x = comparison, y = mean_diff)) + # 置信区间竖线 geom_errorbar(aes(ymin = ci_low, ymax = ci_high), width = 0.2, color = "darkslategray", size = 1) + # 均值差标记点 geom_point(color = "firebrick", size = 4) + # 非劣效界值虚线+标注 geom_hline(yintercept = non_inferiority_delta, color = "steelblue", linetype = "dashed", size = 1) + geom_text(aes(y = non_inferiority_delta, label = paste("非劣效界值\n", non_inferiority_delta)), x = 1.2, color = "steelblue", hjust = 0, vjust = -0.5) + # 均值差为0的参考线 geom_hline(yintercept = 0, color = "gray", linetype = "solid", size = 0.8) + # 主题与标签调整 labs(y = "均值差 (A - B)", x = "", title = "非劣效性分析结果") + theme_minimal() + theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"), axis.text = element_text(size = 12), axis.title.y = element_text(size = 12))
这个图的核心元素:
- 红色点:A与B的均值差
- 黑色竖线:95%置信区间范围
- 蓝色虚线:预设的非劣效界值
- 灰色实线:均值差为0的参考线
方案2:用专用包快速绘制(简化操作)
用ggstatsplot包可以直接基于你的原始数据集生成带非劣效标记的图,适合快速出图:
library(ggstatsplot) library(dplyr) # 基于你的mydata数据集绘制 ggbetweenstats( data = mydata, x = traitement, y = outcome, plot.type = "point", # 仅展示均值和置信区间,也可选"box"显示箱线图 bf.message = FALSE, # 关闭贝叶斯信息 title = "非劣效性分析:A vs B", ylab = "结局指标", # 添加非劣效界值标注 annotations = list( annotate("segment", x = 0.8, xend = 1.2, y = -2, yend = -2, color = "steelblue", linetype = "dashed"), annotate("text", x = 1.3, y = -2, label = "非劣效界值: -2", color = "steelblue") ) )
补充:非劣效性判定提示
你的95%置信区间为[-2.1, 0.8],非劣效界值为-2。由于置信区间下限(-2.1)略低于界值,严格统计标准下未达到非劣效判定(要求置信区间下限 ≥ 非劣效界值),可结合临床意义进一步解读。
内容的提问来源于stack exchange,提问作者Seydou GORO
相关产品推荐
相关产品推荐

