【问题标题】:Adjusting the y-tick labels with hjust not working使用 hjust 调整 y-tick 标签不起作用
【发布时间】:2021-02-12 15:42:51
【问题描述】:

我想将 y 轴刻度标签向左移动(就像我将 axis.text.y 移动到底部一样)。

奇怪的是hjust = -2.5,虽然theme_set() 部分运行没有错误,y 轴上的文本没有移动。但是,vjust = 2确实会很好地影响 y 标签。

我正在使用主题中的所有选项创建错误?

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()

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这似乎是bug in how hjust works for axis text。一种解决方法是使用margin 而不是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))) 
    

    '

    对此相关答案的提示:How do I make my axis ticks face Inwards in ggplot2

    【讨论】:

      猜你喜欢
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 2018-10-10
      • 2020-09-17
      • 2012-01-29
      相关资源
      最近更新 更多