ggplot: rotating and re-positioning ylab to the middle

Viewed 277

I'm trying to rotate ylab 0 however when I do it, it for some reason does not stay in the middle but moves it all the way to the top left. What I want is to have the y title in the middle next to the .5 level.

df = data.frame(id=c("a","b","c"), level=c(1,2,3))
ggplot(df, aes(x = id, fill = level )) + 
    geom_bar() +
    ylab("level") + 
    theme(
        axis.title.y=element_text(angle = 0),
        axis.ticks.y=element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_blank(),
        axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()

    )  + theme(legend.position="right", legend.title=element_blank(), legend.key = element_blank() )

enter image description here

1 Answers

Manually forcing the vjust to be 0.5 seems to fix it:

ggplot(df, aes(x = id, fill = level )) + 
  geom_bar() +
  ylab("level") + 
  theme(axis.title.y = element_text(angle = 0,vjust = 0.5))

...but the fact that just setting the angle changes the vjust feels like a bug to me. Particularly since it seems to only happen with some angle settings.

I'm guessing it's some sort of rotational/angular arithmetic bug, since a little experimentation seems to suggest that it behaves normally for values of angle between 90-179 and between 270-359.

Related