55

I have a plot like this:

fake = data.frame(x=rnorm(100), y=rnorm(100))

ggplot(data=fake, aes(x=x, y=y)) + geom_point() + theme_bw() +
  geom_vline(xintercept=-1, linetype=2, color="red") +
  annotate("text", x=-1, y=-1, label="Helpful annotation", color="red")

enter image description here

How would I rotate just the annotated text 90 degrees so that it is parallel to the reference line?

half-pass
  • 1,851
  • 4
  • 22
  • 33

1 Answers1

100

Just tell it the angle you want.

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

In ?geom_text you can see that angle is a possible aesthetic, and annotate will pass it along, just like any other argument geom_text understands (such as the x, y, label, and color already being used).

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • the problem here is, that it doesn't really take the coordinates seriously. A y=0 for example will put half the text below the x-axis. How can this be fixed? – Jakob Aug 16 '18 at 16:34
  • @PeterPan that has more to do with the text justification than the angle. The default is centered text, so it makes sense to rotate it around the center. For your case, set `hjust = 0`. [See this answer for more detail](https://stackoverflow.com/a/26684121/903061). – Gregor Thomas Aug 18 '18 at 06:52
  • 1
    hmmm.. am I missing something? in `?geom_text` i do not see that `angle` is an option for parameter? I see mapping, data, stat, etc.. – orrymr Jul 01 '20 at 11:27
  • 2
    @orrymr [Here's the docs page](https://ggplot2.tidyverse.org/reference/geom_text.html). Search the page for "angle" and you will find it. It's listed both under the "geom_text understands the following aesthetics..." list and is used in an example. – Gregor Thomas Jul 02 '20 at 17:46