strange effect of justification parameters on geom_label() rendering

If I use geom_text() with hjust and vjust parameters, I get the behavior I expect:

library(tidyverse)
tibble(x = 0, y = 0) %>% 
  ggplot() +
  geom_point(aes(x, y)) +
  geom_text(aes(x, y, label = 'Text'), size = 20)


tibble(x = 0, y = 0) %>% 
  ggplot() +
  geom_point(aes(x, y)) +
  geom_text(
    aes(x, y, label = 'Text'), size = 20, 
    hjust = 0,
    vjust = 0,
  )

Created on 2022-03-07 by the reprex package (v2.0.1)

But if I replace geom_text() with geom_label() (and change opacity so the point at origin is still visible), I get unexpected behavior:

library(tidyverse)
tibble(x = 0, y = 0) %>% 
  ggplot() +
  geom_point(aes(x, y)) +
  geom_label(aes(x, y, label = 'Text'), size = 20, alpha = 0.5)


tibble(x = 0, y = 0) %>% 
  ggplot() +
  geom_point(aes(x, y)) +
  geom_label(
    aes(x, y, label = 'Text'), size = 20, alpha = 0.5,
    hjust = 0,
    vjust = 0,
  )

Created on 2022-03-07 by the reprex package (v2.0.1)

Why is the text rendered outside its bounding box?

I don't know why. But if you are trying to make plot using geom_label() and want to make it not overlapping the data point, you can try geom_label_repel() from ggrepel: An R package • ggrepel. It does the work for you.

Thanks for the pointer, @ibertchen. I was trying to illustrate the use of bounding box versus axis coordinates for my students, so put together these simple examples, but then I found the geom_label() example didn't work as expected, which is why I posted the question.

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.