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

如何在R中定制匹配示例样式的Time to Event Plot

解决R中Time to Event Plot的样式匹配问题

需要绘制符合指定样式的Time to Event Plot,要求实现数据点连接并添加置信带,但现有ggplot2代码未达到预期效果。以下是修改后的解决方案:

数据结构

B <- structure(list(type = c("dynamic", "dynamic", "dynamic", "dynamic", 
"dynamic", "dynamic", "dynamic", "dynamic", "dynamic"), term = c("ATT(-4)", 
"ATT(-3)", "ATT(-2)", "ATT(-1)", "ATT(0)", "ATT(1)", "ATT(2)", 
"ATT(3)", "ATT(4)"), event.time = c(-4, -3, -2, -1, 0, 1, 2, 
3, 4), estimate = c(0.26515256993999, 0.151872124263966, 0.0869620743535941, 
0.511898495150082, 1.70892342183257, 1.47487464226574, 1.27266126343054, 
1.41148383616974, 2.09672811604038), std.error = c(0.165251036032427, 
0.125780865480357, 0.139673647128451, 0.0843033105456399, 0.132559563887908, 
0.153858075990952, 0.165464288658923, 0.226006579280847, 0.291119261169366
), conf.low = c(-0.0587335090914985, -0.09465384202181, -0.186793243607526, 
0.346667042703132, 1.44911145080594, 1.17331835459285, 0.948357216931508, 
0.968519080510181, 1.52614484894251), conf.high = c(0.589038648971479, 
0.398398090549743, 0.360717392314714, 0.677129947597032, 1.96873539285921, 
1.77643092993863, 1.59696530992956, 1.8544485918293, 2.66731138313825
), point.conf.low = c(-0.0587335090914985, -0.09465384202181, 
-0.186793243607526, 0.346667042703132, 1.44911145080594, 1.17331835459285, 
0.948357216931508, 0.968519080510181, 1.52614484894251), point.conf.high = 
 c(0.589038648971479, 
0.398398090549743, 0.360717392314714, 0.677129947597032, 1.96873539285921, 
1.77643092993863, 1.59696530992956, 1.8544485918293, 2.66731138313825
), prepost = c("pre", "pre", "pre", "pre", "post", "post", "post", 
"post", "post")), class = "data.frame", row.names = c(NA, -9L
))

现有代码问题分析

  • 未添加数据点连线的geom_line
  • 使用geom_smooth(method="loess")生成的是拟合曲线的置信带,而非基于现有数据置信区间的带状区域
  • 不必要的position_dodge(无分组需要偏移)
  • x轴错误设置为离散型,event.time是连续数值

修改后的代码

# 定义颜色(假设colores是你已有的颜色向量,这里用示例颜色)
colores <- c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728")

grafica <- B %>%
  ggplot(aes(x = event.time, y = estimate, color = prepost)) +
  # 绘制置信带
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high, fill = prepost), 
              alpha = 0.1, color = NA) +
  # 连接数据点的线
  geom_line(size = 0.8) +
  # 数据点
  geom_point(size = 2) +
  # 误差棒
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.1) +
  # 参考线
  geom_hline(yintercept = 0, linetype = "longdash") +
  geom_vline(xintercept = 0) +
  # 颜色设置
  scale_color_manual("Time", values = colores[c(4, 1)]) +
  scale_fill_manual("Time", values = colores[c(4, 1)]) +
  # 坐标轴设置:x轴用连续型,保留指定刻度
  scale_x_continuous(name = "Time to event", 
                     breaks = c(-4, -3, -2, -1, 0, 1, 2, 3, 4)) +
  # 标签和主题
  labs(title = "Plot", y = "ylab") +
  theme_classic()

grafica

修改说明

  1. 添加geom_line实现数据点的连续连接
  2. 使用geom_ribbon基于数据中的conf.low和conf.high绘制置信带,设置透明度区分
  3. 移除不必要的position_dodge,因为数据为单组无需偏移
  4. 将x轴改为连续型scale_x_continuous,保留目标刻度值,符合数值型变量的展示逻辑
  5. 统一color和fill的映射,保持prepost分组的颜色一致性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 01:10:05