Getting Markdown to Output Multiple Plots?

Hi All! And thanks in advance for your help.

I have a script that generates anywhere between 2 and 30 plot arrangements based on the input data, which will constantly change. How do I tell Markdown to get all of the plots and output them?

Any help, or even a hint as to what direction to begin my search with, would be greatly appreciated!!

1 Like

Hi, and welcome!

Take a look at the patchwork package.

library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle('Plot 2')

p3 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')

p4 <- ggplot(mtcars) + 
  geom_bar(aes(gear)) + 
  facet_wrap(~cyl) + 
  ggtitle('Plot 4')
  
p1 + p2 + p3 + p4

Created on 2020-02-28 by the reprex package (v0.3.0)

Thank you for the response! I have moved the entire code into a chunk in the R-Markdown document, but print() is only sending text to the PDF, when I try to use "print(figure)" to print the gg-arrangement nothing appears in the Markdown-generated PDF.

 figure <- ggarrange(court_receptions, court_perfect, court_good, court_medium, court_bad, labels = c("All Receptions", " Perfect Pass", "  Good Pass", "Medium Pass", "   Bad Pass"), 
                          font.label = list(size = 8, color="white"), vjust = 2.8, hjust = -0.8, align = "hv", ncol = 3, nrow = 2)
      
      plot_title <- paste(attackers[z], "- Serve Receive Attacking")
      annotate_figure(figure, top = text_grob(plot_title, color = "black", face = "bold", size = 12))
      print(figure)

I can't help with ggarrange without a reprex (see the (FAQ: What's a reproducible example (`reprex`) and how do I create one?). patchwork does work. The following was produced with the default Rmd template with output: pdf-document and the greyed area is the chunk.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.