5

I'm struggling to understand the interactions for ggplot's axis.text.x: angle, hjust and vjust. Everything I've read works when angle is between 0 and 45, but not for angles > 45 and < 90.

Below is a minimal reproducible example:

library(ggplot2)   
p <- ggplot(mtcars, aes(1000 * mpg, hp)) +
      geom_point()

The first plot with angle = 90 looks as expected,

p + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

enter image description here

However, when decreasing the angle by only 1 degree compared with the plot above (from angle = 90 to angle = 89), the axis labels are shifted down.

p + theme(axis.text.x = element_text(angle = 89, hjust = 1, vjust = 0.5))

enter image description here

Any insight appreciated.

Henrik
  • 56,228
  • 12
  • 124
  • 139
KJB
  • 71
  • 5

1 Answers1

0

Try specifying both vjust = 0.5 and hjust = 0.5 then you avoid the problem.

ggplot(mtcars, aes(x = 1000*mpg, y = hp)) +
geom_point() +
theme(
axis.text.x = element_text(angle = 75, vjust = .5, hjust = .5)
)

Result

iouraich
  • 2,496
  • 4
  • 26
  • 40