Print a list of plots from a list column in a data frame

I have a list column data frame with some plots, similar to this:

exdata <- diamonds %>%
  group_by(cut) %>% 
  nest %>% 
  crossing(dummy = 1:3) %>% 
  mutate(plots = map(.x = data, ~ ggplot(.x, aes(x = x, y = y)) + geom_point()))

Looks like this:

exdata
# A tibble: 15 x 4
   cut       data                  dummy plots 
   <ord>     <list>                <int> <list>
 1 Fair      <tibble [1,610 × 9]>      1 <gg>  
 2 Fair      <tibble [1,610 × 9]>      2 <gg>  
 3 Fair      <tibble [1,610 × 9]>      3 <gg>  
 4 Good      <tibble [4,906 × 9]>      1 <gg>  
 5 Good      <tibble [4,906 × 9]>      2 <gg>  
 6 Good      <tibble [4,906 × 9]>      3 <gg>  
 7 Very Good <tibble [12,082 × 9]>     1 <gg>  
 8 Very Good <tibble [12,082 × 9]>     2 <gg>  
 9 Very Good <tibble [12,082 × 9]>     3 <gg>  
10 Premium   <tibble [13,791 × 9]>     1 <gg>  
11 Premium   <tibble [13,791 × 9]>     2 <gg>  
12 Premium   <tibble [13,791 × 9]>     3 <gg>  
13 Ideal     <tibble [21,551 × 9]>     1 <gg>  
14 Ideal     <tibble [21,551 × 9]>     2 <gg>  
15 Ideal     <tibble [21,551 × 9]>     3 <gg>  

I would like to plot these in a similar outcome to using facet_grid based on columns of cut and dummy of dummys. The first column would be all the plots for Fair, the second for Good, the third for Very good etc. The rows would be titled '1', '2' and '3' for each value in dummy. A 3 * 5 grid.

I tried the Patchwork package but this does not have a 'direct' way of doing this. Ideally I'd have the column titles clearly labelled with the corresponding cut and then the rows with the corresponding rows.

Note my particular use case prevents me from building a master data frame and wrangling it to use facet grid in the desired manner, I'm really stuck with a list column in a data frame like above.

How can I assemble my list column of plots into a 3*5 grid with clear column and row titles, like with facet_grid?

1 Like

Please use reprex. I made one, and I'll see if I can figure out the 5 in the 3x5 grid.

suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
  library(purrr)
  library(tidyr)
})

exdata <- diamonds %>%
  group_by(cut) %>% 
  nest %>% 
  crossing(dummy = 1:3) %>% 
  mutate(plots = map(.x = data, ~ ggplot(.x, aes(x = x, y = y)) + geom_point()))

exdata[1,4][[1]]
#> [[1]]

Hi, I don't understand your response?!
Btw what is reprex? I thought my code block did make a reproducible example with variable exdata?

That's what I did. The point is that it's cut-and-paste. Even though exdata was there it had to first be converted to csv and read in to be usable. See the FAQ: How to do a minimal reproducible example reprex for beginners.

Git it. I read the linked post you shared and interpreted it as being mostly about data frames for reproducibility. I used diamonds above which I thought was inbuilt but seems it's actually loaded with tidyverse.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.