Hi so I make a lot of graphs and was wondering if I was doing it in a fast, efficient way. I need to make visualizations commonly that are commonly ~2000 pages long (one for each unit of analysis)
I tried to make a toy example here that writes to a temp directory.
library(ggplot2)
library(furrr)
library(dplyr)
plan("multisession")
# define graphing function
plot_graph <- function(dt) {
dt |> ggplot(aes(x = x, y = y)) +
geom_point()
}
# create dataset
a <- 2e3
b <- 1e3
dt <- data.frame(
id = rep(1:a, each = b),
x = runif(a*b),
y = runif(a*b)
)
# create list of plots
list_plots <- dt |>
split(f = "id") |>
purrr::map(plot_graph)
# set up temp dir
dir_out_tmp <- tempdir()
filename_out <- "temp"
# save graphs to temp dir
furrr::future_iwalk(
list_plots,
~withr::with_pdf(
new = fs::path(dir_out_tmp, paste(filename_out, .y, sep = "-"), ext = "pdf"),
width = 15,
height = 8,
code = plot(.x)
)
)
# check for files
files_temp <- fs::path(dir_out_tmp, paste(filename_out, names(list_plots), sep = "-"), ext = "pdf")
stopifnot(all(fs::file_exists(files_temp)))
# combine graphs
qpdf::pdf_combine(
input = files_temp,
output = fs::path(dir_out_tmp, filename_out, ext = "pdf")
)
Basically the code saves individual plots in the temp directory and in real applications, collates those to save in the actual output folder.
This is pretty fast, but I'm wondering if there are tools to just save the multipage pdf from the list of plots directly?
EDIT: I've tried ggsave and it is much slower
purrr::walk(list_plots, print). Rendering as a pdf should then create all your graphs and combine them into the final file.ggsave()but it was just so slow