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
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.
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')
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')
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')
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')
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With