忽略 x-label 与拼凑中的多个地块对齐:这可能吗?
Ignore x-label alignment with multiple plots in patchwork: is this possible?
我真的很喜欢 patchwork 包,多个地块的对齐通常比其他包更好更容易实现 (cowplot/gridextra)。
但是,有一件事我无法解决:是否可以在拼凑中忽略 x-axis 对齐,而只对齐所有其他元素?还是手动调整patchwork中的这个x-axis对齐后缀?
见附图:如果可能的话,我希望补丁 B 和 C (Petal.Length) 中的 x-axis 标题更接近 x-axis。
生成图像的代码:
library(ggplot2)
library(patchwork)
plot.1 <- ggplot(iris, aes(x = Species, y = Petal.Width)) +
geom_boxplot() +
scale_x_discrete(labels = c("A very\nvery\nlong\nlabel", "","")) +
labs(x = element_blank())
plot.2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
geom_point()
(plot.1 |plot.2)/(plot.2|plot.1) +
plot_annotation(tag_levels = "A")
我发现了一个黑客。如果您使用函数 patchwork::patchworkGrob,绘图将存储在可以编辑的 gtable 中。您只需在 table 中找到正确的布局值。每个布局部分都有 't r l b' 个可以编辑的尺寸(我相信是顶部、右侧、左侧、底部)。就我而言,我想更改“xlab-b”的底值。更改值后,可以使用 grid::grid.draw() 绘制 gtable。如果需要,可以以类似的方式以类似的方式更改 y 轴位置 (ylab-l)。
library(grid) # should be loaded already, just to be sure.
# Unadjusted patchwork:
plots <- (plot.1 |plot.2)/(plot.2|plot.1) +
plot_annotation(tag_levels = "A")
# create gTable object
plots.gtable <- patchworkGrob(plots)
# edit gtable object:
# tables are found in the list ‘plots.gtable$grobs’
# Second and fourth grobs in the list are the TableGrobs to edit:
# First edit panel B:
plots.gtable$grobs[[2]]$layout[
plots.gtable$grobs[[2]]$layout$name == "xlab-b",
]$b[2] <- 12 # change this number, for me 12 worked fine
# Ends with $b[2] because we only want to adjust position of
# the x-axis title of panel B.
# To adjust the position of the x-axis title of both plot A and B,
# just leave out the [2].
# Now access lower two patchwork TableGrobs:
# edit panel C:
plots.gtable$grobs[[4]]$layout[
plots.gtable$grobs[[4]]$layout$name == "xlab-b",
]$b[1] <- 12
# plot the adjusted figure:
grid::grid.newpage()
grid::grid.draw(plots.gtable)
edited-patchwork-result
编辑 29-01-2021:我认为 最近的回答比我在这里提到的那个更容易。
OP 示例
library(ggplot2)
library(patchwork)
plot.1 <- ggplot(iris, aes(x = Species, y = Petal.Width)) +
geom_boxplot() +
scale_x_discrete(labels = c("A very\nvery\nlong\nlabel", "","")) +
labs(x = element_blank())
plot.2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
geom_point()
(plot.1 | plot.2) /
(plot.2 | plot.1) +
plot_annotation(tag_levels = "A")
使用负边距
我花了一段时间才意识到问题出在
需要对齐 axis.text
s 和 axis.title
s 的混合物。
尽管它们在语法上是不同的元素,
分类轴文本服务于大多数或所有
连续轴标题的实用功能。
标题将始终 grid-aligned 在对齐的文本区域之外。
仅凭理由无法解决该问题,但负利润率可以。
备注:
- 您想要的负边距值取决于绘图中的其他比例因子,因此这是最后润色 解决方案之一。
-50
在这种情况下有效,但我需要使用其他值
- 它也适用于 y-axis
- 我搞砸了括号,但除了额外的
theme()
层 之外,绘图规范是相同的
- 您通常不希望在单独的绘图定义中包含此负边距主题片段,因为它看起来会导致那里的错误,您将无法以这种方式迭代地细化边距值,至少如果您的 script/Rmd 很长 and/or 复杂,则不会那么容易。这确实是一个仅在使用
patchwork
组装绘图级别的问题,因此最好的做法可能是在您定义拼凑组装的地方保持这样的抛光调整,如我在此处所示。
(plot.1 | (
plot.2 +
theme(axis.title.x = element_text(margin = margin(t = -50, unit = "pt"))))
) / (
(plot.2 +
theme(axis.title.x = element_text(margin = margin(t = -50, unit = "pt")))) |
plot.1
) +
plot_annotation(tag_levels = "A")
我真的很喜欢 patchwork 包,多个地块的对齐通常比其他包更好更容易实现 (cowplot/gridextra)。
但是,有一件事我无法解决:是否可以在拼凑中忽略 x-axis 对齐,而只对齐所有其他元素?还是手动调整patchwork中的这个x-axis对齐后缀? 见附图:如果可能的话,我希望补丁 B 和 C (Petal.Length) 中的 x-axis 标题更接近 x-axis。
生成图像的代码:
library(ggplot2)
library(patchwork)
plot.1 <- ggplot(iris, aes(x = Species, y = Petal.Width)) +
geom_boxplot() +
scale_x_discrete(labels = c("A very\nvery\nlong\nlabel", "","")) +
labs(x = element_blank())
plot.2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
geom_point()
(plot.1 |plot.2)/(plot.2|plot.1) +
plot_annotation(tag_levels = "A")
我发现了一个黑客。如果您使用函数 patchwork::patchworkGrob,绘图将存储在可以编辑的 gtable 中。您只需在 table 中找到正确的布局值。每个布局部分都有 't r l b' 个可以编辑的尺寸(我相信是顶部、右侧、左侧、底部)。就我而言,我想更改“xlab-b”的底值。更改值后,可以使用 grid::grid.draw() 绘制 gtable。如果需要,可以以类似的方式以类似的方式更改 y 轴位置 (ylab-l)。
library(grid) # should be loaded already, just to be sure.
# Unadjusted patchwork:
plots <- (plot.1 |plot.2)/(plot.2|plot.1) +
plot_annotation(tag_levels = "A")
# create gTable object
plots.gtable <- patchworkGrob(plots)
# edit gtable object:
# tables are found in the list ‘plots.gtable$grobs’
# Second and fourth grobs in the list are the TableGrobs to edit:
# First edit panel B:
plots.gtable$grobs[[2]]$layout[
plots.gtable$grobs[[2]]$layout$name == "xlab-b",
]$b[2] <- 12 # change this number, for me 12 worked fine
# Ends with $b[2] because we only want to adjust position of
# the x-axis title of panel B.
# To adjust the position of the x-axis title of both plot A and B,
# just leave out the [2].
# Now access lower two patchwork TableGrobs:
# edit panel C:
plots.gtable$grobs[[4]]$layout[
plots.gtable$grobs[[4]]$layout$name == "xlab-b",
]$b[1] <- 12
# plot the adjusted figure:
grid::grid.newpage()
grid::grid.draw(plots.gtable)
edited-patchwork-result
编辑 29-01-2021:我认为
OP 示例
library(ggplot2)
library(patchwork)
plot.1 <- ggplot(iris, aes(x = Species, y = Petal.Width)) +
geom_boxplot() +
scale_x_discrete(labels = c("A very\nvery\nlong\nlabel", "","")) +
labs(x = element_blank())
plot.2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
geom_point()
(plot.1 | plot.2) /
(plot.2 | plot.1) +
plot_annotation(tag_levels = "A")
使用负边距
我花了一段时间才意识到问题出在
需要对齐 axis.text
s 和 axis.title
s 的混合物。
尽管它们在语法上是不同的元素,
分类轴文本服务于大多数或所有
连续轴标题的实用功能。
标题将始终 grid-aligned 在对齐的文本区域之外。
仅凭理由无法解决该问题,但负利润率可以。
备注:
- 您想要的负边距值取决于绘图中的其他比例因子,因此这是最后润色 解决方案之一。
-50
在这种情况下有效,但我需要使用其他值 - 它也适用于 y-axis
- 我搞砸了括号,但除了额外的
theme()
层 之外,绘图规范是相同的
- 您通常不希望在单独的绘图定义中包含此负边距主题片段,因为它看起来会导致那里的错误,您将无法以这种方式迭代地细化边距值,至少如果您的 script/Rmd 很长 and/or 复杂,则不会那么容易。这确实是一个仅在使用
patchwork
组装绘图级别的问题,因此最好的做法可能是在您定义拼凑组装的地方保持这样的抛光调整,如我在此处所示。
(plot.1 | (
plot.2 +
theme(axis.title.x = element_text(margin = margin(t = -50, unit = "pt"))))
) / (
(plot.2 +
theme(axis.title.x = element_text(margin = margin(t = -50, unit = "pt")))) |
plot.1
) +
plot_annotation(tag_levels = "A")