rm(list = ls())
library(ggrepel)
## Loading required package: ggplot2
#Overview
#ggrepel provides geoms for ggplot2 to repel overlapping text labels:
#geom_text_repel()
#geom_label_repel()
set.seed(42)
dat <- subset(mtcars, wt > 2.75 & wt < 3.45)
dat$car <- rownames(dat)
head(dat)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## car
## Mazda RX4 Wag Mazda RX4 Wag
## Hornet 4 Drive Hornet 4 Drive
## Hornet Sportabout Hornet Sportabout
## Merc 240D Merc 240D
## Merc 230 Merc 230
## Merc 280 Merc 280
p <- ggplot(dat, aes(wt, mpg, label = car)) +
geom_point(color = "red")
p

p1 <- p + geom_text() + labs(title = "geom_text()")
p2 <- p + geom_text_repel() + labs(title = "geom_text_repel()")
gridExtra::grid.arrange(p1, p2, ncol = 2)

#Examples
#1
#1.1 Hide some of the labels
#Set labels to the empty string "" to hide them. All data points repel the non-empty labels.
set.seed(42)
dat2 <- subset(mtcars, wt > 3 & wt < 4)
# Hide all of the text labels.
dat2$car <- ""
head(dat2)
## mpg cyl disp hp drat wt qsec vs am gear carb car
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
# Let's just label these items.
ix_label <- c(2,3,16)
dat2$car[ix_label] <- rownames(dat2)[ix_label]
head(dat2)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## car
## Hornet 4 Drive
## Hornet Sportabout Hornet Sportabout
## Valiant Valiant
## Duster 360
## Merc 240D
## Merc 230
ggplot(dat2, aes(wt, mpg, label = car)) +
geom_point(color = ifelse(dat2$car == "", "grey50", "red")) +
geom_text_repel()

#1.2 Do not repel labels from data points
#Set point.padding = NA to prevent label repulsion away from data points.
#Now labels move away from each other and away from the edges of the plot.
head(dat)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## car
## Mazda RX4 Wag Mazda RX4 Wag
## Hornet 4 Drive Hornet 4 Drive
## Hornet Sportabout Hornet Sportabout
## Merc 240D Merc 240D
## Merc 230 Merc 230
## Merc 280 Merc 280
ggplot(dat, aes(wt, mpg, label = car)) +
geom_point(color = "red") +
geom_text_repel(point.padding = NA)

?geom_text_repel
## starting httpd help server ...
## done
#1.3 Limit labels to a specific area
#Use options xlim and ylim to constrain the labels to a specific area.
#Limits are specified in data coordinates. Use NA when there is no lower or upper bound in a particular direction.
#Here we also use grid::arrow() to render the segments as arrows.
# All labels should be to the right of 3.
x_limits <- c(3, NA)
ggplot(dat, aes(wt, mpg, label = car, color = factor(cyl))) +
geom_vline(xintercept = x_limits, linetype = 3) +
geom_point() +
geom_label_repel(
arrow = arrow(length = unit(0.03, "npc"), type = "closed", ends = "first"),
force = 10,
xlim = x_limits
) +
scale_color_discrete(name = "cyl")
## Warning: Removed 1 rows containing missing values (geom_vline).

#1.4 Align labels on the top or bottom edge
#Use hjust or vjust to justify the text neatly:
#hjust = 0 for left-align
#hjust = 0.5 for center
#hjust = 1 for right-align
ggplot(mtcars, aes(x = wt, y = 1, label = rownames(mtcars))) +
geom_point(color = "red") +
geom_text_repel(
nudge_y = 0.05,
direction = "x",
angle = 90,
vjust = 0,
segment.size = 0.2
) +
xlim(1, 6) +
ylim(1, 0.8) +
theme(
axis.line.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank()
)

################REF
#https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html