ggplot2 geom_label() in R: Boxed Text Labels
The geom_label() function in ggplot2 places text labels with a COLORED BACKGROUND BOX, useful for high-contrast labels over busy plots. It is the boxed version of geom_text().
ggplot(df, aes(x, y, label = name)) + geom_label() geom_label(fill = "yellow", color = "black") geom_label(label.padding = unit(0.5, "lines")) geom_label(label.r = unit(0.3, "lines")) # rounded corners ggrepel::geom_label_repel() # auto-adjust
Need explanation? Read on for examples and pitfalls.
What geom_label() does in one sentence
geom_label() is geom_text() with a filled background box, providing visual contrast against busy plots. Useful when label readability matters.
Syntax
geom_label(label.padding = unit(0.25, "lines"), label.r = unit(0.15, "lines"), label.size = 0.25, ...). Box-specific arguments plus all geom_text args.
geom_label is best for important annotations that must stand out. Use sparingly; many labels with boxes clutter the plot.Five common patterns
1. Standard boxed label
2. Custom fill
3. Conditional labels
4. ggrepel for non-overlapping
5. Rounded corners
geom_label when contrast matters; use geom_text when minimal styling is fine. geom_label adds visual weight; too many boxes clutter.geom_label() vs geom_text() vs ggrepel
| Function | Box | Avoid overlap |
|---|---|---|
geom_label() |
Yes | No |
geom_text() |
No | No |
geom_label_repel() |
Yes | Yes |
geom_text_repel() |
No | Yes |
For many labels, prefer the repel variants.
A practical workflow
Use geom_label sparingly for "must-see" annotations.
Only the high-mpg cars get boxed labels.
Common pitfalls
Pitfall 1: too many labels. geom_label adds visual weight; many boxes clutter the plot. Use sparingly or switch to geom_text.
Pitfall 2: positioning. Like geom_text, hjust/vjust position the box relative to (x, y).
geom_label covers underlying points and lines. If a box overlaps a point, you can't see the point. Use alpha or carefully position.Try it yourself
Try it: Plot mpg vs wt with boxed labels for cars where mpg > 28. Save to ex_plot.
Click to reveal solution
Explanation: Filter to high-mpg cars; geom_label highlights them with yellow boxes.
Related ggplot2 functions
After mastering geom_label, look at:
geom_text(): plain textggrepel::geom_label_repel(): anti-overlap boxed labelsannotate("label", ...): one-off labelsgeom_text_repel(): anti-overlap plain
FAQ
What does geom_label do in ggplot2?
geom_label() places text labels with a colored background box at (x, y) coordinates.
What is the difference between geom_label and geom_text?
geom_label adds a filled box; geom_text is plain. Use label for contrast over busy backgrounds.
How do I prevent labels from overlapping?
Use ggrepel::geom_label_repel(). It auto-shifts labels to avoid collisions.
Can I customize the box appearance?
Yes. label.padding controls inside spacing; label.r controls corner rounding; fill sets background color.
Should I use geom_label or geom_text?
label for IMPORTANT must-read annotations. text for minimal styling. Many labels with boxes look cluttered; use sparingly.