Learn R Programming

afcharts

afcharts is an R package for creating accessible plots by the Government Analysis Function. Currently, functions are available for styling ggplot2 plots.

The package has been developed using the Government Analysis Function Data Visualisation guidance. afcharts should be used in conjunction with these guidance documents.

More information about the package and its functions can be found on the afcharts website. In particular, the cookbook contains lots of examples.

Installation

Install from CRAN

Install the latest release version of afcharts directly from CRAN:

install.packages("afcharts")

Install from GitHub

afcharts can be installed directly from GitHub.

remotes::install_github(
  "best-practice-and-impact/afcharts",
  upgrade = "never",
  build_vignettes = TRUE,
  dependencies = TRUE
)

Getting Started

Once installed, afcharts can be loaded using the library() function:

library(afcharts)

Help files for each function in the package can be found on the References page of the package website. Alternatively, type ?function_name into the RStudio console. For example:

?theme_af()

Use afcharts as default

The easiest way to use afcharts is by adding use_afcharts() to the beginning of your R script, Rmarkdown document or Shiny app code. This function will set a number of defaults to ggplot2 geoms, use afcharts colour palettes and use theme_af().

Example 1: Bar chart with one colour using ggplot2 defaults

library(ggplot2)
library(dplyr)
library(gapminder)
library(afcharts)

gapminder |> 
  filter(year == 2007 & continent == "Americas") |>
  slice_max(order_by = pop, n = 5) |>
  ggplot() +
  geom_col(aes(x = reorder(country, -pop), y = pop)) +
  scale_y_continuous(
    labels = scales::label_number(scale = 1E-6),
    limits = c(0, 350E6),
    expand = expansion(mult = c(0, 0.1))
  ) +
  scale_fill_discrete_af("focus", reverse = TRUE) +
  labs(
    x = NULL,
    y = NULL,
    caption = "Source: Gapminder"
    )

The U.S.A. is the most populous country in the Americas Population of countries in the Americas (millions), 2007

The bar chart above has grey background, white grid lines and dark grey bars.

Example 2: Bar chart of one colour using afcharts defaults

afcharts::use_afcharts()

gapminder |> 
  filter(year == 2007 & continent == "Americas") |>
  slice_max(order_by = pop, n = 5) |>
  ggplot(aes(x = reorder(country, -pop), y = pop)) +
  geom_col(fill = af_colour_values["dark-blue"]) +
  scale_y_continuous(
    labels = scales::label_number(scale = 1E-6),
    limits = c(0, 350E6),
    expand = c(0, 0),expansion(mult = c(0, 0.1))
  ) +
  labs(
    x = NULL,
    y = NULL,
    caption = "Source: Gapminder"
  )

The U.S.A. is the most populous country in the Americas Population of countries in the Americas (millions), 2007

The bar chart above has a white background, light grey horizontal grid lines, and dark blue bars.

Example 3: Multiple colour line chart with afcharts formatting

afcharts::use_afcharts()

gapminder |>
  filter(country %in% c("United Kingdom", "China")) |>
  ggplot(
    aes(
      x = year, y = lifeExp,
      colour = factor(country, levels = c("United Kingdom", "China"))
    )
  ) +
  geom_line(linewidth = 1) +
  scale_y_continuous(
    breaks = seq(0, 80, 20),
    limits = c(0, 82),
    expand = expansion(mult = c(0, 0.1))
  ) +
  scale_x_continuous(breaks = seq(1952, 2007, 10)) +
  labs(
    x = "Year",
    y = NULL,
    caption = "Source: Gapminder",
    colour = NULL
  )

Living Longer Life Expectancy in the United Kingdom and China 1952 to 2007

The line chart above has a white background, light grey horizontal grid lines, a dark blue line representing the UK, and an orange line representing China.

Acknowledgments

The afcharts package is based on the sgplot package, written by Alice Hannah.

Licence

Unless stated otherwise, the codebase is released under the MIT License. This covers both the codebase and any sample code in the documentation.

The documentation is © Crown copyright and available under the terms of the Open Government 3.0 licence.

Copy Link

Version

Install

install.packages('afcharts')

Monthly Downloads

452

Version

0.5.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Olivia Box Power

Last Published

February 26th, 2026

Functions in afcharts (0.5.1)

mm_to_inch

Convert millimetres to inches
scale_fill_continuous_af

Continuous colour fill scales for Analysis Function plots
scale_colour_continuous_af

Continuous colour scales for Analysis Function plots
scale_fill_discrete_af

Discrete colour fill scales for Analysis Function plots
scale_colour_discrete_af

Discrete colour scales for Analysis Function plots
af_colour_palettes

Analysis Function colour palettes
af_colour_values

Analysis Function colour names and hex codes
afcharts-package

afcharts: Produce Charts Following UK Government Analysis Function Guidance
save_govuk

Save a plot at the correct dimensions for publishing on GOVUK
use_afcharts

Use afcharts defaults.
theme_af

Analysis Function theme for ggplot2 charts.
af_colours

Individual Analysis Function colours