Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning labels with ggrepel

Tags:

r

ggplot2

ggrepel

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.

like image 301
Adam_G Avatar asked Nov 26 '17 01:11

Adam_G


2 Answers

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

like image 57
Claus Wilke Avatar answered Nov 11 '22 23:11

Claus Wilke


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

like image 37
www Avatar answered Nov 12 '22 01:11

www