help using walk2 to save multiple graphs

Hi all,
I'm trying to efficiently save multiple graphs for an upcoming manuscript. My code isn't working, but I'm at a loss as to why...any help would be appreciated!

Reproducible example:

# Create list one, the list of objects
figs = list(plot_1=ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(), 
            plot_2=ggplot(faithful, aes(waiting, eruptions))+

# create list 2, the list of file names
fig_names = figs |>
  names() |> 
  map(paste0, ".png")

# pass both to purrr to use ggsave iteratively over both lists once and save all graphs with one command
walk2(figs, fig_names, ~ggsave(plot = figs, filename = fig_names, 
                               path=here::here("Figures and Tables"),
                               device = "png", dpi = 300))

The script "saves" plot_1, but it this png file is actually blank when I open it. Also, it doesn't save the second plot and gives me the following error: Error in UseMethod("grid.draw") : no applicable method for grid.draw applied to an object of class "list"

What if you swap out "figs" and "fig_names" in your .f argument inside walk2 to .x and .y respectively?

walk2(figs, fig_names, ~ggsave(plot = .x, filename = .y, 
                               path=here::here("Figures and Tables"),
                               device = "png", dpi = 300))

That works! Great, thank you!

This topic was automatically closed 7 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.