I am trying to use multiple line labels in ggrepel::geom_label_repel
. It seems like the geom_label_repel
wants to center all text - whereas I want to flush it left. hjust = 0
does what I want with geom_label
, but not with geom_label_repel
. I created a function to do what I want by inserting spaces - but it will only work with monospace fonts.
Is there a way to get geom_label_repel
to align left with variable width fonts?
library(tidyverse)
library(ggrepel)
site_timeline_dfx <- tribble(
~startDate, ~endDate, ~Event,
1984, NA, "Lorem ipsum dolor sit amet, sed, vestibulum, in lacinia erat arcu. Quis magnis sed phasellus ac",
2001, NA, "Vel cursus hac in. Nulla cum quam cum, amet tellus hendrerit.",
2008, 2009, "Adipiscing ut pellentesque malesuada sit venenatis integer proin sapien. Scelerisque ipsum ut.")
my_xwrapFun = function(x, width = 75){
x_wrap <- strwrap(x, width = width)
i <- length(x_wrap)
max_strln <- max(str_length(x_wrap))
x_wrap <- encodeString(x_wrap, width = width, justify ="l")
paste(x_wrap, collapse = "\n")
}
site_timeline_dfx <-
group_by(site_timeline_dfx, Event) %>%
mutate(Event_wrap = my_xwrapFun(Event))
ggplot(site_timeline_dfx, aes(x = startDate, y = 0.5, label = Event_wrap)) + geom_point() +
geom_label(hjust = 0, size = 2) +
xlim(c(1984,2050))
ggplot(site_timeline_dfx, aes(x = startDate, y = 0.5, label = Event_wrap)) + geom_point() +
geom_label_repel(hjust = 0, size = 2) +
xlim(c(1984,2050))
ggplot(site_timeline_dfx, aes(x = startDate, y = 0.5, label = Event_wrap)) + geom_point() +
geom_label_repel(hjust = 0, size = 2, family = "mono") +
xlim(c(1984,2050))