BioF3 FigCode · SCI 绘图代码集

列线图(Nomogram)

导出日期:2026年6月27日

分类:生存分析 | 依赖包:rms、survival

解决的生物学问题

对于一个具体患者,结合所有临床和分子变量,他的 1/3/5 年生存概率是多少?

应用场景

输入数据格式

rms::cph 对象 + datadist 设定

关键参数

快速开始

1. 直接在线运行

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

2. 本地复现

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

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

3. 安装依赖

# CRAN 包
install.packages(c("rms", "survival"))

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

完整代码

library(rms)
library(survival)

# --- Demo data ---
set.seed(42)
n <- 300
df <- data.frame(
  time   = rexp(n, 0.04),
  status = sample(0:1, n, replace = TRUE, prob = c(0.3, 0.7)),
  risk_score = rnorm(n),
  age    = sample(40:80, n, replace = TRUE),
  stage  = factor(sample(c("I","II","III","IV"), n, replace = TRUE))
)

# --- Required: tell rms about variable distribution ---
dd <- datadist(df)
options(datadist = "dd")

# --- Cox model with rms::cph ---
fit <- cph(
  Surv(time, status) ~ risk_score + age + stage,
  data = df, x = TRUE, y = TRUE, surv = TRUE
)

# --- Build survival functions for 1, 3, 5 years ---
surv_obj <- Survival(fit)
surv1 <- function(x) surv_obj(1, lp = x)
surv3 <- function(x) surv_obj(3, lp = x)
surv5 <- function(x) surv_obj(5, lp = x)

# --- Nomogram ---
nom <- nomogram(
  fit,
  fun  = list(surv1, surv3, surv5),
  funlabel = c("1-year Survival", "3-year Survival", "5-year Survival"),
  fun.at   = c(0.95, 0.85, 0.7, 0.5, 0.3, 0.1, 0.01),
  maxscale = 100,
  lp = FALSE
)
plot(nom, xfrac = 0.4, cex.axis = 0.9, cex.var = 1)

替换为自己的数据

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

延伸阅读