0

I'm trying to rotate an annotation on a ggplot in R, similar to this question, but using the label geometry with the background.

Using the code that works with geom = "text" or geom_text with geom = 'label' or geom_label results in un-rotated annotation.

fake = data.frame(x=rnorm(100), y=rnorm(100))
ggplot(data = fake, aes(x = x, y = y)) + 
    geom_point() +
    geom_vline(xintercept = -1, linetype = 2, color = "red") +
#    annotate(geom = "text", x = -1, y = -1, label = "Helpful annotation", color = "red",
#             angle = 90)
    annotate(geom = "label", x = -1, y = -1, label = "Helpful annotation", color = "red",
             angle = 90)

enter image description here

Text shows up with white background, but without rotation.

Is there an alternate way to rotate the label?

Brian Fisher
  • 1,305
  • 7
  • 17
  • 1
    `geom_label()` which has the same parameters as `annotate(geom = "label"...` does not support rotation. You can use `ggtext::geom_richtext()` though. It uses the same arguments, in this case `angle`. – Kat Aug 27 '21 at 23:06

1 Answers1

5

This can't currently be done with geom_label. From the help: "Currently geom_label() does not support the check_overlap argument or the angle aesthetic."

But this can be done with a related function from the ggtext package:

ggplot(data = fake, aes(x = x, y = y)) + 
    geom_point() +
    geom_vline(xintercept = -1, linetype = 2, color = "red") +
    ggtext::geom_richtext(x = -1, y = -1, label = "Helpful annotation", angle = 90)

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53