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

如何将geom_boxplot图层向上微调?

解决ggplot2中箱线图对齐x轴0点的问题

我明白你的需求:想要让箱线图紧贴x轴(0点位置),同时在上方展示缩放后的密度分布。问题出在geom_density用了y=..scaled..将密度值缩放到0-1范围,但默认的箱线图没有固定在y=0的位置,加上分面的自由缩放轴,导致它没有对齐到x轴。

下面是修改后的代码,完美实现你的需求:

library(ggplot2)
library(data.table)

# 加载你的数据
df <- structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("hanehalki", "gsyih"), class = "factor"), value = c(2.92, 3.05, 2.82, 3.26, 2.86, 2.88, 4.86, 4.92, 3.34, 3.56, 3.81, 3.53, 3.41, 2.68, 3.28, 3.23, 3.34, 2.71, 2.98, 3.8, 2.77, 4.09, 3, 3.33, 3.06, 3.4, 3.05, 3.39, 2.98, 3.1, 2.95, 5.39, 3.13, 3.75, 3.12, 4.88, 2.82, 2.99, 3.42, 2.95, 3.41, 3.84, 3.28, 2.83, 3.58, 4.68, 2.76, 4.16, 2.89, 5.03, 41820.1557045057, 29083.9295704606, 31554.2128202566, 58956.7267073248, 41229.086549323, 38757.9798583395, 23327.2168931588, 18708.4485132515, 43749.4317129513, 29105.5044386941, 30235.1079173834, 30422.9283128681, 81228.0951424918, 55608.1583883872, 34800.2529973319, 35914.5488124995, 33982.3005551545, 44302.0289524954, 46529.3034031361, 31898.5500012572, 44088.1553908406, 37633.0337337249, 47287.5078938497, 86798.4429060671, 60631.6357010175, 44210.7976900847, 35742.8253333139, 36110.0565531365, 31586.3538734972, 33771.6554831237, 30267.3076817561, 30192.1075499239, 37314.1420295662, 27322.3774815836, 47001.4874496515, 16727.4244640104, 56462.8833985347, 43783.0846449189, 40892.6265785975, 60553.5509558071, 32228.4494321578, 34105.7208652381, 34644.9683658929, 38715.4350556293, 29966.7509482073, 23645.372107655, 42288.6478926083, 22454.28959872, 37888.643411857, 26591.7884353541)), row.names = c(NA, -100L), class = c("data.table", "data.frame"))

# 修改后的绘图代码
df %>% 
  ggplot(aes(x = value, color = variable)) +
  facet_grid(~variable, scales = 'free') +
  geom_density(aes(y = ..scaled..)) +
  # 将箱线图固定在y=0位置,调整宽度让它紧贴x轴
  geom_boxplot(aes(y = 0, fill = variable), 
               alpha = 0.2, 
               outlier.color = 'black', 
               outlier.shape = 3, 
               outlier.alpha = 1,
               width = 0.1) +
  # 固定y轴范围为0-1,去掉轴扩展空间,确保箱线图完全对齐0点
  scale_y_continuous(limits = c(0, 1), expand = c(0, 0)) +
  # 可选:添加清晰的y轴标签
  labs(y = "Scaled Density")

关键修改说明:

  • 固定箱线图位置:给geom_boxplot添加aes(y = 0),直接把箱线图锚定在x轴(y=0)的位置。
  • 调整箱线图宽度:用width = 0.1控制箱线图在y方向的宽度,避免它挤占密度图的展示空间。
  • 锁定y轴范围:scale_y_continuous(limits = c(0, 1), expand = c(0, 0))确保y轴严格从0到1,并且去掉默认的轴边距,让箱线图完全贴合x轴。

这样修改后,每个分面里的箱线图都会精准对齐x轴0点,同时上方的密度分布也能完整展示,异常值的样式也保留了你原来的设置。

内容的提问来源于stack exchange,提问作者Samet Sökel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 09:52:49