Skip to content

Commit

Permalink
Add save_govUK function
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivia-Box-Power committed Aug 30, 2024
1 parent a1cd5ef commit 39cc403
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 13 deletions.
28 changes: 15 additions & 13 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
72 changes: 72 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
...
)

}
61 changes: 61 additions & 0 deletions man/save_govuk.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 39cc403

Please sign in to comment.