R/lazy_ggsave.R

Defines functions lazy_ggsave

Documented in lazy_ggsave

#' Save a ggplot object as a figure, with some defaults (PNG and PDF only)
#'
#' @param plot A ggplot object
#' @param filename Filename (with no extensions!)
#' @param width Width of the figure (in cm)
#' @param height Height of the figure (in cm)
#'
#' @return Does not return anything, just saves a file to disk
#'
#' @importFrom ggplot2 ggsave
#'
#' @export
#'

lazy_ggsave <- function(plot, filename, width = 14, height = 10){
  # Save as png
  ggplot2::ggsave(plot = plot,
                  filename = here::here("results", "img", paste0(filename, ".png")),
                  width = width,
                  height = height,
                  units = "cm")

  # Save as pdf
  ggplot2::ggsave(plot = plot,
                  filename = here::here("results", "img", paste0(filename, ".pdf")),
                  width = width,
                  height = height,
                  units = "cm")
}
jcvdav/startR documentation built on May 2, 2024, 8:30 p.m.