hkj447
hkj447

Reputation: 737

Rotate axis TEXT labels in R

I want to rotate the y axis label to be horizontal instead of vertical, but every post I see only talks about rotating the tick marks on the y axis. For example, running ggplot(mpg, aes(fl)) + geom_bar() + theme(axis.text.y = element_text(angle=90, hjust=1)) generates the following plot:

enter image description here

I want "count" to be horizontal instead of vertical. I have tried las and theme(), but none of these have rotated the y axis TEXT label. Is this possible to do in R?

Upvotes: 1

Views: 2260

Answers (1)

chemdork123
chemdork123

Reputation: 13793

Very possible to do what you're looking to do. The axis title is different than the text (the labels of the tick marks on the axis). Consequently, the theme() element you want to address is axis.title.y. As with the axis text, you address using element_text() and supply an angle. In this case, the angle is set to 90 degrees by default, so you want to rotate back to angle=0. Importantly, you will want to adjust the vertical alignment too, which is set to be aligned at the top by default. To put the title in the middle of the axis, you need a vertical alignment of 0.5.

p <- your plot code
p + theme(axis.title.y=element_text(angle=0, vjust=0.5))

enter image description here

Upvotes: 5

Related Questions