8

According to the most recent updates, ggrepel now supports hjust and vjust. According to the documentation, using this should align all of the labels. However, I'm unable to get the labels to align, as seen below

enter image description here

I have tried

library(tidyverse)
library(ggrepel)

df <- data.frame(x=seq(1:5), y=seq(1:5), label=letters[seq(1:5)])

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  hjust=0,
                  direction='y',
                  nudge_x=0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm')

How can I align these labels?

EDIT

I should add that it's not just having the labels aligned, but also having them near each other, with different length connectors in order to facilitate that.

Adam_G
  • 7,337
  • 20
  • 86
  • 148

3 Answers3

10

First, as far as I understand, this is only available in the development version. So you need to install it from github:

devtools::install_github("slowkow/ggrepel")

Second, I think this only works for data points with the same x value (for hjust) or y value (for vjust).

Example:

library(tidyverse)
library(ggrepel)

df <- data.frame(x=seq(1:5), y=3, label=letters[seq(1:5)])

# not aligned
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  # vjust=0,
                  direction='y',
                  nudge_x=0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm')

enter image description here

# aligned bottom
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  vjust=0,
                  direction='y',
                  nudge_x=0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm')

enter image description here

# aligned top
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  vjust=1,
                  direction='y',
                  nudge_x=0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm')

enter image description here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
  • Interesting. Damn! I guess I'll wait to see if anyone knows a way to get all of those labels close together. If not, I'll accept this one. – Adam_G Nov 26 '17 at 03:15
5

Based on the documentation (https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html), hjust is not supported in the current version (0.7.0) on CRAN.

In addition, it seems like your direction, nudge_x, and nudge_y are not associated.

I change your code slightly to the following three versions.

direction = 'y' and nudge_y = 0.1

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  direction = 'y',
                  nudge_y = 0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm') 

enter image description here

direction = 'x' and nudge_x = 0.1

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  direction = 'x',
                  nudge_x = 0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm')

enter image description here

direction = 'both', nudge_x = 0.1, and nudge_y = 0.3

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  direction = 'both',
                  nudge_x = 0.1,
                  nudge_y = 0.3,
                  segment.size=0.2) +
  geom_smooth(method='lm')

enter image description here

It seems to be working. The only thing I notice is that label e seems to be restricted because of the limitation in x and y-axis, so you may want to further expand the axis as follows.

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  direction = 'y',
                  nudge_y = 0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm') +
  scale_y_continuous(limits = c(1, 5.5))

enter image description here

www
  • 38,575
  • 12
  • 48
  • 84
  • 1
    Thanks. But the documentation I linked to is more current than the documentation you linked to. `ggrepel` does support `hjust` and `vjust`. I appreciate the thoroughness of your answer, though. – Adam_G Nov 26 '17 at 02:46
  • 1
    Interesting. It seems like the version on CRAN (`0.7.0`) does not support `hjust`, while the version on git (`0.7.1`) supports `hjust`. I am using `0.7.0` and when I ran the code with `hjust` the condole returns `Warning: Ignoring unknown parameters: hjust`. Anyway, I think the key is the `direction` and `nudge` argument need to be the same. – www Nov 26 '17 at 02:53
  • 1
    Yeh, I had to download the github version using devtools. I got the same warning you did before. – Adam_G Nov 26 '17 at 02:58
3

Not sure what exactly your goal here is. If you want all the labels on one side, it may be easier to just draw them manually rather than using ggrepel.

df <- data.frame(x=seq(1:5), y=seq(1:5), label=letters[seq(1:5)])

ggplot(df, aes(x = x, y = y)) +
  geom_point() +
  geom_text(aes(x = max(x) + 0.1, y = y, label=label), hjust = 0, vjust = 0.5) +
  geom_segment(aes(x = x + 0.04, xend = max(x) + 0.06, y = y, yend = y), size = 0.2) +
  geom_smooth(method='lm')

enter image description here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104