A wrapper around ggsave
that facilitates saving ggplot objects within knitr
documents. Automatically handles file naming and directory creation, with
support for multiple output formats.
Arguments
- gg_obj
The ggplot to be written. If not specified, the last ggplot created will be written.
- file
The name of the file to be written. If not specified, the label of the code block will be used.
- target_dir
The directory where the file will be written. If not specified, the current working directory will be used.
- device
The device to be used for saving the ggplot. Options include "pdf" (default), "png", "jpeg", "tiff", and "svg".
- theme
The ggplot2 theme to be applied to the ggplot. Default is the theme specified in the ggplot2 options.
- ...
Additional arguments to be passed to the
ggsave
function from theggplot2
package.
Details
This function extends ggplot2::ggsave
by:
Using knitr code block labels for automatic file naming
Creating target directories if they don't exist
Supporting multiple output formats (PDF, PNG, JPEG, TIFF, SVG)
Applying custom themes to plots before saving