Rotate secondary axis label text

I am using the newly added secondary axis label function in ggplot2. I would like to rotate only the secondary axis, but could not find the documentation or decide how to do this.

Its simple enough to rotate all text using ...

ggplot(mtcars, aes(x = wt, y = mpg, colour = mpg)) +
    geom_point() +
    scale_x_continuous(name = 'Bottom Axis',
                       sec.axis = sec_axis(trans = ~ .,
                                           name  = 'Top Axis',
                                           breaks = c(2:5),
                                           labels = c('Two Two', 'Three Three Three', 'Four Four Four Four', 'Five Five Five Five Five'))) +
## Rotate text of x-axis
    theme(axis.text.x = element_text(angle = 90))

Example of a double axis with both axes rotated It is not mentioned in any of the documentation that I read (for example, scale_continuous and themes ), how to achieve rotation of only one axis.

My motivation for this is that some labels that I wanted to apply to my data should be long and overlap when placed horizontally, rotating them, I can avoid this, but I want to keep the horizontal orientation on the lower axis.

+4
1

dev version ggplot2, axis.text.x.top:

ggplot(mtcars, aes(x = wt, y = mpg, colour = mpg)) +
  geom_point() +
  scale_x_continuous(name = 'Bottom Axis',
                     sec.axis = sec_axis(trans = ~ .,
                                         name  = 'Top Axis',
                                         breaks = c(2:5),
                                         labels = c('Two Two', 'Three Three Three', 'Four Four Four Four', 'Five Five Five Five Five'))) +
  ## Rotate text of x-axis
  theme(axis.text.x.top= element_text(angle = 45, hjust = 0))

enter image description here

+4

Source: https://habr.com/ru/post/1663607/


All Articles