This is a wrapper around ggplot2::ggsave()
with plot dimensions set for
publishing on GOVUK.
Examples
library(ggplot2)
library(dplyr)
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
library(gapminder)
# Images on GOVUK are shrunk. We therefore recommend using font size 20 pt
# when exporting charts for GOVUK, which will appear as approximately 12 pt on
# the website.
use_afcharts(base_size = 20)
#> ℹ Default ggplot2 theme set to `theme_af`.
#> ℹ Default colours set.
#> ℹ Default geom aesthetics set.
#> NULL
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")
#> Warning: ! Using `main2` as only two colours are required.
unlink(file)