How to control both text box placement and justification of text within box, separately

This is a follow-up question to How to align multiline text annotation in ggplot, where I asked how to left-justify text in a geom_text() element.

In @FJCC 's solution to that question, setting the geom_text() parameter hjust to 0 did the trick, but if I replace geom_text() with geom_label(), we can see more easily that the text box is also shifted in the process:

library(tidyverse)
tibble(x = 1, y = c(0, 2)) %>% 
  ggplot(aes(x, y)) +
  geom_line() +
  geom_label(
    x = 1, y = 1,
    label = "one line\ntwo lines\nthree lines",
    #hjust = 0
  )


tibble(x = 1, y = c(0, 2)) %>% 
  ggplot(aes(x, y)) +
  geom_line() +
  geom_label(
    x = 1, y = 1,
    label = "one line\ntwo lines\nthree lines",
    hjust = 0
  )

Created on 2021-03-31 by the reprex package (v0.3.0)

How would one go about left-justifying the text without moving the original position of the box?

1 Like

Looking at the source code for geom_label(), it looks like the same parameters (hj, and vj) are used to calculate justification of the text and the rounded rectangle when constructing the grob that is plotted:

You might try using some of the annotation functions in {cowplot}:

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.