Avoid overlapping labels in ggplot2 charts

January 11, 2016
By

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

If you've ever created a scatterplot with text labels using the text function in R, or the geom_text function in the ggplot2 package, you've probably found that the text labels can easily overlap, rendering some of them unreadable. Now, thanks to the new extensibility capabilities of the ggplot2 package, R user Kamil Slowikowski has created an R package ggrepel that adds alternative text labeling functions to ggplot2 that "repels" labels from data points and other labels to avoid overlapping. The new geom_text_repel replaces the standard geom_text for plain text lablels, and you can also use geom_label_repel instead of geom_label for these rounded and color-coded labels:

Geom_label_repel

The resulting plot is definitely more attractive, and with more readable lables, than the standard version using geom_text:

Geom_text

You can see more examples of ggrepel in action here. A word of caution, though: if you're relying on the text labels as the fundamental element of your visualization, this does have the effect of moving your data around, and that could change your interpretation of theplot. (Case in point: the spread of data appears greater in the first plot than the "messy" one just above, even though it's the exact same data being presented both times.) But if your main goal is not interpretation, or if you just want to label a few points in particular (and ensure the labels are readable), this new ggrepel package is well worth a look. The ggrepel package is available on CRAN now, and you can follow its development on Github at the link below.

github (slowkow): ggrepel

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)