Brian Fisher
Brian Fisher

Reputation: 1367

Rotate label annotation in ggplot2

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?

Upvotes: 0

Views: 1634

Answers (1)

Jon Spring
Jon Spring

Reputation: 66980

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

Upvotes: 5

Related Questions