Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send arrows which cover other labels to the back in geom_label_repel?

Tags:

r

ggplot2

ggrepel

This should seem fairly straight forward but I can't find any argument to do this with ggrepel::geom_label_repel().

Sample of data:

df <- structure(list(Athletename = c("Aries Merritt", "Damian Warner"
), Score = c(12.8, 13.44), Event = c("110m hurdles", "110m hurdles"
), Points = c(1135, 1048), Record = c("World Record", "Decathlon Record"
), score_and_points = c("12.8s, 1135pts", "13.44s, 1048pts")), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Athletename", 
"Score", "Event", "Points", "Record", "score_and_points"))

ggplot2 code:

ggplot(data = data.frame(x = 0), mapping = aes(x = x)) +
  geom_point(data = df, aes(x=Score, y=Points, colour=Record)) +
  geom_label_repel(data = df, 
                   aes(x=Score, y=Points, label = Athletename), 
                   direction = "x",
                   nudge_x = -10) +
  geom_label_repel(data = df, 
                   aes(x=Score, y=Points, label = score_and_points), 
                   direction = "y",
                   nudge_y = -200) +
  scale_y_continuous(name = "Points", 
                     breaks = seq(0,1500,100),
                     limits = c(0,1500)) +
  scale_x_reverse(name = "110m hurdles time (m)",
                     breaks = seq(29,12,-1),
                     limits=c(29,12)) +
  theme(legend.title = element_blank(), legend.position = "top")

enter image description here

like image 744
Nautica Avatar asked Oct 15 '18 20:10

Nautica


1 Answers

Hacky but works: add a copy of the geom_label_repel call, but with the addition of segment.alpha = 0. Then all the labels will be on top of all the arrows.

library(ggrepel)
ggplot(data = data.frame(x = 0), mapping = aes(x = x)) +
  geom_point(data = df, aes(x=Score, y=Points, colour=Record)) +
  geom_label_repel(data = df,
                   aes(x=Score, y=Points, label = Athletename),
                   direction = "x",
                   nudge_x = -10) +
  geom_label_repel(data = df, 
                   aes(x=Score, y=Points, label = score_and_points), 
                   direction = "y",
                   nudge_y = -200, ) +
  geom_label_repel(data = df, 
                   aes(x=Score, y=Points, label = score_and_points), 
                   direction = "y", segment.alpha = 0,
                   nudge_y = -200, ) +
  scale_y_continuous(name = "Points", 
                     breaks = seq(0,1500,100),
                     limits = c(0,1500)) +
  scale_x_reverse(name = "110m hurdles time (m)",
                  breaks = seq(29,12,-1),
                  limits=c(29,12)) +
  theme(legend.title = element_blank(), legend.position = "top")

enter image description here

like image 53
Jon Spring Avatar answered Oct 05 '22 22:10

Jon Spring