`ggplot2`axis.text 边距修改后的比例位置

And*_*son 10 r ggplot2

我不确定我是遇到了错误还是做错了。axis.text在 ggplot 中指定边距并移动轴的位置时,设置不保留。

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

library(ggplot)

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

带有额外边距的绘图

但是,当头寸改变时,保证金不适用:

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))) 
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

Z.L*_*Lin 8

我相信这是因为右侧 y 轴标签的外观是由axis.text.y.rightin决定的theme(),虽然它继承自axis.text.y但它只继承了axis.text.y.right本身没有说明的参数

根据 中的详细信息?theme,for的继承链axis.text.y.right如下:

axis.text.y.right-> axis.text.y-> axis.text->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)
}
Run Code Online (Sandbox Code Playgroud)

?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)
Run Code Online (Sandbox Code Playgroud)

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

  • 家庭= base_family(来自text
  • 脸= "plain"(来自text
  • 颜色 = "grey30"(来自axis.text,覆盖text's "black"
  • 大小的= 80% base_size(来自axis.textrel(0.8)的修改textS' base_size
  • hjust = 0(from axis.text.y.right, 覆盖axis.text.y's 1, 覆盖text's 0.5)
  • vjust = 0.5(来自text
  • 角度 = 0(从text)
  • 行高 = 0.9(从text)
  • margin = margin(l = 0.8 * half_line/2)(from axis.text.y.right, 覆盖axis.text.y's margin = margin(r = 0.8 * half_line/2, 覆盖text's margin())
  • 调试 = FALSE(来自text
  • inherit.blank = FALSE(element_text的默认参数)

因此,给定一段如下所示的代码,axis.text.y.right将继承color = "red"(覆盖axis.text's colour = "grey30")。但由于它有自己的 margin 参数,它不会继承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))) 
Run Code Online (Sandbox Code Playgroud)

指定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))) 
Run Code Online (Sandbox Code Playgroud)