0

I want to move the y-axis tick labels to the left (as i did with the axis.text.y to the bottom).

The weird thing is with hjust = -2.5, although the theme_set() part runs without errors, the text on the y-axis does not get moved. However, vjust = 2does affect the y labels just fine.

I am creating a bug with all my options in the theme?

library(tidyverse)
library(ggplot2) 
theme_set(
    theme_classic() + 
        theme(
            axis.ticks.length = unit(-0.25, "cm"),
            axis.text.x = element_text(vjust = -2.5),
            axis.text.y = element_text(hjust = -2.5), # this does not do anything.
#            axis.text.y = element_text(vjust = -2.5), # while vjust does affect the text placement.
            axis.line = element_blank(),
            panel.grid.major.y = element_line(linetype = 2),
            plot.title = element_text(hjust = 0.5),
            text = element_text(family = "serif"),
            legend.justification = c(1, 1),
            legend.position = c(1, 1),
            panel.border = element_rect(fill = NA, size = 2))) 
mpgdat <- tibble(mtcars)
mpgdat %>% ggplot(aes(disp, mpg)) + geom_point()

thebilly
  • 47
  • 5

1 Answers1

0

This does seem to be a bug in how hjust works for axis text. A workaround would be to use margin instead of hjust:

theme_set(
  theme_classic() + 
    theme(
      axis.ticks.length = unit(-0.25, "cm"),
      axis.text.x = element_text(vjust = -2.5),
      axis.text.y = element_text(margin = unit(c(0,0.4,0,0), "cm")),
      panel.border = element_rect(fill = NA, size = 2))) 

enter image description here'

Hat tip to this related answer: How do I make my axis ticks face Inwards in ggplot2

Jon Spring
  • 55,165
  • 4
  • 35
  • 53