ggrepel package in R is an extension of the ggplot2
package, designed to simplify the process of avoiding overlapped
texts in plots.
It offers 2 main functions:
✍️ author → Kamil Slowikowski
📘 documentation → github
⭐️ more than 1000 stars on github
To get started with
ggrepel, you can install it directly
from CRAN using the
ggrepel package allows you to display labels using a
single geom and invert the functions
geom_label_repel() without constraint:
Here’s a basic example:
You can labels on individual data points with the
geom_label_repel() function. All points will not be
displayed if there is not enough room for them, but you can force it
with the max.overlaps argument (check below).
The easiest way to display only some labels is to create a new column on your dataframe with a non-empty value only for the observations you’re interested in.
# Create a new columns with the label only for row 2,3 and 14 mtcars$car = "" idx_to_label = c(2, 20, 14) mtcars$car[idx_to_label] = rownames(mtcars)[idx_to_label] # Display the result ggplot(mtcars, aes(wt, mpg, label = car)) + geom_text_repel() + geom_point(color = ifelse(mtcars$car == "", "grey50", "red"))
geoms provided by
ggrepel try do avoid
as much as possible overlapping, with a maximum of 10 by default.
However, you can change this value if overlapping is not a problem for
you with the
Parallel coordinates plot are easy to create with
ggrepel combined with the
function from ggplot2
library(hrbrthemes) df = data.frame(x1 = 1, y1 = rnorm(10), x2 = 2, y2 = rnorm(10), lab = state.name[1:10]) ggplot(df, aes(x1, y1, xend = x2, yend = y2, label = lab, col = lab)) + geom_segment(size = 0.5) + guides(color = "none") + # remove legend theme_bw() + # change background color and overall theme geom_text_repel(nudge_x = -0.2, direction = "y", hjust = "right") + geom_text_repel(aes(x2, y2), nudge_x = 0.1, direction = "y", hjust = "left")
Thanks to the
bg.r, you can change colors of your labels.