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

如何保留pre/post箱线图并为week0-week8添加geom_smooth

解决方案

核心思路是拆分数据分别处理:用原因子型timepoint绘制before treatment组的箱线图,同时用连续型的numeric.timepoint为A-D组拟合平滑曲线,最后通过手动设置x轴刻度来保留原标签样式。

步骤1:确保numeric.timepoint与原timepoint一一对应

先确认数据集里每个时间点都有唯一的数值映射(如果还没生成,可通过case_when手动指定):

library(dplyr)
library(ggplot2)

# 假设数据集名为df,补充numeric.timepoint列
df <- df %>%
  mutate(numeric.timepoint = case_when(
    timepoint == "pre" ~ 0,
    timepoint == "post" ~ 1,
    timepoint == "week0" ~ 2,
    timepoint == "week1" ~ 3,
    timepoint == "week2" ~ 4,
    timepoint == "week3" ~ 5,
    timepoint == "week4" ~ 6,
    timepoint == "week5" ~ 7,
    timepoint == "week6" ~ 8,
    timepoint == "week7" ~ 9,
    timepoint == "week8" ~ 10
  ))

步骤2:绘制混合图表(箱线图+平滑曲线)

通过分层绘制实现需求,同时保留原x轴样式:

ggplot() +
  # 绘制before treatment组的pre/post箱线图
  geom_boxplot(
    data = df %>% filter(group == "before treatment"),
    aes(x = timepoint, y = value),
    fill = "gray80"
  ) +
  # 绘制A-D组的week0-week8箱线图(可选,不需要可删除)
  geom_boxplot(
    data = df %>% filter(group != "before treatment"),
    aes(x = timepoint, y = value, color = group)
  ) +
  # 为A-D组添加平滑曲线,用连续型numeric.timepoint作为x轴
  geom_smooth(
    data = df %>% filter(group != "before treatment"),
    aes(x = numeric.timepoint, y = value, color = group),
    method = "loess", # 可替换为"lm"等拟合方法
    se = TRUE, # 显示置信区间,不需要则设为FALSE
    linewidth = 1
  ) +
  # 手动设置x轴刻度:用numeric.timepoint的数值定位,显示原timepoint标签
  scale_x_continuous(
    breaks = df %>% distinct(timepoint, numeric.timepoint) %>% arrange(numeric.timepoint) %>% pull(numeric.timepoint),
    labels = df %>% distinct(timepoint, numeric.timepoint) %>% arrange(numeric.timepoint) %>% pull(timepoint)
  ) +
  # 调整图表标签和主题
  labs(
    x = "Timepoint",
    y = "Measurement Value",
    color = "Treatment Group"
  ) +
  theme_bw()

关键说明

  1. 数据拆分:用filter分别提取before treatment组和A-D组数据,避免因子x轴与连续x轴的冲突
  2. 平滑曲线适配:geom_smooth使用连续型的numeric.timepoint作为x轴,解决因子类型无法拟合的问题,同时通过group = group确保每个处理组生成独立曲线
  3. x轴样式保留:scale_x_continuous通过breaks和labels参数,将连续型x轴的刻度位置与原因子标签一一对应,完美还原需要的显示效果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 16:40:16