I wish to add labels to points on a ggplot. The labels should be below each point. There may be multiple labels per point. If so, they should be left-justified. Each label may be a different length.
For each point, the shortest name should be centered below each point. Thus, I wish to nudge_x = half the length of the shortest name for each point.
How do I determine the length of a label so as to nudge half its value?
Example
library("tidyverse")
df <- tibble(
x = c("one", "two"),
y = c(2.5, 1.7),
company = c("Normal", "Short\nA_bit_longer")
)
company_nudge_x <- -0.1
company_nudge_y <- -0.2
ggplot(df, aes(x = x, y = y, group = x)) +
geom_point(size = 5) +
geom_line(aes(group = "x")) +
coord_cartesian(ylim = c(0.3, 2.7)) +
# Labels
geom_text(aes(label = company),
#nudge_x = company_nudge_x,
nudge_y = company_nudge_y,
hjust = 0) # left_justify text
