Skip to content

ggsave with Metadata

The ggsave_with_metadata() function in the reportifyr package is an extension of the ggsave() function from the ggplot2 package. It saves plot objects and simultaneously captures analysis-relevant metadata in a .json file via reportifyr’s write_object_metadata() function.


ggsave_with_metadata()

R: Wrapper around the ggplot2 ggsave function. Saves a ggplot...
ggsave_with_metadataR Documentation

Description

Extension to the ggsave() function that allows capturing object metadata as a separate .json file.

Usage

ggsave_with_metadata(
  filename,
  plot = ggplot2::last_plot(),
  meta_type = "NA",
  meta_equations = NULL,
  meta_notes = NULL,
  meta_abbrevs = NULL,
  ...
)

Arguments

filename

The filename for the plot to save to.

plot

The plot object to save. Default is the last displayed plot (ggplot2::last_plot()).

meta_type

A string to specify the type of object. Default is "NA".

meta_equations

A string or vector of strings representing equations to include in the metadata. Default is NULL.

meta_notes

A string or vector of strings representing notes to include in the metadata. Default is NULL.

meta_abbrevs

A string or vector of strings representing abbreviations to include in the metadata. Default is NULL.

...

Additional arguments passed to the ggplot2::ggsave() function.

Examples

## Not run: 

# ---------------------------------------------------------------------------
# Construct and save a simple ggplot
# ---------------------------------------------------------------------------
g <- ggplot2::ggplot(
  data = Theoph,
  ggplot2::aes(x = Time, y = conc, group = Subject)
) +
  ggplot2::geom_point() +
  ggplot2::geom_line() +
  ggplot2::theme_bw()

# Save a png using the wrapper function
figures_path <- here::here("OUTPUTS", "figures")
plot_file_name <- "01-12345-pk-timecourse1.png"
ggsave_with_metadata(filename = file.path(figures_path, plot_file_name))

## End(Not run)

Returns

  • Plot File: A plot saved according to the specified filename and ... provided.
  • Metadata File: A .json file saved with metadata.

Additionally, a prompt confirms that an image with dimensions H x W (height x width) inches has been successfully saved to the specified filename.

If the source file is not tracked by Git, a similar warning is displayed:

2025-01-10 18:30:00 [WARN] Source file path not tracked by git: /cluster-data/user-homes/user/project/scripts/analysis.R

Logging

  • Debug: Logs the start and end of the function, as well as metadata validation and writing.
  • Info: Logs a confirmation of plot saving and metadata capture.

Additional Resources

  • reportifyr GitHub: Refer to the reportifyr repository for more details.
  • ggsave Documentation: Refer to the ggplot2::ggsave() function for additional arguments to pass to ggsave_with_metadata().
  • Write Object Metadata Reference: Refer to the reference on write_object_metadata() here.