R/export_plot.r

Defines functions export_plot_svg export_plot_pdf

Documented in export_plot_pdf export_plot_svg

#' Export plot to a PDF file
#'
#' @description This function exports a plot to a PDF file. The default plot
#'   dimension (width/height) follows the golden ratio, 1.618.
#'
#' @param plot An object of the `ggplot` class
#' @param file Output file directory
#' @param width Plot width in inches
#' @param height Plot height in inches
#'
#' @return This function outputs an object of the `ggplot` class to a PDF file.
#'
#' @author Kevin Surya
#'
#' @import Cairo
#' @importFrom grDevices graphics.off
#'
#' @export
#'
#' @references \href{https://cran.r-project.org/web/packages/Cairo/index.html}{Urbanek and Horner (2022)}
#'
export_plot_pdf <- function(plot, file, width = 4.75, height = 2.94) {
  CairoPDF(width = width, height = height, file = file)
  print(plot)
  graphics.off()
}

#' Export plot to a SVG file
#'
#' @description This function exports a plot to a SVG file. The default plot
#'   dimension (width/height) follows the golden ratio, 1.618.
#'
#' @param plot An object of the `ggplot` class
#' @param file Output file directory
#' @param width Plot width in inches
#' @param height Plot height in inches
#'
#' @return This function outputs an object of the `ggplot` class to a SVG file.
#'
#' @author Kevin Surya
#'
#' @import Cairo svglite
#' @importFrom grDevices graphics.off
#'
#' @export
#'
#' @references \href{https://cran.r-project.org/web/packages/Cairo/index.html}{Urbanek and Horner (2022)}
#'
export_plot_svg <- function(plot, file, width = 4.75, height = 2.94) {
  CairoSVG(width = width, height = height, file = file)
  print(plot)
  graphics.off()
}
suryakevin/drugcandy documentation built on May 6, 2022, 6:37 p.m.