From 27bffa864ac6ebfa9bc5ad4c577d5da02cd3bac4 Mon Sep 17 00:00:00 2001 From: Chris Sobczak Date: Fri, 12 Jun 2026 11:30:56 -0700 Subject: Add save function --- R/saveplot.R | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 R/saveplot.R (limited to 'R') diff --git a/R/saveplot.R b/R/saveplot.R new file mode 100644 index 0000000..8c5677c --- /dev/null +++ b/R/saveplot.R @@ -0,0 +1,40 @@ +#' Save a ggplot object +#' +#' Standard method for saving ggplot objects for best +#' quality for the desired use case +#' +#' @param p ggplot object you want to save +#' @param filename The name of the file you want to export to +#' @param units The units for dimensions of the plot (default inches) +#' @param width Width of the plot in units (default 10) +#' @param height Height of the plot in units (default 10) +#' @param dpi Dots per inch (detault 300) +#' +#' @return No return value +#' +#' @examples +#' df <- data.frame(x = rnorm(100)) +#' p <- ggplot2::ggplot(data = df, ggplot2::aes(x = x)) + ggplot2::geom_histogram() +#' saveplot(p, filename = 'plot.png') +#' +#' @import ggplot2 +#' @import ragg +#' @export +saveplot <- function( + p, + filename, + units = 'in', + width = 10, + height = 10, + dpi = 600 +){ + ggplot2::ggsave( + filename = filename, + plot = p, + width = width, + height = height, + units = units, + dpi = dpi, + device = ragg::agg_png + ) +} -- cgit v1.2.3