Title: | Produce Charts Following UK Government Analysis Function Guidance |
---|---|
Description: | Colour palettes and a 'ggplot2' theme to follow the UK Government Analysis Function best practice guidance for producing data visualisations, available at <https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/>. Includes continuous and discrete colour and fill scales, as well as a 'ggplot2' theme. |
Authors: | Crown Copyright [cph], Government Analysis Function [fnd], Alice Hannah [aut], Olivia Box Power [cre, ctb] |
Maintainer: | Olivia Box Power <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4.0 |
Built: | 2024-11-07 13:38:56 UTC |
Source: | CRAN |
A list grouping colours into palettes.
af_colour_palettes
af_colour_palettes
A character list
Government Analysis Function Colours Guidance
A vector containing colour names and their corresponding hex code.
af_colour_values
af_colour_values
A character vector
Government Analysis Function Colours Guidance
Convert millimetres to inches
mm_to_inch(x)
mm_to_inch(x)
x |
Numeric value in millimetres |
A numerical value in inches
mm_to_inch(100)
mm_to_inch(100)
This is a wrapper around ggplot2::ggsave()
with plot dimensions set for
publishing on GOVUK.
save_govuk( filename, plot = ggplot2::last_plot(), device = c("svg", "png", "jpg"), path = NULL, ... )
save_govuk( filename, plot = ggplot2::last_plot(), device = c("svg", "png", "jpg"), path = NULL, ... )
filename |
File name |
plot |
The plot to save |
device |
File type to produce (svg, png or jpg). svg is preferred as it scales well without pixelating |
path |
Directory to save the plot in |
... |
Other params passed to ggplot::ggsave |
Character vector giving path to saved file
library(ggplot2) library(dplyr) 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) 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)
library(ggplot2) library(dplyr) 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) 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)
Continuous colour scales for Analysis Function plots
scale_colour_continuous_af( palette = "sequential", palette_type = c("af"), reverse = FALSE, na_colour = "grey50", guide = "colourbar", ... )
scale_colour_continuous_af( palette = "sequential", palette_type = c("af"), reverse = FALSE, na_colour = "grey50", guide = "colourbar", ... )
palette |
Name of palette to use; e.g. "main", "sequential", "focus". Default value is "sequential". |
palette_type |
Currently only the Analysis Function palettes are supported. Defaults to "af". |
reverse |
Boolean value to indicate whether the palette should be reversed. |
na_colour |
Colour to set for missing values. |
guide |
A name or function used to create guide. Default is "colourbar". |
... |
Additional arguments passed to scale type. |
ggplot2 continuous colour scale
library(ggplot2) ggplot(mtcars, aes(x = mpg, y = wt, colour = cyl)) + geom_point() + scale_colour_continuous_af()
library(ggplot2) ggplot(mtcars, aes(x = mpg, y = wt, colour = cyl)) + geom_point() + scale_colour_continuous_af()
Discrete colour scales for Analysis Function plots
scale_colour_discrete_af( palette = "main", palette_type = c("af"), reverse = FALSE, ... )
scale_colour_discrete_af( palette = "main", palette_type = c("af"), reverse = FALSE, ... )
palette |
Name of palette to use; e.g. "main", "sequential", "focus." Default value is "main". |
palette_type |
Currently only the Analysis Function palettes are supported. Defaults to "af". |
reverse |
Boolean value to indicate whether the palette should be reversed. |
... |
Additional arguments passed to scale type. |
ggplot2 discrete colour scale
library(ggplot2) library(dplyr) economics_long %>% filter(variable %in% c("psavert", "uempmed")) %>% ggplot(aes(x = date, y = value, colour = variable)) + geom_line(linewidth = 1) + scale_colour_discrete_af()
library(ggplot2) library(dplyr) economics_long %>% filter(variable %in% c("psavert", "uempmed")) %>% ggplot(aes(x = date, y = value, colour = variable)) + geom_line(linewidth = 1) + scale_colour_discrete_af()
Continuous colour fill scales for Analysis Function plots
scale_fill_continuous_af( palette = "sequential", palette_type = c("af"), reverse = FALSE, na_colour = "grey50", guide = "colourbar", ... )
scale_fill_continuous_af( palette = "sequential", palette_type = c("af"), reverse = FALSE, na_colour = "grey50", guide = "colourbar", ... )
palette |
Name of palette to use; e.g. "main", "sequential", "focus." Default value is "sequential". |
palette_type |
Currently only the Analysis Function palettes are supported. Defaults to "af". |
reverse |
Boolean value to indicate whether the palette should be reversed. |
na_colour |
Colour to set for missing values. |
guide |
A name or function used to create guide. Default is "colourbar". |
... |
Additional arguments passed to scale type. |
ggplot2 continuous fill scale
library(ggplot2) ggplot(faithfuld, aes(x = waiting, y = eruptions, fill = density)) + geom_raster() + scale_fill_continuous_af()
library(ggplot2) ggplot(faithfuld, aes(x = waiting, y = eruptions, fill = density)) + geom_raster() + scale_fill_continuous_af()
Discrete colour fill scales for Analysis Function plots
scale_fill_discrete_af( palette = "main", palette_type = c("af"), reverse = FALSE, ... )
scale_fill_discrete_af( palette = "main", palette_type = c("af"), reverse = FALSE, ... )
palette |
Name of palette to use; e.g. "main", "sequential", "focus." Default value is "main." |
palette_type |
Currently only the Analysis Function palettes are supported. Defaults to "af". |
reverse |
Boolean value to indicate whether the palette should be reversed. |
... |
Additional arguments passed to scale type. |
ggplot2 discrete fill scale
library(ggplot2) d <- subset(mpg, manufacturer == "ford") ggplot(d, aes(x = class, fill = class)) + geom_bar() + scale_fill_discrete_af()
library(ggplot2) d <- subset(mpg, manufacturer == "ford") ggplot(d, aes(x = class, fill = class)) + geom_bar() + scale_fill_discrete_af()
ggplot2 theme for Analysis Function plots.
theme_af( base_size = 14, base_line_size = base_size/24, base_rect_size = base_size/24, grid = c("y", "x", "xy", "none"), axis = c("x", "y", "xy", "none"), ticks = c("xy", "x", "y", "none"), legend = c("right", "left", "top", "bottom", "none") )
theme_af( base_size = 14, base_line_size = base_size/24, base_rect_size = base_size/24, grid = c("y", "x", "xy", "none"), axis = c("x", "y", "xy", "none"), ticks = c("xy", "x", "y", "none"), legend = c("right", "left", "top", "bottom", "none") )
base_size |
base font size, given in pts. |
base_line_size |
base size for line elements. |
base_rect_size |
base size for rect elements. |
grid , axis , ticks
|
'x', 'y', 'xy' or 'none' to determine for which axes the attribute should be drawn. Grid defaults to 'y', axis to 'x', and ticks to 'xy'. |
legend |
'right', 'left', 'top', 'bottom', or 'none' to determine the position of the legend. Defaults to 'right'. |
ggplot2 plot theme
library(ggplot2) p <- ggplot(mpg, aes(x = class)) + geom_bar() p p + theme_af()
library(ggplot2) p <- ggplot(mpg, aes(x = class)) + geom_bar() p p + theme_af()
Set afcharts theme, colour palette and geom aesthetic defaults for ggplot2 charts.
use_afcharts(default_colour = af_colour_values["dark-blue"], ...)
use_afcharts(default_colour = af_colour_values["dark-blue"], ...)
default_colour |
Default colour/fill for geoms. Default value is 'blue'
from |
... |
Arguments passed to |
NULL. Function is used for side effects of setting ggplot2 plot theme, colour palette and geom aesthetic defaults.
library(ggplot2) d <- subset(mpg, manufacturer == "ford") ggplot(d, aes(x = model)) + geom_bar() ggplot(d, aes(x = model, fill = class)) + geom_bar() use_afcharts() ggplot(d, aes(x = model)) + geom_bar() ggplot(d, aes(x = model, fill = class, colour = class)) + geom_bar()
library(ggplot2) d <- subset(mpg, manufacturer == "ford") ggplot(d, aes(x = model)) + geom_bar() ggplot(d, aes(x = model, fill = class)) + geom_bar() use_afcharts() ggplot(d, aes(x = model)) + geom_bar() ggplot(d, aes(x = model, fill = class, colour = class)) + geom_bar()