Bogaso
Bogaso

Reputation: 3308

Arranging multiple ggplots in the plot window

I have multiple ggplots and want to arrange them in the plot window in a rectangular fashion. 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

enter image description here

I am wondering if the 3rd plot can be placed in the middle of the plot window?

Any pointer will be very helpful.

Upvotes: 0

Views: 3161

Answers (3)

Allan Cameron
Allan Cameron

Reputation: 173793

This is very easy to do in patchwork:

library(patchwork)

(plot1 + plot2) / plot3

enter image description here

Or

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

enter image description here

Upvotes: 2

Laura Jamison
Laura Jamison

Reputation: 133

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/

The first option puts one plot on top and two on bottom, second option puts 2 on top and one on bottom.

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

Upvotes: 0

Jon Spring
Jon Spring

Reputation: 66425

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

enter image description here

Upvotes: 1

Related Questions