Justifying lines of text within individual ggrepel

2019-07-07 23:57发布

问题:

I have a plot where I'm labelling elements with multi-line ggrepel labels. I'd like to right-justify within some of these and left-justify within others. See example:

p <- ggplot() +
  coord_cartesian(xlim=c(0,1), ylim=c(0,1)) +
  theme_void()
p

labelInfo <- data.frame(x=c(0.45,0.55), y=c(0.5,0.5), 
                        g=c("I'd like very much to be\nright justified","And I'd like to be\nleft justified"))

p + geom_label_repel(data=labelInfo, aes(x,y,label=g), 
                     box.padding = 0.5, point.padding = 0.75, 
                     nudge_x = c(-0.05,0.05), nudge_y = 0, direction="x",
                     arrow=arrow(length=unit(2,"mm"), ends="last", type="closed"))

If I were doing this with ggplot2 annotations, I'd do it like this, which yields what I want in terms of the left/right justification:

p + annotate("text", label="I'd like very much to be\nright justified", x=0.45, y=0.5, hjust=1) +
  annotate("text", label="And I'd like to be\nleft justified", x=0.55, y=0.5, hjust=0)

How do I achieve this with ggrepel labels?

Thanks!