Title: Produce Charts Following UK Government Analysis Function Guidance
Version: 0.5.1
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.
License: MIT + file LICENSE
URL: https://github.com/best-practice-and-impact/afcharts, https://best-practice-and-impact.github.io/afcharts/
BugReports: https://github.com/best-practice-and-impact/afcharts/issues
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.3
Depends: R (≥ 4.1.0)
Imports: ggplot2, scales, cli, rlang, dplyr, purrr, stats
Suggests: ggtext, knitr, rmarkdown, tibble, tidyr, glue, stringr, testthat (≥ 2.1.0), plotly, gt, svglite (≥ 2.1.2), ragg (≥ 1.2.6), gapminder, diffviewer, vdiffr, withr
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2026-02-26 11:06:09 UTC; OBoxPower
Author: Crown Copyright [cph], Government Analysis Function [fnd], Alice Hannah [aut], Olivia Box Power [cre, ctb]
Maintainer: Olivia Box Power <Olivia.BoxPower@dhsc.gov.uk>
Repository: CRAN
Date/Publication: 2026-02-26 11:20:03 UTC

afcharts: 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.

Author(s)

Maintainer: Olivia Box Power Olivia.BoxPower@dhsc.gov.uk [contributor]

Authors:

Other contributors:

See Also

Useful links:


Analysis Function colour palettes

Description

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.

Usage

af_colour_palettes

Format

A character list

Source

Government Analysis Function Colours Guidance


Analysis Function colour names and hex codes

Description

A vector containing colour names and their corresponding hex code.

Usage

af_colour_values

Format

A character vector

Source

Government Analysis Function Colours Guidance


Individual Analysis Function colours

Description

Quick access to individual colours from the Analysis Function colour palettes.

Usage

af_dark_blue

af_orange

af_grey

af_pale_grey

Format

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.

Source

Government Analysis Function Colours Guidance

Examples


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

Description

Convert millimetres to inches

Usage

mm_to_inch(x)

Arguments

x

Numeric value in millimetres

Value

A numerical value in inches

Examples

mm_to_inch(100)


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.

Usage

save_govuk(
  filename,
  plot = ggplot2::last_plot(),
  device = c("svg", "png", "jpg"),
  path = NULL,
  ...
)

Arguments

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

Value

Character vector giving path to saved file

Examples

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

Description

Continuous colour scales for Analysis Function plots

Usage

scale_colour_continuous_af(
  palette = "sequential",
  palette_type = c("af"),
  reverse = FALSE,
  na.value = afcharts::af_pale_grey,
  guide = "colourbar",
  ...
)

Arguments

palette

Name of palette to use from af_colour_palettes; e.g. "categorical", "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.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.

Value

ggplot2 continuous colour scale

Examples

library(ggplot2)

ggplot(mtcars, aes(x = mpg, y = wt, colour = cyl)) +
  geom_point() +
  scale_colour_continuous_af()


Discrete colour scales for Analysis Function plots

Description

Discrete colour scales for Analysis Function plots

Usage

scale_colour_discrete_af(
  palette = "categorical",
  palette_type = c("af"),
  reverse = FALSE,
  na.value = afcharts::af_pale_grey,
  ...
)

Arguments

palette

Name of palette to use from af_colour_palettes; e.g. "categorical", "sequential", "focus". Default value is "categorical".

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.

Details

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.

Value

ggplot2 discrete colour scale

Examples

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

Description

Continuous colour fill scales for Analysis Function plots

Usage

scale_fill_continuous_af(
  palette = "sequential",
  palette_type = c("af"),
  reverse = FALSE,
  na.value = afcharts::af_pale_grey,
  guide = "colourbar",
  ...
)

Arguments

palette

Name of palette to use from af_colour_palettes; e.g. "categorical", "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.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.

Value

ggplot2 continuous fill scale

Examples

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

Description

Discrete colour fill scales for Analysis Function plots

Usage

scale_fill_discrete_af(
  palette = "categorical",
  palette_type = c("af"),
  reverse = FALSE,
  na.value = afcharts::af_pale_grey,
  ...
)

Arguments

palette

Name of palette to use from af_colour_palettes; e.g. "categorical", "sequential", "focus". Default value is "categorical".

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.

Details

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.

Value

ggplot2 discrete fill scale

Examples

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")

Analysis Function theme for ggplot2 charts.

Description

ggplot2 theme for Analysis Function plots.

Usage

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")
)

Arguments

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'.

Value

ggplot2 plot theme

Examples

library(ggplot2)

p <- ggplot(mpg, aes(x = class)) + geom_bar()

p
p + theme_af()


Use afcharts defaults.

Description

Set afcharts theme, colour palette and geom aesthetic defaults for ggplot2 charts.

Usage

use_afcharts(
  default_colour = afcharts::af_colour_values["dark-blue"],
  ...,
  reset = FALSE
)

Arguments

default_colour

Default colour/fill for geoms. Default value is 'blue' from af_colour_values.

...

Arguments passed to theme_af().

reset

Logical. Turn off use_afcharts. This aims to reset the default chart setting to their status when use_afcharts was first called.

Value

NULL. Function is used for side effects of setting ggplot2 plot theme, colour palette and geom aesthetic defaults.

Examples

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()