3

我真的很喜欢 patchwork 包,多个地块的对齐通常比其他包(cowplot/gridextra)更好也更容易实现。

但是,我无法解决的一件事:是否可以在拼凑中忽略 x 轴对齐,而只对齐所有其他元素?或者手动调整拼凑中的这个x轴对齐后缀?见附图:如果可能,我希望补丁 B 和 C (Petal.Length) 中的 x 轴标题更靠近 x 轴。

在此处输入图像描述

生成图像的代码:

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

2 回答 2

2

操作示例

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

重新显示 OP 图

使用负边距

我花了一段时间才意识到问题源于需要对齐axis.texts 和axis.titles 的混合。尽管它们在语法上是不同的元素,但分类轴文本提供了连续轴标题所做的大部分或全部实际功能。标题将始终在对齐的文本区域之外与网格对齐。单独的理由不能解决这个问题,但负边距可以。

笔记:

  • 您需要的负边距值取决于图中的其他比例因子,因此这是最终解决方案中的一种。-50在这种情况下工作,但我需要使用其他值
  • 它也适用于 y 轴
  • 我把括号弄乱了,但除额外theme()层外,绘图规范是相同的
  • 您通常不希望在单个绘图定义中包含此负边距主题片段,因为它似乎会在那里导致错误,并且您将无法以这种方式迭代地细化边距值,至少如果您的脚本不容易/Rmd 很长和/或很复杂。这确实是仅在用 组装图的级别上的问题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")

在此处输入图像描述

于 2021-01-28T23:03:41.303 回答
0

我发现了一个黑客。如果使用函数 patchwork::patchworkGrob,绘图将存储在可编辑的 gtable 中。您只需要在表格中找到正确的布局值。每个布局部分都有可以编辑的“trl 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)

编辑拼凑的结果

编辑 29-01-2021:我认为Paul McMurdie最近的回答比我在这里提到的要容易。

于 2020-06-11T15:48:13.850 回答