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")
How would I rotate just the annotated text 90 degrees so that it is parallel to the reference line?
How to Rotate Axis Labels in ggplot2 (With Examples) You can use the following syntax to rotate axis labels in a ggplot2 plot: p + theme (axis.text.x = element_text (angle = 45, vjust = 1, hjust=1)) The angle controls the angle of the text while vjust and hjust control the vertical and horizontal justification of the text.
However, text annotation can be tricky due to the way that R handles fonts. The ggplot2 package doesn’t have all the answers, but it does provide some tools to make your life a little easier.
For this, we can use the annotate function and and the angle argument of the annotate function. To the angle argument, we have to assign the degree of rotation that we want to use (i.e. 90 degree). ggp + # Annotate rotated text label annotate ("text" , x = 2, y = 3 , label = "This Is My Text Label" , angle = 90)
It aligns text towards the middle of the plot, which ensures that labels remain within the plot limits: The font size is controlled by the size aesthetic. Unlike most tools, ggplot2 specifies the size in millimeters (mm), rather than the usual points (pts).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With