You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用R绘制汇总临床试验入组年龄要求的折线图

解决方法

1. 准备示例数据

先构造你的试验数据集:

trial_data <- data.frame(
  minAge = c(12, 10, 11),
  maxAge = c(25, 17, 30)
)

2. 生成目标年龄序列

定义需要覆盖的年龄范围(比如5到35):

age_range <- 5:35

3. 计算各年龄对应的研究数量

遍历每个年龄,统计有多少项试验的年龄区间包含该年龄:

# 统计每个年龄符合入组条件的试验数
counts <- sapply(age_range, function(age) {
  sum(trial_data$minAge <= age & trial_data$maxAge >= age)
})

# 整理为ggplot可直接使用的数据框
age_counts <- data.frame(
  Age = age_range,
  Study_Count = counts
)

4. 用ggplot绘制折线图

基于整理好的数据绘制折线图,添加点标记提升可读性:

library(ggplot2)

ggplot(age_counts, aes(x = Age, y = Study_Count)) +
  geom_line(color = "steelblue", linewidth = 1) +
  geom_point(color = "darkred", size = 2) +
  labs(
    x = "年龄",
    y = "允许入组的研究数量",
    title = "各年龄对应的临床试验入组资格数量"
  ) +
  theme_minimal()

大数据量优化方案

如果你的数据集规模很大,逐行遍历效率偏低,可以用lubridate包的区间函数优化:

library(lubridate)

# 将试验年龄范围转换为interval对象
trial_data$age_interval <- interval(trial_data$minAge, trial_data$maxAge)

# 快速统计每个年龄的符合数量
counts <- sapply(age_range, function(age) {
  sum(age %within% trial_data$age_interval)
})

内容的提问来源于stack exchange,提问作者felbamato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 11:43:14