class: center, middle, inverse, title-slide # Putting plots together ### Abhijit Dasgupta, PhD --- layout: true <div class="my-header"> <span>BIOF 339: Practical R</span></div> --- class: middle, inverse # Putting plots together --- ## The packages There are three excellent packages for putting separate `ggplot` graphs together in panels. 1. **ggpubr** 1. **cowplot** 1. **patchwork** --- ## The graphs ```r plt1 <- ggplot(penguins, aes(x = species, y = body_mass_g, fill=species)) + geom_boxplot() plt2 <- ggplot(penguins, aes(x = bill_length_mm, y = body_mass_g, color = species))+ geom_point() plt3 <- ggplot(penguins, aes(x = bill_length_mm, y = flipper_length_mm, color = species))+ geom_smooth(se=F) ``` ![](06-patchwork_files/figure-html/unnamed-chunk-2-1.png)<!-- --> --- ## ggpubr ```r ggarrange(plt1, plt2, plt3, ncol = 2, nrow=2) ``` ![](06-patchwork_files/figure-html/unnamed-chunk-3-1.png)<!-- --> --- ## ggpubr ```r ggarrange(plt1, plt2, plt3, ncol = 2, nrow=2, common.legend = TRUE) ``` ![](06-patchwork_files/figure-html/unnamed-chunk-4-1.png)<!-- --> --- ## cowplot ```r cowplot::plot_grid(plt1, plt2, plt3, nrow = 2, ncol = 2) ``` ![](06-patchwork_files/figure-html/unnamed-chunk-5-1.png)<!-- --> --- ## cowplot ```r cowplot::plot_grid(plt1, plt2, plt3, nrow = 2, ncol = 2, labels = c('A','B','C')) ``` ![](06-patchwork_files/figure-html/unnamed-chunk-6-1.png)<!-- --> --- ## cowplot ```r grid1 = cowplot::plot_grid(plt1, plt2, nrow = 1, rel_widths=c(0.3, 0.7), labels=c('A','B')) cowplot::plot_grid(grid1, plt3, nrow=2, labels = c('', 'C')) ``` ![](06-patchwork_files/figure-html/unnamed-chunk-7-1.png)<!-- --> --- ## patchwork ```r plt1 | plt2 / plt3 ``` ![](06-patchwork_files/figure-html/unnamed-chunk-8-1.png)<!-- --> --- ## patchwork ```r (plt1 | plt2) / plt3 ``` ![](06-patchwork_files/figure-html/unnamed-chunk-9-1.png)<!-- --> --- ## patchwork ```r plt1 + plt2 + plt3 + plot_layout(ncol = 2) ``` ![](06-patchwork_files/figure-html/unnamed-chunk-10-1.png)<!-- --> --- ## Further references ggpubr: [https://rpkgs.datanovia.com/ggpubr/](https://rpkgs.datanovia.com/ggpubr/) cowplot: [https://wilkelab.org/cowplot/index.html](https://wilkelab.org/cowplot/index.html) and Fundamentals of Data Visualization patchwork: [https://patchwork.data-imaginist.com/index.html](https://patchwork.data-imaginist.com/index.html)