简体   繁体   English

控制使用拼凑创建的子图的大小 - 代码改进

[英]Control size of subplots created with patchwork - code improvement

I want to make a multipanel figure containing multiple labeled plots.我想制作一个包含多个标记图的多面板图形。 My plots are produced in ggplot2 & I would like to arrange them with patchwork.我的地块是在 ggplot2 生产的,我想把它们拼凑起来。

I want to combine two subplots in the first row, followed by two other plots arranged one plot per row:我想合并第一行中的两个子图,然后是另外两个图,每行排列一个 plot:

  • plot3 + plot4 - both in row #1 plot3 + plot4 - 都在第 1 行
  • plot1 - in row #2 plot1 - 在第 2 行
  • plot2 - in row #3 plot2 - 在第 3 行

Here is a dummy example to illustrate the problem:这是一个虚拟示例来说明问题:

```{r, fig.width=10, fig.height=13}
library(ggplot2)
library(patchwork)

#Dummy plots
plot1 <- ggplot2::ggplot(data = mpg, aes(x = class, fill=drv)) +
  geom_bar(aes(y = ..count..)) + ggplot2::ggtitle("Plot1")
plot2 <- ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, color=class)) +
  geom_point()+ ggplot2::ggtitle("Plot2")
plot3 <- ggplot2::ggplot(data = mpg, aes(x = cty)) +
  geom_density()+ ggplot2::ggtitle("Plot3")
plot4 <- ggplot2::ggplot(data = mpg, aes(x = cty, y=drv, fill = fl)) +
  geom_col()+ ggplot2::ggtitle("Plot4")

# this works, but it is not the desired layout
final <- plot1 + plot2 + {plot3 + plot4 + patchwork::plot_layout(ncol=2)} + 
  patchwork::plot_layout(ncol=1,heights = unit(c(4, 7, 2),c('cm')))
plot(final)

#this does not work
final2 <- {plot3 + plot4 + patchwork::plot_layout(ncol=2)} + plot1 + plot2 + 
  patchwork::plot_layout(ncol=1, heights = unit(c(2, 4, 7),c('cm')))
print(final2)

```

This is the output I can produce, but this is not what I want :这是我可以生成的 output,但这不是我想要的:

在此处输入图像描述

And this is the picture I would like to obtain :这是我想要获得的图片:

在此处输入图像描述

Some of my other attempts:我的一些其他尝试:

#this does not work either
up_patch <- plot3 + plot4 + patchwork::plot_layout(ncol=2)

final2 <- up_patch + plot1 + plot2 + patchwork::plot_layout(ncol=1, heights = unit(c(2, 4, 7),c('cm')))
print(final2)


#and this as well
up_patch <- plot3 + plot4 + patchwork::plot_layout(ncol=2, heights= unit(2,c('cm')))
bottom_patch <- plot1 + plot2 + patchwork::plot_layout(ncol=1, heights = unit(c(4, 7),c('cm')))

final2 <- up_patch + bottom_patch
print(final2)

# THIS WORKS but needs improvement
final_desired <- (plot3 | plot4) / plot1 + plot2

print(final_desired)

In the last attempt I was able to produce the desired layout, however I would like to be able to control the dimensions of the subplots as in my dummy example in the beginning of this post).在最后一次尝试中,我能够生成所需的布局,但是我希望能够像本文开头的虚拟示例一样控制子图的尺寸)。 It is important for me to adjust the image size to the size of the page.将图像大小调整为页面大小对我来说很重要。

I would also like to know how to use a namespace qualifier while calling patchwork in the working example, so I would not call a function from another package by an accident.我还想知道如何在工作示例中调用 patchwork 时使用命名空间限定符,所以我不会意外地从另一个 package 调用 function。

I followed the instructions from these sources:我遵循了这些来源的说明:

https://ggplot2-book.org/arranging-plots.html https://patchwork.data-imaginist.com/articles/guides/layout.html - (footnote: I do not understand the textual representation of layout) Combine multiple patchworks https://ggplot2-book.org/arranging-plots.html https://patchwork.data-imaginist.com/articles/guides/layout.html - (脚注:我不明白布局的文本表示) 组合多个错落有致

One option would be to use the design argument to specify the layout:一种选择是使用design参数来指定布局:

library(ggplot2)
library(patchwork)

design = "
CD
AA
BB
"

plot1 + plot2 + plot3 + plot4 + 
  plot_layout(
    design = design, 
    heights = c(2, 4, 7))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM