ggplot2 axis.text边距,比例尺位置已修改 [英] `ggplot2` axis.text margin with modified scale position
问题描述
我不确定是否遇到错误或执行不正确.在ggplot中指定axis.text
边距并移动轴的位置时,设置不会持久.
I'm not sure if I've encountered a bug or am doing incorrectly. When specifying axis.text
margins in ggplot, and moving the position of the axis, the settings to not persist.
在不移动轴文本的情况下,轴周围有足够的空间:
Without moving the axis text, there is plenty of space around the axis:
library(ggplot)
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
theme(axis.text.y = element_text(color = "red", margin = margin(40, 40, 40, 40)))
但是,当头寸改变时,边距不适用:
But, when the position is changed, the margins do not apply:
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是在右边还是在左边,边距都将保留下来.我在做错什么吗?
I'd expect the margins to carry over regardless of whether the axis.text is on the right or left. Am I doing something wrong?
推荐答案
我相信,这是因为右侧y轴标签的外观由theme()
中的axis.text.y.right
决定,而它是从axis.text.y.right
本身未声明的参数.
I believe this arises because the appearance of the right side y-axis label is dictated by axis.text.y.right
in theme()
, and while it inherits from axis.text.y
, it only inherits arguments that are not stated in axis.text.y.right
itself.
根据?theme
中的详细信息,axis.text.y.right
的继承链如下:
According to details in ?theme
, the chain of inheritance for axis.text.y.right
goes as follows:
axis.text.y.right
-> axis.text.y
-> axis.text
-> text
axis.text.y.right
-> axis.text.y
-> axis.text
-> text
ggplot中的默认主题为theme_grey
.在控制台中输入theme_grey
(末尾没有()
),您将看到完整的功能.让我们看一下相关的位:
The default theme in ggplot is theme_grey
. Enter theme_grey
(without ()
at the end) into your console, and you'll see the full function. Let's look at the relevant bits:
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
shows the full list of arguments that element_text
expects:
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)
给出所有继承,theme_grey
中axis.text.y.right
的实际参数是什么?
Given all the inheritance, what are axis.text.y.right
's actual arguments in theme_grey
?
- 家庭=
base_family
(来自text
) - face =
"plain"
(来自text
) - colour =
"grey30"
(来自axis.text
,覆盖了text
的"black"
) - 大小=
base_size
的80%(摘自axis.text
base_size
的axis.text
rel(0.8)
) - just =
0
(来自axis.text.y.right
,它覆盖了axis.text.y
的1
,它覆盖了text
的0.5
) - vjust =
0.5
(来自text
) - angle =
0
(来自text
) - lineheight =
0.9
(来自text
) - 保证金=
margin(l = 0.8 * half_line/2)
(来自axis.text.y.right
,覆盖了axis.text.y
的margin = margin(r = 0.8 * half_line/2
,覆盖了text
的margin()
) - debug =
FALSE
(来自text
) - inherit.blank =
FALSE
(element_text
的默认参数)
- family =
base_family
(fromtext
) - face =
"plain"
(fromtext
) - colour =
"grey30"
(fromaxis.text
, which overridestext
's"black"
) - size = 80% of
base_size
(fromaxis.text
'srel(0.8)
modification oftext
'sbase_size
) - hjust =
0
(fromaxis.text.y.right
, which overridesaxis.text.y
's1
, which overridestext
's0.5
) - vjust =
0.5
(fromtext
) - angle =
0
(fromtext
) - lineheight =
0.9
(fromtext
) - margin =
margin(l = 0.8 * half_line/2)
(fromaxis.text.y.right
, which overridesaxis.text.y
'smargin = margin(r = 0.8 * half_line/2
, which overridestext
'smargin()
) - debug =
FALSE
(fromtext
) - inherit.blank =
FALSE
(element_text
's default parameter)
这样,给定一段类似以下的代码,axis.text.y.right
将继承color = "red"
(覆盖axis.text
的colour = "grey30"
).但是,由于它具有自己的margin参数,因此不会继承margin = margin(40, 40, 40, 40)
:
As such, given a piece of code like below, axis.text.y.right
will inherit color = "red"
(which overrides axis.text
's colour = "grey30"
). But since it has its own margin argument, it will not inherit 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
可以解决问题:
Specifying axis.text.y.right
instead of axis.text.y
would do the trick:
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)))
这篇关于ggplot2 axis.text边距,比例尺位置已修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!