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

R `ggplot2`axis.text带修改比例位置的边距,r,ggplot2,R,Ggplot2,我不确定我是否遇到了bug或者做得不正确。在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))) 但是,当头寸发生变化时,

我不确定我是否遇到了bug或者做得不正确。在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)))

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

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


我希望不管axis.text是在右边还是左边,边距都会保留下来。我做错了什么吗?

我认为这是因为右侧y轴标签的外观是由
主题()中的
轴.text.y.right
决定的,虽然它继承自
轴.text.y
,但它只继承
轴.text.y.right
本身中未声明的参数

根据
主题
中的详细信息,axis.text.y.right
的继承链如下所示:

axis.text.y.right
->
axis.text.y
->
axis.text
->
text

ggplot中的默认主题是
主题灰
。在控制台中输入
theme\u 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)
}
考虑到所有继承,axis.text.y.right在
主题中的实际参数是什么

  • 族=
    基本族
    (来自
    文本
  • 面=
    “普通”
    (来自
    文本)
  • 颜色=
    “灰色30”
    (来自
    轴.text
    ,它覆盖
    文本的
    “黑色”
  • 尺寸=基本尺寸的80%(从轴.text
    rel(0.8)
    修改基本尺寸
  • hjust=
    0
    (从
    axis.text.y.right
    ,它覆盖
    axis.text.y
    1
    ,它覆盖
    text
    0.5
  • vjust=
    0.5
    (来自
    text
  • 角度=
    0
    (从
    text
  • lineheight=
    0.9
    (来自
    text
  • margin=
    margin(l=0.8*half_line/2)
    (从
    axis.text.y.right
    ,它覆盖
    axis.text.y
    margin=margin(r=0.8*half_line/2
    ,它覆盖
    text
    margin()
  • 调试=
    FALSE
    (来自
    文本
  • inherit.blank=
    FALSE
    element\u text
    的默认参数)
同样地,对于下面这样的一段代码,
axis.text.y.right
将继承
color=“red”
(它将覆盖
axis.text
color=“grey30”
)。但是由于它有自己的margin参数,所以它不会继承
margin=margin(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))) 

我相信你确实发现了一个bug。我会把它作为一个问题发布在ggplot2回购协议上。
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))) 
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)))