【问题标题】:Combining plots with patchwork when plot aspect ratio is 1当地块纵横比为 1 时将地块与拼凑组合
【发布时间】:2021-09-23 08:20:00
【问题描述】:

theme(aspect.ratio = 1) 时,我遇到了使用patchwork 组合图的问题。 提供几个例子:

library(tidyverse)
library(patchwork)

# Create the base plots
plotlist = list(
  fig1 = iris %>% 
    filter(Species == "setosa") %>% 
    ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point(),
  
  fig2 = iris %>% 
    filter(Species == "versicolor") %>% 
    ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point(),
  
  fig3 = iris %>% 
    filter(Species == "virginica") %>% 
    ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point()
)

# Here patchwork combines the plots nicely
plotlist$fig1 / (plotlist$fig2 + plotlist$fig3)


# However, if we change the aspect.ratio to 1 things don't look so nice anymore
plotlist = lapply(plotlist, function(x) {
  x + theme(aspect.ratio = 1)
})

# Notice the large gap between plots. Instead, I would like the plots in the second row to be almost directly under the first row.
plotlist$fig1 / (plotlist$fig2 + plotlist$fig3)

# I tried setting the margins to zero, but that doesn't change anything
plotlist = lapply(plotlist, function(x) {
  x + theme(plot.margin = margin(0, 0, 0, 0, unit = "pt"))
})

plotlist$fig1 / (plotlist$fig2 + plotlist$fig3)

如何使用拼凑改进布局?第二行的图的宽度应该是第一行的一半,与第一个图一样,但所有图的纵横比都应为 1。

【问题讨论】:

  • plotlist$fig1 / (plotlist$fig2 + plotlist$fig3) + plot_layout(widths = 1)怎么样
  • 这是个好建议!虽然这确实解决了间距问题,但它并没有产生我想要的布局(第二行中的图应限制为第一行的宽度)。使用 widths = c(1,2) 也没有解决这个问题。
  • 很难正确。这使它更接近。 design <- "#1# #23" plotlist$fig1 / (plotlist$fig2 + plotlist$fig3) + plot_layout(design = design, widths = c(1,2)) 。顺便说一句,设计应该在 2 条代码线上。从评论中复制粘贴效果不太好。
  • 不幸的是,这并没有按预期工作,design <- c(area(1, 1), area(2, 3)) 也没有。如果没有纵横比,您的建议有效,但当 aspect.ratio = 1 无法生成所需的布局时。

标签: r ggplot2 patchwork


【解决方案1】:

我也为使用拼凑而成的具有纵横比的人物而苦苦挣扎。

首先我给你解决方案,下面我给你更多解释为什么我认为这段代码重现了正确的数字。

解决方案:

design <- "
  33
  12
    "
plotlist$fig2 + plotlist$fig3 + plotlist$fig1 + 
    plot_layout(
        design = design, 
        heights = unit(c(2,-1),c("null","null"))
  )


额外信息:

我的一个关键见解是,可以在 heightwidth 参数中指定 -1 NULL 单位,指定具有这种单位的列和行应该适应尺寸具有固定纵横比的图形(我认为...)。

例如,在下面的代码中,我给你一个例子,我想保留一个纵横比为 1 的图形,但我希望能够指定其他列的宽度。这里我指定中间列应该有 3cm 宽度(至少在我截屏时)右列应该尊重图形的纵横比(例如-1 NULLunit),左列可以填充剩余空间(例如.1 NULL单位)。

library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear)) + theme(aspect.ratio = 1)
p1 + p1 + p2 + plot_layout(widths = unit(c(1,3,-1), c("null","cm","null")))

但我不能怪你不知道这一点,因为这似乎没有在任何地方记录。我自己也不是很了解,只是通过查看 patchwork 包的 test 文件夹偶然发现了这个事实。

因此,为了生成您的图形,我指定最后一行应尊重图形的纵横比,同时指定第一行应为高度的两倍。


小心:

请注意该设计是经过仔细挑选的,由于某种原因,以下设计似乎不起作用:

design <- "
  11
  23
    "
plotlist$fig1 + plotlist$fig2 + plotlist$fig3 

这对我来说似乎是一个错误,我会将其作为问题报告给开发人员。

【讨论】:

    猜你喜欢
    • 2021-11-15
    • 1970-01-01
    • 2013-03-06
    • 2020-01-31
    • 2020-07-24
    • 1970-01-01
    • 2021-10-04
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多