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

ggplot2绘制分类因子x轴小提琴图时如何添加轴断裂体现时间间隔?

方案1:将x轴映射为实际连续时间(最推荐)

你可以直接放弃分类x轴,把测试阶段对应的实际天数作为连续变量映射到x轴,天然就能体现时间间隔差异,还可以直接配合ggbreak做轴断裂,仅需手动指定x轴刻度标签对应原session名称即可。
修改后代码如下:

library(ggbreak)
# 给数据集新增对应实际天数的列
tidied_data <- tidied_data %>%
  mutate(day = case_when(
    Session == "Baseline (Day 1)" ~ 1,
    Session == "Post-training (Day 3)" ~ 3,
    Session == "Follow-up (Day 30)" ~ 30
  ))

tidied_data %>%
  ggplot(aes(x=day, y=Amplitude, fill=Group, group=interaction(day, Group))) + # 增加分组避免小提琴图错位
  geom_violin(position=position_dodge(2), trim=FALSE, width = 1.5) + # 调整宽度适配x轴间距
  geom_jitter(position=position_dodge(2), size=1) +
  stat_summary(fun = "mean", geom = "point", 
               size = 3, position=position_dodge(2), color="white") +
  stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width=0.3, position=position_dodge(2), color="white") +
  scale_x_continuous(breaks = c(1,3,30), 
                     labels = c('Baseline (Day 1)', 'Post-training (Day 3)', 'Follow-up (Day 30)')) +
  scale_x_break(c(4,28)) + # 直接添加x轴断裂,切掉3天到30天之间的空白区域
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + 
  theme(panel.border = element_blank()) +  
  theme(axis.line = element_line(colour = "black")) + 
  labs(title = "Group x Session", 
       x = "Session", 
       y = "Amplitude")

该方案的优势是完全符合实际时间逻辑,读者可以直观感知到后两个测试点的时间间隔远大于前两个。

方案2:保留分类轴,手动拉大间距加断裂标记

如果你一定要使用分类因子x轴,可以通过插入空白因子水平的方式拉大不同阶段的间隔,再手动添加轴断裂的视觉标记:

# 给Session因子插入空白水平,拉大随访阶段和前两个阶段的间距
level_order <- factor(tidied_data$Session, 
                      level = c('Baseline (Day 1)', 'Post-training (Day 3)', 
                                rep(" ", 5), # 调整空白水平的数量可以控制间距大小
                                'Follow-up (Day 30)'))

p <- tidied_data %>%
  ggplot(aes(x=level_order, y=Amplitude, fill=Group)) +
  geom_violin(position=position_dodge(1), trim=FALSE) +
  geom_jitter(binaxis='y', stackdir='center',
              position=position_dodge(1)) +
  stat_summary(fun = "mean", geom = "point", 
               size = 3, position=position_dodge(1), color="white") +
  stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width=0.3, position=position_dodge(1), color="white") +
  scale_x_discrete(breaks = c('Baseline (Day 1)', 'Post-training (Day 3)', 'Follow-up (Day 30)'), # 隐藏空白水平的刻度
                   drop = FALSE) + # 保留空水平维持间距
  # 手动添加轴断裂的视觉标记
  annotate("segment", x = 3.2, xend = 3.3, y = min(tidied_data$Amplitude)*0.95, yend = min(tidied_data$Amplitude)*0.98, color = "black") +
  annotate("segment", x = 3.3, xend = 3.4, y = min(tidied_data$Amplitude)*0.95, yend = min(tidied_data$Amplitude)*0.92, color = "black") +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + 
  theme(panel.border = element_blank()) +  
  theme(axis.line = element_line(colour = "black")) + 
  labs(title = "Group x Session", 
       x = "Session", 
       y = "Amplitude")
print(p)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 15:24:01