Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_label_repel text justification and alignment

Tags:

r

ggplot2

ggrepel

Is there a possible work-around to left-justify the text label created by geom_label_repel (or geom_text_repel) in the example below where all text are placed with positive nudge_x value and y-only adjusted position in direction parameter? Currently, the default behavior is to center-align the text:

library(ggplot2)
library(ggrepel)

ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) + 
  geom_point(size=3) + 
  facet_wrap(~cyl, labeller=label_both) + 
  scale_x_discrete(expand=c(0, 1.5)) + 
  geom_label_repel(aes(label=rownames(mtcars)), 
               size=3, segment.size=0.25, nudge_x=0.5, direction="y")

enter image description here

I am looking to emulate the left-justification that is possible in geom_label (or geom_text) by setting hjust=0 as seen in example below, while being able to automatically repel labels in the y direction:

ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) + 
  geom_point(size=3) + 
  facet_wrap(~cyl, labeller=label_both) +
  scale_x_discrete(expand=c(0, 1.5)) + 
  geom_label(aes(label=rownames(mtcars)), size=3, nudge_x=0.2, hjust=0)

enter image description here

Edited: As a hack, would it be possible to build hjust (and vjust) into ggrepel?

like image 503
Djork Avatar asked Oct 30 '17 09:10

Djork


1 Answers

In the 4 years since OP posted this question, hjust= seems to have been added to the ggrepel package:

library(ggplot2)
library(ggrepel)

ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) + 
  geom_point(size=3) + 
  facet_wrap(~cyl, labeller=label_both) + 
  scale_x_discrete(expand=c(0, 1.5)) + 
  geom_label_repel(
    aes(label=rownames(mtcars)), hjust=0,
    size=3, segment.size=0.25, nudge_x=0.5, direction="y")

enter image description here

like image 67
chemdork123 Avatar answered Sep 26 '22 02:09

chemdork123