Axis title won't rotate when y-axis is on the right

Viewed 21

I am trying to generate a plot with y-axis on the right and I would like to rotate the y-axis title to be horizontal. Here's some sample code :

ggplot() + geom_function(fun = ~ -.x) + 
      scale_x_continuous(limits = c(-0.27,0),name = "example")+
      scale_y_continuous(position = "right",limits = c(0,0.27),name = "test")+
      theme_classic()+
      theme(axis.title.y = element_text(angle = 0, hjust = 0), 
            axis.title.x = element_text(hjust = 0))

"angle = 0" doesn't seem to do anything when you set "position = "right"", but works fine when you use the default y-axis position. Why this is happening and how do I rotate the title when y-axis is on the right?

Thank you.

1 Answers

You have to use the theme element axis.title.y.right

ggplot() + geom_function(fun = ~ -.x) + 
  scale_x_continuous(limits = c(-0.27,0),name = "example")+
  scale_y_continuous(position = "right",limits = c(0,0.27),name = "test")+
  theme_classic()+
  theme(axis.title.y.right = element_text(angle = 0), 
        axis.title.x = element_text(hjust = 0))

enter image description here

Related