<- pmplots_data_obs()
data <- dv_pred(data)
p p
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:
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.
<- list(
p dv_pred(data),
dv_ipred(data),
cwres_time(data)
)
<- mrggsave(p, stem = "diagnostics", onefile = FALSE) ans
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:
<- mrggsave(p, stem = "png-example", dev = "png")
a
basename(a)
[1] "png-example001.png" "png-example002.png" "png-example003.png"
Or save to multiple devices:
<- mrggsave(p[[2]], stem = "multi-devices", dev = "pdf,png")
b
basename(b)
[1] "multi-devices.pdf" "multi-devices.png"
6 String interpolation
mrggsave helps form file names as well:
<- 100
run
<- mrggsave(p[[1]], stem = "diagnostics-{run}")
a
basename(a)
[1] "diagnostics-100.pdf"