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

ggplot中geom_text精准定位及geom_text_repel报错问题求助

Fixing the geom_text_repel Error & Achieving Your Desired Label Layout

Hey there! Let's work through your problem step by step—first fixing that annoying error, then getting your label layout exactly how you want it.


First: Why the Error Happens

That Incorrect number of arguments (11), expecting 9 for '_ggrepel_repel_boxes' error is a classic version mismatch issue. Your ggrepel package is out of date and doesn't play nice with your current ggplot2 version. Newer ggplot2 added extra parameters that older ggrepel doesn't recognize, causing the function call to fail.

Fix the Error

Update your ggrepel package first—this should resolve the parameter mismatch:

install.packages("ggrepel")

If you still run into issues after that, update ggplot2 too to ensure everything is in sync:

install.packages("ggplot2")

Next: Get Your Ideal Label Layout

Once the packages are updated, we can tweak geom_text_repel parameters to create that clean, non-overlapping layout you want: labels stay aligned when they don't overlap, shift vertically only when needed, and keep gaps minimal.

Adjusted Full Code

library(ggplot2)
library(ggrepel)

Data <- data.frame(
  Group=c("Group 2","Group 1","Group 3","Group 2","Group 5","Group 4","Group 6", "Group 7","Group 4","Group 3","Group 1","Group 5","Group 6","Group 7", "Group 2","Group 4","Group 6","Group 7","Group 3","Group 1"),
  Fruit=c("apple","apple","apple","mango","apple","apple","apple","apple","mango","mango", "mango","mango","mango","mango","orange","orange","orange","orange","orange", "orange"),
  Percentage=c(68.46846847,77.35849057,72.72727273,26.12612613,76.31578947,62.79069767, 71.05263158,69.23076923,30.23255814,25,20.75471698,23.68421053,23.68421053, 23.07692308,5.405405405,6.976744186,5.263157895,7.692307692,2.272727273, 1.886792453)
)

ggplot(Data, aes(x=reorder(Fruit, Percentage), y=Percentage, color=Group, label=paste0(round(Percentage),"%"))) +
  geom_point(size=4) +
  coord_flip() +
  scale_y_continuous(limits=c(0,100)) +
  theme(
    panel.grid.major.y=element_line(color="gray90",size = 0.7),
    panel.background=element_blank(),
    strip.background=element_blank(),
    panel.border=element_rect(color="black",fill=NA,size=1)
  ) +
  labs(x="", y="% of people", color="") +
  geom_text_repel(
    hjust = 0,                # Align labels to the right of their points
    direction = "y",          # Only shift labels vertically (along the Fruit axis)
    force = 0.8,              # Reduce gaps between labels (lower = tighter spacing)
    box.padding = 0.2,        # Tighten space between label and its point
    point.padding = 0.1,
    segment.size = 0.3,       # Thin down connecting lines (optional tweak)
    show.legend = FALSE       # Keep legend clean (labels don't belong here)
  )

Key Parameter Breakdown:

  • direction = "y": This is the most important setting—it restricts label movement to only the vertical (Fruit) axis, so labels stay aligned with their point's percentage value unless they overlap.
  • force = 0.8: Controls how strongly labels repel each other. Lower numbers mean smaller gaps between overlapping labels (adjust this if you want even tighter spacing).
  • hjust = 0: Makes labels left-aligned, sitting neatly to the right of their corresponding points.
  • show.legend = FALSE: Prevents label markers from cluttering up your color legend.

This code will produce a layout that matches your desired output: clean, non-overlapping labels that stay aligned when possible, and shift only as needed to avoid overlap.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:28:19