ggplot2facet-wrapggforce

Separating out facet_wrap using facet_wrap_paginate


I have never used facet_wrap_paginate before and am not finding any good examples of how to use it. Normally I use facet_wrap but save it as one plot and just increase the height and width.

Below I have put a simple example of the code I normally use to produce/save plots. Please note this is a very reduced version for the sake of simplicity.

   Draft <- 
   ggplot(aes(x, y) + 
   geom_boxplot() 

   ggsave(plot = Draft, "./Draft.jpeg", width = 15, height = 10, units = "cm")

However, I want to produce a multi-page PDF for the separated out by plot, using facet_wrap_paginate. When I ran the code (example below) I recieved no errors but was unable to find the output anywhere.

   Draft <- 
   ggplot(aes(x, y) + 
   geom_boxplot() +
   facet_wrap_paginate(~ chemical) 

Solution

  • My understanding is, that ggforce::facet_wrap_paginate() will produce separate plots of subsets of facets. You'd still have to place them in a pdf.

    According to your description, you want plots of subsets of your dataset on separate pages of a PDF document.

    Here is an example of how to do that without facet_wrap_paginate() in a quarto document. The idea is to split the dataset into subsets, then plot them with a fig.height so that each plot takes up one page.

    ---
    title: "Untitled"
    format: pdf
    editor: visual
    execute:
      echo: false
      message: false
      warning: false
    ---
    
    ```{r}
    library(tidyverse)
    ```
    
    ```{r}
    mtcars_cyl_split <- 
      mtcars |> 
      group_by(cyl) |> 
      group_split()
    ```
    
    ```{r}
    #| fig-height: 6
    walk(mtcars_cyl_split,
        \(x) print(ggplot(x, aes(mpg, disp, group = cyl)) +
      geom_boxplot()))
    ```