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

R语言使用ggplot绘制dotplot时出现双点问题如何修复

R ggplot2 geom_dotplot边缘重复双点问题修复方案

问题原因

你在代码中同时使用geom_dotplot()的内置y轴分箱堆叠逻辑和position_jitter()的高度偏移参数,二者产生冲突:geom_dotplot()会先根据binwidth参数对y轴的Length数值做分箱,自动排列同个分箱内的点位置。额外添加的position_jitter(height = 0.2)会让边缘分箱的点在y轴方向偏移,落到相邻分箱的判定范围内,被识别为两个分箱的点重复绘制,最终表现为边缘偏大的双点。

可选用的修复方案

  • 方案1:移除position_jitter参数,保留geom_dotplot原生堆叠效果,彻底解决分箱冲突
    修正后代码:
    ggplot(AM138_141, aes(x=Worm_Names, y=Length))+ 
      geom_boxplot()+
      geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 1, dotsize = 1, binwidth = 1) +
      theme_classic()
    
  • 方案2:如果需要保留横向抖动避免点堆叠过密,仅将position_jitter的height参数设为0,关闭y轴方向的偏移,避免跨分箱重复判定
    修正后代码:
    ggplot(AM138_141, aes(x=Worm_Names, y=Length))+ 
      geom_boxplot()+
      geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 1, dotsize = 1, position = position_jitter(width = 0.2, height = 0), binwidth = 1) +
      theme_classic()
    
  • 方案3:如果需要更自然的点分散效果,改用ggbeeswarm包的geom_beeswarm(),该函数自带点分散逻辑,不会出现分箱重复绘制问题
    先安装依赖包:install.packages("ggbeeswarm")
    修正后代码:
    library(ggbeeswarm)
    ggplot(AM138_141, aes(x=Worm_Names, y=Length))+ 
      geom_boxplot(width = 0.5)+
      geom_beeswarm(cex = 2, size = 1) +
      theme_classic()
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 11:54:04