Why does hjust move my labels up and down, and vjust left and right? Isn't that backward?

Viewed 838

I've created a chart in RStudio and this is part of my code:

theme(
    plot.title = element_text(colour = "#ec008c", family = "Inter", size = 25),
    axis.title.y = element_text(size = 14, family = "Inter", hjust = 0.1),
    axis.text.x = element_text(family = "Inter"),
    axis.text.y = element_text(family = "Inter"),
    plot.subtitle = element_text(family = "Inter")
  )

I understand that hjust (i.e. horizontal justification) and vjust (i.e. vertical justification) are used to move labels around, my question is why does this code using hjust move the y-axis title up and down rather than left and right?

It makes more sense to me intuitively to use vjust to move the axis title up and down, is there something wrong with my thought process?

1 Answers

To expand a little bit on the discussion in the comments, the hjust parameter is not relative to the axis in theory (though in practise it is), but the hjust parameter is relative to the direction of the text. Perhaps a visual indicator makes it easier to grasp. In the example below we repeat hjust = c(0, 0.5, 1) for three angles of text, relative to the point that defines the text's x and y.

df <- expand.grid("Test!", hjust = c(0, 0.5, 1), angle = c(0, 45, 90))

grid::grid.newpage()

grid::grid.points(
  x = seq(0.1, 0.9, length.out = nrow(df)),
  y = rep(0.5, nrow(df)),
  pch = 21,
  gp = grid::gpar(col = "red", fill = "red", fontsize = 10),
  default.units = "npc"
)

grid::grid.text(
  x = seq(0.1, 0.9, length.out = nrow(df)),
  df$Var1, hjust = df$hjust, vjust = 0.5, rot = df$angle
)

enter image description here

Created on 2021-02-03 by the reprex package (v1.0.0)

Related