afcharts Demo

The afcharts package is maintained by the GSS Presentation Champions Data Visualisation tools subgroup.

afcharts is an R package for creating accessible charts 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.

1 Installation

1.1 Install from CRAN

Install the latest release version of afcharts directly from CRAN:

install.packages("afcharts")

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

Please get in contact with the group is neither of these options work for your department.

2 Getting Started

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

library(afcharts)
#> Warning: package 'afcharts' was built under R version 4.4.3

We’ll also use the following packages in this demo:

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

3 Analysis Function colour palettes

afcharts provides access to the Analysis function recommended colour palettes.

3.1 List of palettes

af_colour_palettes
#> $main
#> dark-blue turquoise dark-pink    orange 
#> "#12436D" "#28A197" "#801650" "#F46A25" 
#> 
#> $main2
#> dark-blue    orange 
#> "#12436D" "#F46A25" 
#> 
#> $main6
#>    dark-blue    turquoise    dark-pink       orange    dark-grey light-purple 
#>    "#12436D"    "#28A197"    "#801650"    "#F46A25"    "#3D3D3D"    "#A285D1" 
#> 
#> $sequential
#>  dark-blue   mid-blue light-blue 
#>  "#12436D"  "#2073BC"  "#6BACE6" 
#> 
#> $focus
#> dark-blue      grey 
#> "#12436D" "#BFBFBF"

3.2 Character vector of colour values

af_colour_values
#>    dark-blue    turquoise    dark-pink       orange    dark-grey light-purple 
#>    "#12436D"    "#28A197"    "#801650"    "#F46A25"    "#3D3D3D"    "#A285D1" 
#>     mid-blue   light-blue         grey 
#>    "#2073BC"    "#6BACE6"    "#BFBFBF"

3.3 Use an AF colour in your chart

You can use the af colours in a ggplot2 chart.

ggplot(mpg, aes(x = class)) +
  geom_bar()

ggplot(mpg, aes(x = class)) +
  geom_bar(fill = af_colour_values["dark-blue"])

4 Apply analysis function theme

4.1 theme_af

To apply the analysis function theme, use the theme_af function.

ggplot(mpg, aes(x = class)) +
  geom_bar()

ggplot(mpg, aes(x = class)) +
  geom_bar() +
  theme_af()

4.2 Decide which grid lines, axes or ticks to show

theme_af can set where grid lines, axes or tick marks are displayed.

ggplot(mpg, aes(x = class)) +
  geom_bar() +
  theme_af(axis = "xy")

ggplot(mpg, aes(x = class)) +
  geom_bar() +
  theme_af(axis = "none")

4.3 Change legend position

The theme_af function allows you to set the legend position.

economics_long %>%
  filter(variable %in% c("psavert", "uempmed")) %>%
  ggplot(aes(x = date, y = value, colour = variable)) +
  geom_line(linewidth = 1) +
  theme_af()

economics_long %>%
  filter(variable %in% c("psavert", "uempmed")) %>%
  ggplot(aes(x = date, y = value, colour = variable)) +
  geom_line(linewidth = 1) +
  theme_af(legend = "bottom")

5 Apply analysis function colours to scales

5.1 Fill, discrete colour scale

d <- subset(mpg, manufacturer == "ford")

ggplot(d, aes(x = class, fill = class)) +
 geom_bar()


ggplot(d, aes(x = class, fill = class)) +
 geom_bar() +
 scale_fill_discrete_af()

See also scale_colour_discrete_af

5.2 Colour, continuous colour scale

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

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

See also scale_fill_continuous_af

5.3 Using your own colour palette

There may be instances where you’d like to use a colour palette that is not available in afcharts. If so, this should be carefully considered to ensure it meets accessibility requirements. The Government Analysis Function guidance outlines appropriate steps for choosing your own accessible colour palette and should be used.

The example below uses scale_colour_manual in order to colour bars using a custom colour palette. Note that we can still use theme_af to apply the analysis function theme.

my_palette <- c("#0F820D", "#000000")

gapminder |>
  filter(country %in% c("United Kingdom", "China")) |>
  ggplot(aes(x = year, y = lifeExp, colour = country)) +
  geom_line(linewidth = 1) +
  theme_af(legend = "bottom") +
  scale_colour_manual(values = my_palette) +
  scale_y_continuous(
    limits = c(0, 82),
    breaks = seq(0, 80, 20),
    expand = c(0, 0)
  ) +
  scale_x_continuous(breaks = seq(1952, 2007, 5)) +
  labs(
    x = "Year",
    y = NULL,
    title = "Living Longer",
    subtitle = "Life Expectancy in the United Kingdom and China 1952-2007",
    caption = "Source: Gapminder",
    colour = NULL
  )

6 Automatically apply analysis function theme and colour scales

The use_afcharts function allows you to use the analysis function theme and scale by default for all charts.

economics <- economics_long %>%
  filter(variable %in% c("psavert", "uempmed")) %>%
  ggplot(aes(x = date, y = value, colour = variable)) +
  geom_line(linewidth = 1)

bar_chart <- ggplot(d, aes(x = class, fill = class)) +
 geom_bar()

economics

bar_chart

use_afcharts()
#> ℹ Default ggplot2 theme set to `theme_af`.
#> ℹ Default colours set.
#> ℹ Default geom aesthetics set.

economics
#> ℹ Using `main2` palette as only two colours are required.

bar_chart

Note that it not currently possible to turn use_af off

7 Save for GOVUK

Use the save_govuk function to save a chart at the correct dimensions for publishing on GOVUK.

SVG charts are preferred as they can be scaled to any size.

save_govuk("demo_chart.svg", plot = economics, device = "svg")
save_govuk("demo_chart.png", plot = economics, device = "png")

8 Future developments

  • Additional colour palettes?
  • Maps?
  • Interactive charts using plotly, highcharts or ggiraph?