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

如何调整ggplot2柱状图y轴刻度范围以完整显示误差棒

ggplot2分组柱状图误差棒截断修复方案

问题表现

使用ggplot2配合ggbreak包绘制带断轴的分组柱状图时,第二组柱子的误差棒显示不完整,被坐标轴截断,要求完全保留原有图表样式完成修复。
原错误绘图效果:
原错误效果柱状图

问题原因

  • 代码重复在geom_col和geom_errorbar中定义x轴映射,和全局aes映射冲突,存在分组错位风险
  • 柱状图默认的dodge避让宽度和误差棒设置的position_dodge(0.9)不匹配,易出现对齐偏差
  • 同时调用scale_y_cut()和scale_y_continuous()造成y轴标度冲突,且断轴各分段的坐标轴范围没有预留足够空间,跨断轴位置的误差棒被边界裁剪
  • 原代码theme配置末尾存在冗余逗号,存在语法报错隐患

修复操作

  • 移除geom层中重复的x轴映射定义,直接继承全局aes配置
  • 将柱状图的position参数统一设置为position_dodge(0.9),和误差棒的避让宽度完全一致,保证元素对齐
  • 删除单独调用的scale_y_continuous(),将y轴断点、刻度、范围扩展配置全部整合到scale_y_cut()中,通过expand参数给每个断轴分段预留上下边距,确保所有误差棒的上下限都落在绘图区域内
  • 删除theme末尾的冗余逗号,消除语法报错

修复后完整代码

# 需提前加载依赖包
library(ggplot2)
library(ggbreak)

ggplot(data, aes(x = reorder(Treatment, -mean), y = mean, fill = Grapevine)) +
  geom_col(
    stat = "identity",
    position = position_dodge(0.9)
  ) +
  geom_errorbar(aes(
    ymin = mean - se,
    ymax = mean + se
  ),
  width = 0.1,
  position = position_dodge(0.9)
  ) +
  scale_y_cut(
    breaks = c(2.0E-09, 2.0E-08, 1.0E-07),
    scales = list(
      scale_y_continuous(breaks = c(0, 6.0e-09), expand = expansion(mult = c(0, 0.12))),
      scale_y_continuous(breaks = c(6.0e-08), expand = expansion(mult = c(0.1, 0.1))),
      scale_y_continuous(breaks = c(6.0e-07), expand = expansion(mult = c(0.1, 0.15)))
    )
  ) +
  theme(
    line = element_line(
      colour = "black", size = 1,
      linetype = 1, lineend = "butt"
    ),
    rect = element_rect(
      fill = "white", colour = "black",
      size = 1, linetype = 1
    ),
    aspect.ratio = 0.6,
    plot.background = element_rect(fill = "white"),
    plot.margin = margin(1, 1, 1, 1, "cm"),
    axis.text = element_text(size = rel(2.5), colour = "#000000", margin = 1),
    strip.text = element_text(size = rel(0.8)),
    axis.line = element_blank(),
    axis.text.x = element_text(vjust = 0.2, angle = 90),
    axis.text.y = element_text(hjust = 1),
    axis.ticks = element_line(colour = "#000000", size = 1.2),
    axis.title.x = element_text(size = 30, vjust = 0.5),
    axis.title.y = element_text(size = 30, angle = 90),
    axis.ticks.length = unit(0.1, "cm"),
    legend.background = element_rect(colour = NA),
    legend.margin = unit(0.15, "cm"),
    legend.key = element_rect(fill = "grey95", colour = "white"),
    legend.key.size = unit(1.2, "lines"),
    legend.key.height = NULL,
    legend.key.width = NULL,
    legend.text = element_text(size = rel(2.0)),
    legend.text.align = NULL,
    legend.title = element_text(size = rel(2.0), face = "bold", hjust = 0),
    legend.title.align = NULL,
    legend.position = "right",
    legend.direction = NULL,
    legend.justification = "right",
    legend.box = NULL,
    panel.background = element_rect(
      fill = "#ffffff", colour = "#000000",
      size = 2, linetype = "solid"
    ),
    panel.border = element_rect(colour = "black", fill = NA, size = 2)
  ) +
  ylab(expression(Lp[r] ~ (m ~ s^-1 ~ Mpa^-1))) +
  xlab(expression(Treatment))

注:如果运行后仍有个别误差棒显示不全,可对应调整scale_y_cut里各分段expand参数的数值,适当增大边距即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 04:48:16