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

ggplot2分组箱线图无法按不同迭代次数分离分组求助

ggplot2分组箱线图问题修复方案

问题核心

需要实现每个迭代次数(Iterations)对应7个不同粒子数(Particles)的分组箱线图,之前遇到两个关键错误:

  • 离散化x轴时,因factor(Iterations)与xlim(5,250)的连续刻度冲突,报错Error: Discrete value supplied to continuous scale
  • 用连续x轴时,因分组逻辑错误,position_dodge()无法识别独立分组,报错position_dodge() requires non-overlapping x intervals,箱线图未按粒子数分组

错误原因与修复思路

  1. x轴类型冲突:离散型x轴(factor(Iterations))不能搭配连续型刻度限制(xlim()),需统一用scale_x_continuous()(连续型)或直接保留离散型映射
  2. 分组逻辑错误:仅用group=Particles无法区分同一迭代次数下的不同粒子数分组,需用interaction(Iterations, Particles)明确每个(迭代次数+粒子数)的组合为独立分组

修正代码(连续x轴版本)

library(ggplot2)
library(latex2exp)
library(ggthemes)

# 自定义配色
colour_palette_parts <- c("#ffbe0b", "#ff8e09", "#ff5d07", "#ff2b05", "#ff040e", "#ff023e", "#ff006e")

# 绘图代码
g <- ggplot(logliks, aes(x = Iterations, y = llh, 
                         group = interaction(Iterations, Particles), 
                         fill = factor(Particles))) +
  geom_boxplot(position = position_dodge(width = 10)) +  # 根据Iterations间隔调整width值,确保同组箱线图不重叠
  ylim(-4000, -400) +
  scale_x_continuous(limits = c(5, 250)) +  # 替代xlim,匹配连续型x轴
  theme(axis.text.x = element_text(angle = 65, vjust = 0.6)) +
  labs(title = "log Likelihoods",
       subtitle = TeX("For one guess of $\\epsilon$ and $\\kappa$ each"),
       caption = "Likelihoods with respect to iterations and particles",
       x = "Iterations",
       y = "log Likelihood",
       fill = "Particles") +
  scale_fill_manual(values = colour_palette_parts) +
  guides(fill = guide_legend(nrow = 2, override.aes = list(size = 6, shape = 20))) +  # 修正图例映射为fill(而非colour)
  theme_bw() +
  themespecs  # 自定义主题

print(g)

修正代码(离散x轴版本)

若希望x轴显示每个迭代次数的离散标签,用此版本:

library(ggplot2)
library(latex2exp)
library(ggthemes)

colour_palette_parts <- c("#ffbe0b", "#ff8e09", "#ff5d07", "#ff2b05", "#ff040e", "#ff023e", "#ff006e")

g <- ggplot(logliks, aes(x = factor(Iterations), y = llh, 
                         group = interaction(factor(Iterations), Particles), 
                         fill = factor(Particles))) +
  geom_boxplot(position = position_dodge(width = 0.8)) +  # 离散x轴常用width=0.8
  ylim(-4000, -400) +
  theme(axis.text.x = element_text(angle = 65, vjust = 0.6)) +
  labs(title = "log Likelihoods",
       subtitle = TeX("For one guess of $\\epsilon$ and $\\kappa$ each"),
       caption = "Likelihoods with respect to iterations and particles",
       x = "Iterations",
       y = "log Likelihood",
       fill = "Particles") +
  scale_fill_manual(values = colour_palette_parts) +
  guides(fill = guide_legend(nrow = 2, override.aes = list(size = 6, shape = 20))) +
  theme_bw() +
  themespecs

print(g)

关键改动说明

  • 分组逻辑:interaction(Iterations, Particles)明确每个迭代次数与粒子数的组合为独立分组,让position_dodge()正确识别箱线图位置
  • x轴处理:连续x轴用scale_x_continuous()替代xlim(),避免类型冲突;离散x轴直接用factor(Iterations)映射,无需额外刻度限制
  • 图例修正:将guides(colour=...)改为guides(fill=...),匹配fill的图例映射,确保图例正常显示
  • 间距调整:根据x轴类型调整position_dodge(width)值,确保同迭代次数下的箱线图紧密排列且不重叠

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 10:35:38