求助:绘制按序列和访视分组带95%置信区间的Spaghetti图
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 mentionedmeanis the aggregated value, so you need this raw column to draw Spaghetti lines). - We use
alphato adjust transparency: individual lines and CI bands are muted so the mean lines remain the focal point. geom_ribbonis vastly better for showing continuous confidence intervals thangeom_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
TRTSEQPto 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, andLBTPTNUMfirst (usedplyr::arrange()to fix this). - If CI bands don't align with mean lines, double-check that your
mean,lower, anduppervalues are correctly aggregated byTRTSEQP,VISITNUM, andLBTPTNUM.
内容的提问来源于stack exchange,提问作者S.Gradit

