diff --git a/DESCRIPTION b/DESCRIPTION index 3315362..0f48090 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,19 +21,21 @@ Imports: ggplot2, scales, cli, - rlang + rlang, + dplyr Suggests: - dplyr, - knitr, - rmarkdown, - tibble, - tidyr, - glue, - purrr, - gapminder, - stringr, - testthat (>= 2.1.0), - plotly, - gt + knitr, + rmarkdown, + tibble, + tidyr, + glue, + purrr, + gapminder, + stringr, + testthat (>= 2.1.0), + plotly, + gt, + svglite (>= 2.1.2), + ragg (>= 1.2.6) VignetteBuilder: knitr Roxygen: list(markdown = TRUE) diff --git a/NAMESPACE b/NAMESPACE index 5ef7d92..6b996da 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(mm_to_inch) +export(save_govuk) export(scale_colour_continuous_af) export(scale_colour_discrete_af) export(scale_fill_continuous_af) diff --git a/R/utils.R b/R/utils.R index 977724a..de9c07a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -17,3 +17,75 @@ mm_to_inch <- function(x) { x / 25.4 } + + +#' Save a plot at the correct dimensions for publishing on GOVUK +#' +#' @description +#' This is a wrapper around [ggplot2::ggsave()] with plot dimensions set for +#' publishing on GOVUK. +#' +#' @param filename File name +#' @param plot The plot to save +#' @param device File type to produce (svg, png or jpg). svg is preferred as +#' it scales well without pixellating +#' @param path Directory to save the plot in +#' @param ... Other params passed to ggplot::ggsave +#' +#' @export +#' +#' @examples +#' library(ggplot2) +#' library(dplyr) +#' library(gapminder) +#' +#' use_afcharts() +#' +#' grouped_bar_data <- +#' gapminder |> +#' filter(year %in% c(1967, 2007) & +#' country %in% c("United Kingdom", "Ireland", "France", "Belgium")) +#' +#' bar_chart <- ggplot(grouped_bar_data, +#' aes(x = country, y = lifeExp, fill = as.factor(year))) + +#' geom_bar(stat = "identity", position = "dodge") + +#' scale_y_continuous(expand = c(0, 0)) + +#' scale_fill_discrete_af() + +#' labs( +#' x = "Country", +#' y = NULL, +#' fill = NULL, +#' title = "Living longer", +#' subtitle = "Difference in life expectancy, 1967-2007", +#' caption = "Source: Gapminder" +#' ) +#' +#' file <- tempfile(fileext = ".svg") +#' save_govuk(file, bar_chart, device = "svg") +#' unlink(file) +#' +save_govuk <- function(filename, + plot = ggplot2::last_plot(), + device = c("svg", "png", "jpg"), + path = NULL, + ...) { + + device <- match.arg(device) + + width <- 960 + height <- 640 + units <- "px" + + ggplot2::ggsave( + filename = filename, + plot = plot, + device = device, + path = path, + width = width, + height = height, + units = units, + dpi = 72, + ... + ) + +} diff --git a/man/save_govuk.Rd b/man/save_govuk.Rd new file mode 100644 index 0000000..894247f --- /dev/null +++ b/man/save_govuk.Rd @@ -0,0 +1,61 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{save_govuk} +\alias{save_govuk} +\title{Save a plot at the correct dimensions for publishing on GOVUK} +\usage{ +save_govuk( + filename, + plot = ggplot2::last_plot(), + device = c("svg", "png", "jpg"), + path = NULL, + ... +) +} +\arguments{ +\item{filename}{File name} + +\item{plot}{The plot to save} + +\item{device}{File type to produce (svg, png or jpg). svg is preferred as +it scales well without pixellating} + +\item{path}{Directory to save the plot in} + +\item{...}{Other params passed to ggplot::ggsave} +} +\description{ +This is a wrapper around \code{\link[ggplot2:ggsave]{ggplot2::ggsave()}} with plot dimensions set for +publishing on GOVUK. +} +\examples{ +library(ggplot2) +library(dplyr) +library(gapminder) + +use_afcharts() + +grouped_bar_data <- + gapminder |> + filter(year \%in\% c(1967, 2007) & + country \%in\% c("United Kingdom", "Ireland", "France", "Belgium")) + +bar_chart <- ggplot(grouped_bar_data, + aes(x = country, y = lifeExp, fill = as.factor(year))) + + geom_bar(stat = "identity", position = "dodge") + + scale_y_continuous(expand = c(0, 0)) + + scale_fill_discrete_af() + + labs( + x = "Country", + y = NULL, + fill = NULL, + title = "Living longer", + subtitle = "Difference in life expectancy, 1967-2007", + caption = "Source: Gapminder" + ) + + file <- tempfile(fileext = ".svg") + save_govuk(file, bar_chart, device = "svg") + unlink(file) + +}