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

求助:绘制按序列和访视分组带95%置信区间的Spaghetti图

Fixing Your Longitudinal Concentration-Time Spaghetti Plot with 95% CIs

Got it, let's sort out this Spaghetti plot issue for you! The core problem with your current code is that you're only plotting mean lines and individual confidence interval segments—you're missing the actual subject-level Spaghetti lines (the defining feature of this plot type), plus the vertical segments aren't the most intuitive way to show uncertainty. Let's adjust this to get exactly what you want: each visit facet will display every subject's concentration-time line (grouped by treatment sequence), overlaid with sequence-specific mean lines and shaded 95% confidence interval bands.

Step-by-Step Solution

First, let's break down the components we need to include:

  • Subject-level Spaghetti lines for each TRTSEQP
  • Clear sequence-specific mean lines
  • Shaded 95% CI bands (a far more readable alternative to vertical segments)

Here's the revised code, with explanations for each part:

# First, convert TRTSEQP to a factor for consistent coloring/legend behavior
CI_spag <- CI_spag() %>%
  mutate(TRTSEQP = as.factor(TRTSEQP))

ggplot(data = CI_spag, aes(x = LBTPTNUM)) +
  # 1. Plot subject-level Spaghetti lines (lightened to avoid overpowering means)
  geom_line(aes(y = [INDIVIDUAL_CONC_COLUMN], color = TRTSEQP, group = USUBJID),
            alpha = 0.3) +  # Lower alpha keeps focus on mean lines
  # 2. Add sequence-specific mean lines (thicker, distinct linetypes)
  geom_line(aes(y = mean, color = TRTSEQP, linetype = TRTSEQP),
            linewidth = 1) +  # Bolder line for mean visibility
  # 3. Add shaded 95% confidence interval bands
  geom_ribbon(aes(ymin = lower, ymax = upper, fill = TRTSEQP),
              alpha = 0.2) +  # Transparent fill so underlying lines show through
  # Facet by visit number as you originally intended
  facet_grid(~ VISITNUM, labeller = label_both) +
  # Customize labels and theme for clarity
  labs(x = "Time Point", y = "Concentration",
       color = "Treatment Sequence", fill = "Treatment Sequence",
       linetype = "Treatment Sequence") +
  theme_minimal()

Critical Notes:

  • Replace [INDIVIDUAL_CONC_COLUMN] with the actual column name in your data that holds raw subject-level concentration values (you mentioned mean is the aggregated value, so you need this raw column to draw Spaghetti lines).
  • We use alpha to adjust transparency: individual lines and CI bands are muted so the mean lines remain the focal point.
  • geom_ribbon is vastly better for showing continuous confidence intervals than geom_segment—it creates a shaded band that's directly tied to the corresponding mean line, making the plot much easier to interpret.
  • All aesthetic mappings are tied to TRTSEQP to keep color/fill/linetype consistent across layers, reducing confusion for readers.

Troubleshooting Tips:

  • If your individual lines look disconnected or jumbled, make sure your data is sorted by USUBJID, TRTSEQP, VISITNUM, and LBTPTNUM first (use dplyr::arrange() to fix this).
  • If CI bands don't align with mean lines, double-check that your mean, lower, and upper values are correctly aggregated by TRTSEQP, VISITNUM, and LBTPTNUM.

内容的提问来源于stack exchange,提问作者S.Gradit

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:33:18