Set Anchor point geom_text_repel/geom_label_repel to be the same for all labels (left side)

985 Views Asked by At

I am am using a geom_text_repel, kind of based on this example: https://ggrepel.slowkow.com/articles/examples.html#align-labels-on-the-top-or-bottom-edge. However, in my code, the end of the line segment sometimes snaps to the left side of the label and sometimes to the bottom of the label. This kind of ruins the clean look I am looking for. I realize this behaviour is scale and resolution dependent (if you change the size of the viewer or save in a different resolution this behaviour does not happen), but I cannot change those for now. Is there a way to force ggrepel to snap (anchor) to the left side of my labels

This code replicates the problem (my actual code is of course not such a clustered mess). From FB to FI the segment anchors at the bottom, DK to the left, then IP to the bottom and from there it anchors to the left side (desired).

library(ggplot2)
library(ggrepel)
library(dplyr)
set.seed(2)
dat <- tibble(A = c(seq(1,1.5, .015), 6),
              B = 1,
              C = replicate(35, paste0(sample(LETTERS, 2), collapse = '')))

p1 <- dat %>%
  ggplot(aes(x = B, 
             y = A, 
             label = C))+
  geom_point() +
  geom_text_repel( direction = "y",
                   xlim = c(1.1, 5),
                   min.segment.length = 0)+
  coord_cartesian(expand = T, clip = "off")+
  xlim(1,1.2)


ggsave("p1.png", p1, width = 7, height = 5)

enter image description here

0

There are 0 best solutions below