Introduction to mrggsave

Save figures with annotation.

mrggsave

1 Introduction

The mrggsave package can be used to save images in bulk and annotate figures with the file names of the source code and the output image.

To illustrate, we make some plots using the pmplots package. Here’s a plot of DV versus PRED:

data <- pmplots_data_obs()
p <- dv_pred(data)
p

2 Save a plot

We can save this plot out to a pdf, with annotation, using the mrggsave() function; we pass in a stem to create the output file name and directory as well as the source code file name.

mrggsave(p, stem = "intro-1", dir = tempdir(), script = "mrggsave.Rmd")

The idea here is that the plot gets annotated with the output file name (intro-1.pdf) and the plot gets saved to this file, ensuring that the annotation is consistent with the output file name.

Note also that, by default, the plot is saved in pdf format and mrggsave takes care of adding the extension.

3 Set some options

Because we tend to save lots of images in one script, we can set some options to make the command simpler:

options(mrg.script = "mrggsave.Rmd", mrggsave.dir = tempdir())

Now the call is just:

mrggsave(p, stem = "intro-1")

4 Save a list of plots

If we have a batch of plots in a list, we can just pass that to mrggsave.

p <- list(
  dv_pred(data), 
  dv_ipred(data), 
  cwres_time(data)
)

ans <- mrggsave(p, stem = "diagnostics", onefile = FALSE)

By default, mrggsave saves the plots in one file; here we’ve asked that each image get saved to a separate file.

basename(ans)
[1] "diagnostics001.pdf" "diagnostics002.pdf" "diagnostics003.pdf"

5 Save to a different device

We can save figures to other formats, for example, png format:

a <- mrggsave(p, stem = "png-example", dev = "png")

basename(a)
[1] "png-example001.png" "png-example002.png" "png-example003.png"

Or save to multiple devices:

b <- mrggsave(p[[2]], stem = "multi-devices", dev = "pdf,png")

basename(b)
[1] "multi-devices.pdf" "multi-devices.png"

6 String interpolation

mrggsave helps form file names as well:

run <- 100

a <- mrggsave(p[[1]], stem = "diagnostics-{run}")

basename(a)
[1] "diagnostics-100.pdf"