`ggplot2` axis.text边距,修改了比例位置

我不确定我是否遇到过错误或做错了.在ggplot中指定axis.text边距并移动轴的位置时,设置不会持久存在.

在不移动轴文本的情况下,轴周围有足够的空间:

library(ggplot)

ggplot(mtcars, aes(mpg, wt)) +
    geom_point() +
    theme(axis.text.y = element_text(color = "red", margin = margin(40, 40, 40, 40)))

《`ggplot2` axis.text边距,修改了比例位置》

但是,当头寸发生变化时,保证金不适用:

ggplot(mtcars, aes(mpg, wt)) +
    geom_point() +
    scale_y_continuous(position = "right") + #This is the new line
    theme(axis.text.y = element_text(color = "red", margin = margin(40, 40, 40, 40))) 

《`ggplot2` axis.text边距,修改了比例位置》

无论axis.text是在右边还是左边,我都希望边距可以延续.难道我做错了什么?

最佳答案 我相信这是因为右侧y轴标签的外观由theme()中的axis.text.y.right决定,虽然它继承自axis.text.y,但它只继承了未在axis.text.y.right本身.

根据?theme中的细节,axis.text.y.right的继承链如下:

axis.text.y.right – > axis.text.y – > axis.text – >文本

ggplot中的默认主题是theme_grey.在控制台中输入theme_grey(最后没有()),您将看到完整的功能.我们来看看相关的位:

function(base_size = 11, base_family = "", base_line_size = base_size/22, 
         base_rect_size = base_size/22) {

  half_line <- base_size/2

  theme(text = element_text(family = base_family, 
                            face = "plain", 
                            colour = "black",
                            size = base_size, 
                            lineheight = 0.9, 
                            hjust = 0.5, 
                            vjust = 0.5, 
                            angle = 0, 
                            margin = margin(), 
                            debug = FALSE), 

        axis.text = element_text(size = rel(0.8), 
                                 colour = "grey30"), 

        axis.text.y = element_text(margin = margin(r = 0.8 * half_line/2), 
                                   hjust = 1), 

        axis.text.y.right = element_text(margin = margin(l = 0.8 * half_line/2), 
                                         hjust = 0), 
        ...
        complete = TRUE)
}

?element_text显示了element_text期望的完整参数列表:

element_text(family = NULL, face = NULL, colour = NULL, size = NULL,
  hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL,
  color = NULL, margin = NULL, debug = NULL, inherit.blank = FALSE)

鉴于所有的继承,axis.text.y.right在theme_grey中的实际参数是什么?

> family = base_family(来自文字)
> face =“plain”(来自文字)
> color =“grey30”(来自axis.text,它覆盖文本的“黑色”)
> size = base_size的80%(来自axis.text对文本的base_size的rel(0.8)修改)
> hjust = 0(来自axis.text.y.right,它覆盖了axis.text.y的1,它覆盖了文本的0.5)
> vjust = 0.5(来自文字)
> angle = 0(来自文字)
> lineheight = 0.9(来自文字)
> margin = margin(l = 0.8 * half_line / 2)(来自axis.text.y.right,它会覆盖axis.text.y的margin = margin(r = 0.8 * half_line / 2,它会覆盖文本的margin())
> debug = FALSE(来自文字)
> inherit.blank = FALSE(element_text的默认参数)

因此,给定一段代码如下,axis.text.y.right将继承color =“red”(它会覆盖axis.text的color =“grey30”).但由于它有自己的边距参数,它不会继承margin = margin(40,40,40,40):

ggplot(mtcars, aes(mpg, wt)) +
    geom_point() +
    scale_y_continuous(position = "right") +
    theme(axis.text.y = element_text(color = "red", margin = margin(40, 40, 40, 40))) 

指定axis.text.y.right而不是axis.text.y可以解决这个问题:

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  scale_y_continuous(position = "right") +
  theme(axis.text.y.right = element_text(color = "red", margin = margin(40, 40, 40, 40))) 
点赞