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

在ggplot2箱线图中用均值线替换中位数线

移除箱线图中位数线,仅保留均值线的解决方案

要实现只显示均值线、彻底去掉箱线图默认的中位数线,只需要在geom_boxplot()里添加控制中位数线的参数即可,修改后的最简代码如下:

ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + 
  geom_boxplot(median.linewidth = 0) +  # 将中位数线宽度设为0,彻底隐藏
  stat_summary(fun=mean, geom="errorbar", 
               mapping = aes(ymin = ..y.., ymax = ..y..),
               width = 0.75, color = "red", size = 1)

你也可以用median.colour = NA替代median.linewidth = 0,把中位数线设为透明色,同样能达到隐藏效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 12:04:51