简体   繁体   English

在 plot window 中安排多个 ggplots

[英]Arranging multiple ggplots in the plot window

I have multiple ggplots and want to arrange them in the plot window in a rectangular fashion.我有多个ggplots ,想以矩形方式将它们排列在 plot window 中。 Below is my code下面是我的代码

library(ggplot2)
library(ggpubr)
plot1 = ggplot(data.frame(x = 1:3, y = 4:6), aes(x = x, y = y)) + geom_line()
plot2 = ggplot(data.frame(x = 1:3, y = 4:6), aes(x = x, y = y)) + geom_line()
plot3 = ggplot(data.frame(x = 1:3, y = 4:6), aes(x = x, y = y)) + geom_line()
ggarrange(plot1, plot2, plot3, ncol = 2, nrow = 2, align = 'h')

I am getting below arrangement我得到低于安排

在此处输入图像描述

I am wondering if the 3rd plot can be placed in the middle of the plot window?我想知道是否可以将第 3 个 plot 放在 plot window 的中间?

Any pointer will be very helpful.任何指针都会非常有帮助。

This is very easy to do in patchwork :这在patchwork中很容易做到:

library(patchwork)

(plot1 + plot2) / plot3

在此处输入图像描述

Or或者

wrap_plots(A = plot1, B = plot2, C = plot3, design = "AABB\n#CC#")

在此处输入图像描述

library(patchwork)
design <- 
  "1122
   1122
   #33#
   #33#"
plot1 + plot2 + plot3 + plot_layout(design = design)

在此处输入图像描述

I adapted code from http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/81-ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/我改编了http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/81-ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-中的代码页/

The first option puts one plot on top and two on bottom, second option puts 2 on top and one on bottom.第一个选项将一个 plot 放在顶部,两个放在底部,第二个选项将 2 放在顶部,一个在底部。

Hope this helps - check out the webpage for further options.希望这会有所帮助 - 查看网页以获取更多选项。

library(ggplot2)
library(ggpubr)
plot1 = ggplot(data.frame(x = 1:3, y = 4:6), aes(x = x, y = y)) + geom_line()
plot2 = ggplot(data.frame(x = 1:3, y = 4:6), aes(x = x, y = y)) + geom_line()
plot3 = ggplot(data.frame(x = 1:3, y = 4:6), aes(x = x, y = y)) + geom_line()
ggarrange(plot1,                                                 
          ggarrange(plot2, plot3, ncol = 2, labels = c("plot2", "plot3")), 
          nrow = 2, 
          labels = "plot1"                                        
)

library("cowplot")
ggdraw() +
  draw_plot(plot1, x = 0, y = .5, width = .5, height = .5) +
  draw_plot(plot2, x = .5, y = .5, width = .5, height = .5) +
  draw_plot(plot3, x = 0, y = 0, width = 1, height = 0.5) +
  draw_plot_label(label = c("plot1", "plot2", "plot3"), size = 15,
                  x = c(0, 0.5, 0), y = c(1, 1, 0.5))    

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

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