| 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.5.1 |
| Built: | 2026-06-04 06:49:51 UTC |
| Source: | https://github.com/best-practice-and-impact/afcharts |
A list grouping colours into palettes. Note that the use of the
main, main2 and main6 colour palettes is deprecated. Please use
categorical and categorical2 instead, which give access to the same
colours.
af_colour_palettesaf_colour_palettes
A character list
Government Analysis Function Colours Guidance
A vector containing colour names and their corresponding hex code.
af_colour_valuesaf_colour_values
A character vector
Government Analysis Function Colours Guidance
Quick access to individual colours from the Analysis Function colour palettes.
af_dark_blue af_orange af_grey af_pale_greyaf_dark_blue af_orange af_grey af_pale_grey
An object of class character of length 1.
An object of class character of length 1.
An object of class character of length 1.
An object of class character of length 1.
Government Analysis Function Colours Guidance
library(dplyr) library(ggplot2) # Example of using af_dark_blue to colour bars iris %>% group_by(Species) %>% summarise(Petal.Width = mean(Petal.Width)) %>% ggplot() + geom_col( aes(Species, Petal.Width), fill = af_dark_blue ) + scale_y_continuous( expand = expansion(c(0, 0.05)) ) + theme_af()library(dplyr) library(ggplot2) # Example of using af_dark_blue to colour bars iris %>% group_by(Species) %>% summarise(Petal.Width = mean(Petal.Width)) %>% ggplot() + geom_col( aes(Species, Petal.Width), fill = af_dark_blue ) + scale_y_continuous( expand = expansion(c(0, 0.05)) ) + theme_af()
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.value = afcharts::af_pale_grey, guide = "colourbar", ... )scale_colour_continuous_af( palette = "sequential", palette_type = c("af"), reverse = FALSE, na.value = afcharts::af_pale_grey, guide = "colourbar", ... )
palette |
Name of palette to use from |
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.value |
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 = "categorical", palette_type = c("af"), reverse = FALSE, na.value = afcharts::af_pale_grey, ... )scale_colour_discrete_af( palette = "categorical", palette_type = c("af"), reverse = FALSE, na.value = afcharts::af_pale_grey, ... )
palette |
Name of palette to use from |
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.value |
Colour to set for missing values. |
... |
Additional arguments passed to scale type. |
If the palette is set to "categorical" or "sequential" and fewer than the maximum number of colours are required then the colours will be used in the correct order following the analysis function guidance.
E.g. If only two colours are required and the palette is set to
"categorical" then the "categorical2" palette will be used instead,
without warning.
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.value = afcharts::af_pale_grey, guide = "colourbar", ... )scale_fill_continuous_af( palette = "sequential", palette_type = c("af"), reverse = FALSE, na.value = afcharts::af_pale_grey, guide = "colourbar", ... )
palette |
Name of palette to use from |
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.value |
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 = "categorical", palette_type = c("af"), reverse = FALSE, na.value = afcharts::af_pale_grey, ... )scale_fill_discrete_af( palette = "categorical", palette_type = c("af"), reverse = FALSE, na.value = afcharts::af_pale_grey, ... )
palette |
Name of palette to use from |
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.value |
Colour to set for missing values. |
... |
Additional arguments passed to scale type. |
If the palette is set to "categorical" or "sequential" and fewer than the maximum number of colours are required then the colours will be used in the correct order following the analysis function guidance.
E.g. If only two colours are required and the palette is set to
"categorical" then the "categorical2" palette will be used instead,
without warning.
ggplot2 discrete fill scale
library(ggplot2) d <- subset(mpg, manufacturer == "ford") ggplot(d, aes(x = class, fill = class)) + geom_bar() + scale_fill_discrete_af() # The Analysis Function guidance recommends using a dark blue outline on # barcharts with a sequential colour palette d2 <- data.frame( age = c("<25", "25-44", "45-54", "55-64", "65 plus"), score = c(20, 34, 44, 88, 90) ) ggplot(d2, aes(x = age, y = score, fill = age)) + geom_col(colour = af_dark_blue) + scale_fill_discrete_af(palette = "sequential")library(ggplot2) d <- subset(mpg, manufacturer == "ford") ggplot(d, aes(x = class, fill = class)) + geom_bar() + scale_fill_discrete_af() # The Analysis Function guidance recommends using a dark blue outline on # barcharts with a sequential colour palette d2 <- data.frame( age = c("<25", "25-44", "45-54", "55-64", "65 plus"), score = c(20, 34, 44, 88, 90) ) ggplot(d2, aes(x = age, y = score, fill = age)) + geom_col(colour = af_dark_blue) + scale_fill_discrete_af(palette = "sequential")
ggplot2 theme for Analysis Function plots.
theme_af( base_size = getOption("afcharts.base_size", 14), base_line_size = getOption("afcharts.base_line_size", base_size/24), base_rect_size = getOption("afcharts.base_rect_size", base_size/24), grid = getOption("afcharts.grid", "y"), axis = getOption("afcharts.axis", "x"), ticks = getOption("afcharts.ticks", "xy"), legend = getOption("afcharts.legend", "right"), axis_text = getOption("afcharts.axis_text", "xy"), axis_title = getOption("afcharts.axis_title", "xy"), legend_title = getOption("afcharts.legend_title", "show") )theme_af( base_size = getOption("afcharts.base_size", 14), base_line_size = getOption("afcharts.base_line_size", base_size/24), base_rect_size = getOption("afcharts.base_rect_size", base_size/24), grid = getOption("afcharts.grid", "y"), axis = getOption("afcharts.axis", "x"), ticks = getOption("afcharts.ticks", "xy"), legend = getOption("afcharts.legend", "right"), axis_text = getOption("afcharts.axis_text", "xy"), axis_title = getOption("afcharts.axis_title", "xy"), legend_title = getOption("afcharts.legend_title", "show") )
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. This can also be followed by a justification along that side (top, bottom, left, right or centre) e.g. 'top-left', 'left-bottom', 'right-centre'. Defaults to 'right'. |
axis_text, axis_title
|
'x', 'y', 'xy' or 'none' to determine whether axis text and/or axis titles should be displayed. Text defaults to 'xy', as does title. Note that axis text refers to the 'labels' under the tick marks. |
legend_title |
Set to 'none' to suppress legend titles. Defaults to 'show'. |
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 = afcharts::af_colour_values["dark-blue"], ..., reset = FALSE )use_afcharts( default_colour = afcharts::af_colour_values["dark-blue"], ..., reset = FALSE )
default_colour |
Default colour/fill for geoms. Default value is 'blue'
from |
... |
Arguments passed to |
reset |
Logical. Turn off use_afcharts. This aims to reset the default
chart setting to their status when |
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()