1

I'd like to arrange a series of plot generated through the openair package in a grid. Here's an example from the openair book

library(openair)
polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(0, 10))
polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(10, 20))
polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(20, 30))
polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(30, 40))
polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(40, 50))
polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(50, 60))
polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(60, 70))
polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(70, 80))
polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(80, 90))
polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(90, 100))

I tried par(mfrow=c(5, 5)) as well as gridExtra::grid.arrange(plot1,plot2,...). Neither of them work. I noticed that in the boookdown book code the plots are nicely arranged in a grid.

Thomas Speidel
  • 1,369
  • 1
  • 14
  • 26
  • Forgive me, but I don't understand :-( The first link points to section 8 where the polarplots are displayed: https://bookdown.org/david_carslaw/openair/sec-polarPlot.html#conditional-probability-function-cpf-plot The second link points to the Rmd of the same section, where on line 149 the full code chunk is shown: https://github.com/davidcarslaw/openair-book/blob/master/polar-plots.Rmd – Thomas Speidel Jul 07 '21 at 20:37
  • 1
    At the bottom of the bookdown section 8 points back to [multiple_plots](https://bookdown.org/david_carslaw/openair/sec-openair-package.html#sec:multiple-plots-page) section 2.8 that maybe gets us there. I would have chosen the par(mfrow approach, but appears he goes about it differently. – Chris Jul 07 '21 at 20:48
  • Oh wow, I completely missed that. I don't think I would have figured out to use `print()` this way. – Thomas Speidel Jul 07 '21 at 20:57
  • 1
    Me neither, though I suppose we should expect it under the 'there's always another way to do it paradigm'. Now I'll read his docs too! – Chris Jul 07 '21 at 21:25

1 Answers1

1

When running openair::polarPlot, the console shows this message:

this contains:
    a single data frame:
    $data [with no subset structure]
    a single plot object:
    $plot [with no subset structure]

If you save the plot as an object, you can call just the plot using $plot:

object1 <- polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(0, 10))
object2 <- polarPlot(mydata, poll = "so2", stati = "cpf", percentile = c(10, 20))

object1$plot
object2$plot

You can then arrange and export the plots as you would with any plot objects such as with ggarrange and ggsave (or whichever functions you prefer).

ggarrange(object1$plot, object2$plot) %>%
ggsave('file.png')

Ben

Ben Nikkel
  • 11
  • 2