Split plot into individual plots
We build a function along these steps :
- We go through the structure of the object to get the names of the variables used for faceting (here
'z'
).
- We overwrite the
facet
element of our plot object with the one from the empty ggplot
object (so if we print it at this stage facets are gone).
- We extract the data and split it along the variables we identified in 1st step.
- We overwrite the original data with each subset (
12
times here) and store all outputs in a list.
code
splitFacet <- function(x){
facet_vars <- names(x$facet$params$facets) # 1
x$facet <- ggplot2::ggplot()$facet # 2
datasets <- split(x$data, x$data[facet_vars]) # 3
new_plots <- lapply(datasets,function(new_data) { # 4
x$data <- new_data
x})
}
new_plots <- splitFacet(myplot)
length(new_plots) # [1] 12
new_plots[[3]] # 3rd plot

Split plot into faceted plots of n
subplots max
If we want to keep the facets but have less plots by facet we can skip step 2, and rework our split instead so it includes several values of the variables used for faceting.
Rather than making a separate function we'll generalize the 1st, n
is the number of facets you get by plot.
n = NULL
means you get the previous output, which is slightly different from n = 1
(one facet by plot).
splitFacet <- function(x, n = NULL){
facet_vars <- names(x$facet$params$facets) # 1
if(is.null(n)){
x$facet <- ggplot2::ggplot()$facet # 2a
datasets <- split(x$data, x$data[facet_vars]) # 3a
} else {
inter0 <- interaction(x$data[facet_vars], drop = TRUE) # 2b
inter <- ceiling(as.numeric(inter0)/n)
datasets <- split(x$data, inter) # 3b
}
new_plots <- lapply(datasets,function(new_data) { # 4
x$data <- new_data
x})
}
new_plots2 <- splitFacet(myplot,4)
length(new_plots2) # [1] 3
new_plots2[[2]]

This might come in handy too :
unfacet <- function(x){
x$facet <- ggplot2::ggplot()$facet
x
}
The tidy way
If the code is available, no need to go through all this trouble, we can split the data before feeding it to ggplot
:
library(tidyverse)
myplots3 <-
df %>%
split(ceiling(group_indices(.,z)/n_facets)) %>%
map(~ggplot(.,aes(x =x, y=y))+geom_point()+facet_wrap(~z))
myplots3[[3]]
