Repulsive Text and Label Geoms for 'ggplot2'

Provides text and label geoms for 'ggplot2' that help to avoid overlapping text labels. Labels repel away from each other and away from the data points.


ggrepel provides geoms for ggplot2 to repel overlapping text labels.

library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(wt, mpg)) +
  geom_point(color = 'red') +
  geom_text_repel(aes(label = rownames(mtcars))) +
  theme_classic(base_size = 16)

See the vignette for more usage examples.

Also, look at the help pages:

?geom_text_repel
?geom_label_repel

Install the latest stable release from CRAN:

install.packages("ggrepel")

Alternatively, install the latest development version from github:

install.packages("devtools")
devtools::install_github("slowkow/ggrepel")

Or install a tagged version:

devtools::install_github("slowkow/ggrepel@0.6.2")

Please submit an issue to report bugs or ask questions.

Please contribute bug fixes or new features with a pull request to this repository.

directlabels

statistical practice by making available a body of useful functions that make direct labeling of common plots easy to do with high-level plotting systems such as lattice and ggplot2. The main function that the package provides is direct.label(p), which takes a lattice or ggplot2 plot p and adds direct labels.

wordcloud

Pretty word clouds.

The wordcloud package implements a spiraling algorithm to prevent text labels from overlapping each other.

FField

Force field simulation of interaction of set of points. Very useful for placing text labels on graphs, such as scatterplots.

I found that functions in the FField package were not ideal for repelling overlapping rectangles, so I wrote my own.

See this gist for examples of how to use the wordcloud and FField packages with ggplot2.

News

CHANGES (thanks to @jiho)

  • changed alpha in geom_label_repel() to control text, label background, label border, and segment.

  • Allow segment.colour as well as segment.color.

  • By default, map text color and text alpha to the segment color unless they are overridden.

FIXES (thanks to @jiho)

  • Call scales::alpha() instead of alpha().

FIXES

  • Fix a bug that caused ggrepel to fail on polar coordinates coord_polar(). See issue 56.

NEW FEATURES

  • Use point.padding=NA to ignore data points in repulsion calculations.

FIXES

  • Stop the labels from escaping the plot boundaries instead of applying a force at the boundary.

  • Call set.seed within geom_text_repel() and geom_label_repel() to allow recreating identical plots. Fixes issue 33.

NEW FEATURES

  • Add min.segment.length to geom_text_repel() and geom_label_repel().

CHANGES

  • Tweak repel_boxes.cpp. Dampen forces to tune how the labels move. The result looks better, at least for the examples in the vignette.

NEW FEATURES

  • Do not draw labels with empty strings. When a label is an empty string, the text will not be shown, the segment will not be drawn, but the corresponding data point will repel other labels. See issue 51.
  • Add segment.alpha as an option for geom_text_repel() and geom_label_repel().

  • Implement angle aesthetic for geom_text_repel(), the same way as done in ggplot2 geom_text().

CHANGES

  • Move nudge_x and nudge_y out of the aesthetics function aes(). This makes ggrepel consistent with ggplot2 functions geom_text() and geom_label(). Backwards incompatible with 0.5.1.

  • Restore segment.color as an option for geom_text_repel() and geom_label_repel().

  • Tweak repel_boxes.cpp. Do not weight repulsion force by ratios of bounding box heights and widths. This seems to perform better, especially after rotating text labels.

  • Optimize C++ code further by reducing number of calls to rnorm().
  • First push to CRAN.

CHANGES

  • Tweak point.padding so that users can configure how far labels are pushed away from data points.

CHANGES

  • Optimize C++ code for a 2.5X speed improvment.

  • Delete unnecessary .Rd files.

FIXES

  • Fix the bug when the line segment from the data point points to the origin at (0,0) instead of the text label.

CHANGES

  • Automatically recompute repulsion between labels after resizing the plot.

CHANGES

  • Change distance between segment and label in geom_label_repel(). Now there is no gap between the end of the segment and the label border.

FIXES

  • Fix spring_force() so that it never returns NaN.

CHANGES

  • Add nudge_x and nudge_y to better control positioning of labels.

CHANGES

  • Add arrow parameter to allow plotting arrows that point to the labeled data points rather than plain line segments.

  • Always draw segments, even if the labeled point is very close to the label.

FIXES

  • Fix point.padding so that horizontal and vertical padding is calculated correctly.

  • Tweak forces to improve layout near borders and in crowded areas.

FIXES

  • Fix issue 7. Labels can now be placed anywhere in the plotting area instead of being limited to the x and y ranges of their corresponding data points.
  • Fix DESCRIPTION to require ggplot2 >= 2.0.0

CHANGES

  • Add new parameter point.padding to add padding around the labeled points. The line segment will stop before reaching the coordinates of the point. The text labels are also now padded from the line segment to improve legibility.

  • Add volcano plot to the vignette usage examples.

  • Add Travis continuous integration to test against R-devel, R-release, and R-oldrel.

  • Dampen repulsion force to slightly improve algorithm efficiency.

  • Move intersect_line_rectangle() to src/repel_boxes.cpp.

CHANGES

  • Remove unused imports: colorspace.

  • Update NAMESPACE with new version of roxygen.

  • Use spring force to attract each label to its own point.

  • Change default maximum iterations from 10,000 to 2000.

  • Update man pages.

  • Remove unused code.

CHANGES

  • Update geom_text_repel() and geom_label_repel().

    • Change label.padding to box.padding.

    • Remove unsupported parameters:

      • position
      • nudge_x
      • nudge_y
      • hjust
      • vjust
  • Remove unused imports.

DOCUMENTATION

  • Add roxygen docs to all functions.

NEW FEATURES

  • Add geom_label_repel().

  • Add fudge width to help with legends.

  • Add expand=TRUE to allow text to be placed in the expanded plot area.

  • Add man/ folder.

  • Add links to ggplot2 docs in vignette.

MINOR FEATURES

  • Add unused R implementation of repel_boxes(), just for your reference.
  • Initial release to github.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("ggrepel")

0.6.5 by Kamil Slowikowski, 6 months ago


http://github.com/slowkow/ggrepel


Report a bug at http://github.com/slowkow/ggrepel/issues


Browse source code at https://github.com/cran/ggrepel


Authors: Kamil Slowikowski [aut, cre], Jean-Olivier Irisson [ctb]


Documentation:   PDF Manual  


GPL-3 | file LICENSE license


Imports grid, Rcpp, scales

Depends on ggplot2

Suggests knitr, rmarkdown

Linking to Rcpp


Imported by MEGENA, NetworkInference, clustrd, corrr, factoextra, gMOIP, gghalfnorm, ggnetwork, ggpubr, ggraph, ggspectra, idm, soc.ca, starmie, userfriendlyscience.

Suggested by geomnet, ggfortify, ggpmisc, grattan, sjPlot.


See at CRAN