summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Sobczak <chris@sobczak.family>2026-06-12 11:30:56 -0700
committerChris Sobczak <chris@sobczak.family>2026-06-12 11:30:56 -0700
commit27bffa864ac6ebfa9bc5ad4c577d5da02cd3bac4 (patch)
treee70565988c60f18c3e8e47702d70bb420ed7f203
parente7fb3801a7bfc507fb6eb86101741d9d41fd8fd0 (diff)
Add save function
-rw-r--r--DESCRIPTION1
-rw-r--r--NAMESPACE2
-rw-r--r--R/saveplot.R40
-rw-r--r--man/saveplot.Rd34
4 files changed, 77 insertions, 0 deletions
diff --git a/DESCRIPTION b/DESCRIPTION
index 8bc7286..0152b88 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -10,6 +10,7 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 8.0.0.9000
Imports:
ggplot2,
+ ragg,
reshape2
Suggests:
testthat (>= 3.0.0)
diff --git a/NAMESPACE b/NAMESPACE
index 713d046..6f4e5ca 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -2,5 +2,7 @@
export(corplot)
export(gg_csobczak)
+export(saveplot)
import(ggplot2)
+import(ragg)
importFrom(reshape2,melt)
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
+ )
+}
diff --git a/man/saveplot.Rd b/man/saveplot.Rd
new file mode 100644
index 0000000..170e077
--- /dev/null
+++ b/man/saveplot.Rd
@@ -0,0 +1,34 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/saveplot.R
+\name{saveplot}
+\alias{saveplot}
+\title{Save a ggplot object}
+\usage{
+saveplot(p, filename, units = "in", width = 10, height = 10, dpi = 600)
+}
+\arguments{
+\item{p}{ggplot object you want to save}
+
+\item{filename}{The name of the file you want to export to}
+
+\item{units}{The units for dimensions of the plot (default inches)}
+
+\item{width}{Width of the plot in units (default 10)}
+
+\item{height}{Height of the plot in units (default 10)}
+
+\item{dpi}{Dots per inch (detault 300)}
+}
+\value{
+No return value
+}
+\description{
+Standard method for saving ggplot objects for best
+quality for the desired use case
+}
+\examples{
+df <- data.frame(x = rnorm(100))
+p <- ggplot2::ggplot(data = df, ggplot2::aes(x = x)) + ggplot2::geom_histogram()
+saveplot(p, filename = 'plot.png')
+
+}