Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate only text in annotation in ggplot?

Tags:

r

ggplot2

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?

like image 370
half-pass Avatar asked Dec 21 '14 00:12

half-pass


People also ask

How to rotate axis labels in ggplot2?

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.

Is there a way to annotate text in ggplot2?

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.

How to annotate rotated text in Python?

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)

Why does ggplot2 align text towards the middle of the plot?

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).


1 Answers

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).

like image 114
Gregor Thomas Avatar answered Oct 15 '22 18:10

Gregor Thomas