Aligning labels with ggrepel

Viewed 4609

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.

3 Answers

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

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

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

Related