Error(s) in re-building vignettes:
  ...
--- re-building ‘accessibility.Rmd’ using rmarkdown
--- finished re-building ‘accessibility.Rmd’

--- re-building ‘colours.Rmd’ using rmarkdown
--- finished re-building ‘colours.Rmd’

--- re-building ‘cookbook.Rmd’ using rmarkdown
## Line charts
### Line chart with one line

``` r
gapminder |>
  filter(country == "United Kingdom") |>
  ggplot(aes(x = year, y = lifeExp)) +
  geom_line(linewidth = 1, colour = af_colour_values["dark-blue"]) +
  theme_af() +
  scale_y_continuous(
    limits = c(0, 82),
    breaks = seq(0, 80, 20),
    expand = expansion(mult = c(0, 0.1))
  ) +
  scale_x_continuous(breaks = seq(1952, 2007, 5)) +
  labs(
    x = "Year",
    y = NULL
  )
```

<div class="chart-title">Living Longer</div>
<div class="chart-subtitle">Life Expectancy in the United Kingdom 1952 to 2007</div>
![](/home/hornik/tmp/R.check/r-devel-gcc/Work/PKGS/afcharts.Rcheck/vign_test/afcharts/vignettes/cookbook_files/figure-html/unnamed-chunk-4-1.png)<!-- -->

This line chart uses the afcharts theme. There are pale grey grid lines extending from the y axis, and there is a thicker dark blue line representing the data.

### Line chart with multiple lines


``` r
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) +
  theme_af(legend = "right") +
  scale_colour_discrete_af() +
  scale_y_continuous(
    limits = c(0, 82),
    breaks = seq(0, 80, 20),
    expand = expansion(mult = c(0, 0.1))
  ) +
  scale_x_continuous(breaks = seq(1952, 2007, 10)) +
  labs(
    x = "Year",
    y = NULL,
    colour = NULL
  )
```

<div class="chart-title">Living Longer</div>
<div class="chart-subtitle">Life Expectancy in the United Kingdom and China 1952 to 2007</div>
![](/home/hornik/tmp/R.check/r-devel-gcc/Work/PKGS/afcharts.Rcheck/vign_test/afcharts/vignettes/cookbook_files/figure-html/unnamed-chunk-5-1.png)<!-- -->

This line chart uses the afcharts theme and there are thin pale grey lines extending from the y-axis. There are two thicker lines showing the life expectancy in the UK and China over time. The line colours are from the Analysis Function categorical2 palette - dark blue for the UK and orange for China, denoted by a legend at the bottom of the chart.

Legends should be avoided unless absolutely necessary, as these usually rely on using colour to match labels to data. More information can be found in the Analysis Function [charts guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/#section-11). It is best practice to label lines directly, and an example of this can be found in the [annotations](#annotations) section.

## Bar charts


``` r
pop_bar_data <- gapminder |>
  filter(year == 2007 & continent == "Americas") |>
  slice_max(order_by = pop, n = 5)
```


``` r
ggplot(pop_bar_data, aes(x = reorder(country, -pop), y = pop)) +
  geom_col(fill = af_colour_values["dark-blue"]) +
  theme_af() +
  scale_y_continuous(
    limits = c(0, 350E6),
    labels = scales::label_number(scale = 1E-6),
    expand = expansion(mult = c(0, 0.1)),
  ) +
  labs(
    x = NULL,
    y = NULL,
  )
```

<div class="chart-title">The U.S.A. is the most populous country in the Americas</div>
<div class="chart-subtitle">Population of countries in the Americas (millions), 2007</div>
![](/home/hornik/tmp/R.check/r-devel-gcc/Work/PKGS/afcharts.Rcheck/vign_test/afcharts/vignettes/cookbook_files/figure-html/unnamed-chunk-6-1.png)<!-- -->

This bar chart uses the afcharts theme, and shows the populations of the five most populous countries in the Americas. Each bar is dark blue and labelled by country underneath. All text is black in a sans serif font. Pale grey grid lines extend out from the y axis.

A bar chart can sometimes be easier to interpret with horizontal bars. This can also be a good option if your bar labels are long and difficult to display on the x axis. To produce a horizontal bar chart, swap the variables defined for x and y in `aes()` and make a few tweaks to `theme_af()`; draw grid lines for the x axis only by setting the `grid` argument, and draw an axis line for the y axis only by setting the `axis` argument. We can also hide the x and y axis titles by setting the `axis_title` argument. This replaces the need to use the `labs` function, with x and y set to NULL.


``` r
ggplot(pop_bar_data, aes(x = pop, y = reorder(country, pop))) +
  geom_col(fill = af_colour_values["dark-blue"]) +
  theme_af(grid = "x", axis = "y", axis_title = "none") +
  scale_x_continuous(
    limits = c(0, 350E6),
    labels = scales::label_number(scale = 1E-6),
    expand = expansion(mult = c(0, 0.1))
  )
```

<div class="chart-title">The U.S.A. is the most populous country in the Americas</div>
<div class="chart-subtitle">Population of countries in the Americas (millions), 2007</div>

![](/home/hornik/tmp/R.check/r-devel-gcc/Work/PKGS/afcharts.Rcheck/vign_test/afcharts/vignettes/cookbook_files/figure-html/unnamed-chunk-7-1.png)<!-- -->

This bar chart uses the afcharts theme and displays the populations of the five most populous countries in the Americas. The country names are displayed on the y axis, with the bars extending from left to right. Each bar is dark blue, and pale grey grid lines extend up from the x axis.

### Grouped bar chart

To create a grouped bar chart, set `stat = "identity"` and `position = "dodge"` in the call to `geom_bar()`. Also assign a variable to `fill` within `aes()` to determine what variable is used to create bars within groups. The `legend` argument in `theme_af()` can be used to set the position of the legend.


``` r
grouped_bar_data <-
  gapminder |>
  filter(
    year %in% c(1967, 2007) &
      country %in% c("United Kingdom", "Ireland", "France", "Belgium")
  )

ggplot(
  grouped_bar_data,
  aes(x = country, y = lifeExp, fill = as.factor(year))
) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_y_continuous(
    limits = c(0, 100),
    breaks = c(seq(0, 100, 20)),
    labels = c(seq(0, 100, 20)),
    expand = expansion(mult = c(0, 0.1))
  ) +
  theme_af(legend = "bottom") +
  scale_fill_discrete_af() +
  labs(
    x = "Country",
    y = NULL,
    fill = NULL
  )
```

<div class="chart-title">Living longer</div>
<div class="chart-subtitle">Difference in life expectancy, 1967 to 2007</div>

![](/home/hornik/tmp/R.check/r-devel-gcc/Work/PKGS/afcharts.Rcheck/vign_test/afcharts/vignettes/cookbook_files/figure-html/unnamed-chunk-8-1.png)<!-- -->

This grouped bar chart uses the afcharts theme. It shows the life expectancy in 1967 and 2007 for four countries, which are displayed on the x axis. For each country there are two bars. The bar colours are from the Analysis Function categorical2 palette - dark blue for 1967 and orange for 2007, denoted by a legend at the bottom of the chart.

### Stacked bar chart

To create a stacked bar chart, set `stat = "identity` and `position = "fill"` in the call to `geom_bar()` and assign a variable to `fill` as before. This will plot your data as part-to-whole. To plot counts, set `position = "identity"`.

Caution should be taken when producing stacked bar charts. They can quickly become difficult to interpret if plotting non part-to-whole data, and/or if plotting more than two categories per stack. First and last categories in the stack will always be easier to compare across bars than those in the middle. Think carefully about the story you are trying to tell with your chart.


``` r
stacked_bar_data <-
  gapminder |>
  filter(year == 2007) |>
  mutate(
    lifeExpGrouped = cut(
      lifeExp,
      breaks = c(0, 75, Inf),
      labels = c("Under 75", "75+")
    )
  ) |>
  group_by(continent, lifeExpGrouped) |>
  summarise(n_countries = n(), .groups = "drop")

ggplot(
  stacked_bar_data,
  aes(x = continent, y = n_countries, fill = lifeExpGrouped)
) +
  geom_bar(stat = "identity", position = "fill") +
  theme_af(legend = "right") +
  scale_y_continuous(
    labels = scales::percent,
    expand = expansion(mult = c(0, 0.1))
  ) +
  coord_cartesian(clip = "off") +
  scale_fill_discrete_af() +
  labs(
    x = NULL,
    y = NULL,
    fill = "Life Expectancy",
  )
```

<div class="chart-title">How life expectancy varies across continents</div>
<div class="chart-subtitle">Percentage of countries by life expectancy band, 2007</div>
![](/home/hornik/tmp/R.check/r-devel-gcc/Work/PKGS/afcharts.Rcheck/vign_test/afcharts/vignettes/cookbook_files/figure-html/unnamed-chunk-9-1.png)<!-- -->

This stacked bar chart uses the afcharts theme and shows the proportions of countries with a life expectancy over and under 75 by continent. The continents are listed along the x axis, with the y axis labelled between 0% and 100% in breaks of 25%. The colours for the bar segments are from the Analysis Function categorical2 palette - dark blue for under 75 and orange for over 75, denoted by a legend at the bottom of the chart. There is whitespace between each bar.

## Histograms


``` r
gapminder |>
  filter(year == 2007) |>
  ggplot(aes(x = lifeExp)) +
  geom_histogram(
    binwidth = 5,
    colour = "white",
    fill = af_colour_values["dark-blue"]
  ) +
  theme_af() +
  scale_y_continuous(
    limits = c(0, 35),
    breaks = c(seq(0, 35, 5)),
    expand = expansion(mult = c(0, 0.1))
  ) +
  labs(
    x = NULL,
    y = "Number of \ncountries",
  )
```
<div class="chart-title">How life expectancy varies</div>
<div class="chart-subtitle">Distribution of life expectancy, 2007</div>
![](/home/hornik/tmp/R.check/r-devel-gcc/Work/PKGS/afcharts.Rcheck/vign_test/afcharts/vignettes/cookbook_files/figure-html/unnamed-chunk-10-1.png)<!-- -->

This histogram uses the afcharts theme, and shows the distribution of life expectancy by number of countries. There are pale grey grid lines extending out from the y axis. The bars are dark blue with white space between each.

## Scatterplots


``` r
gapminder |>
  filter(year == 2007) |>
  ggplot(aes(x = gdpPercap, y = lifeExp)) +
  geom_point(colour = af_colour_values["dark-blue"]) +
  theme_af(axis = "none", grid = "xy") +
  scale_x_continuous(labels = scales::label_comma()) +
  labs(
    x = "GDP (US$, inflation-adjusted)",
    y = "Life\nExpectancy\n(years)",
  )
```

<div class="chart-title">The relationship between GDP and Life Expectancy is complex</div>
<div class="chart-subtitle">GDP and Life Expectancy for all countries, 2007</div>

![](/home/hornik/tmp/R.check/r-devel-gcc/Work/PKGS/afcharts.Rcheck/vign_test/afcharts/vignettes/cookbook_files/figure-html/unnamed-chunk-11-1.png)<!-- -->

This scatterplot uses the afcharts theme, and shows life expectancy against GDP for all countries. Thin pale grey lines extend out from the x and y axis labels, forming a grid. The data points are plotted as dark blue circles. Both axes are labeled in black using a sans serif font. 

It is best practice to use commas to separate thousands. Use scales::label_comma() to add commas to axis labels.

## Small multiples


``` r
gapminder |>
  filter(continent != "Oceania") |>
  group_by(continent, year) |>
  summarise(pop = sum(as.numeric(pop)), .groups = "drop") |>
  ggplot(aes(x = year, y = pop, fill = continent)) +
  geom_area() +
  theme_af(axis = "none", ticks = "none", legend = "none") +
  scale_fill_discrete_af() +
  facet_wrap(~ continent, ncol = 2, scales = "free_x") +
  scale_x_continuous(
    breaks = c(1952, 2007),
    labels = c("1952", "2007"),
    limits = c(1950, 2010)
  ) +
  scale_y_continuous(
    breaks = c(0, 2e9, 4e9),
    labels = c(0, "2bn", "4bn")
  ) +
  coord_cartesian(clip = "off") +
  labs(
    x = NULL,
    y = NULL
  )
```

<div class="chart-title">Asia's rapid growth</div>
<div class="chart-subtitle">Population growth in billions by continent, 1952 to 2007</div>

![](/home/hornik/tmp/R.check/r-devel-gcc/Work/PKGS/afcharts.Rcheck/vign_test/afcharts/vignettes/cookbook_files/figure-html/unnamed-chunk-12-1.png)<!-- -->

This chart uses the afcharts theme. It contains four subplots in a two by two grid showing how the populations of four continents have changed over time. Each subplot is labelled with the continent. The subplots have a common y axis, with no values on the x axis to facilitate for a simple comparison of the relative values. Each subplot is filled with a different colour from the Analysis Function categorical colour palette to be distinct from other subplots.

## Pie charts


``` r
stacked_bar_data |>
  filter(continent == "Europe") |>
  ggplot(aes(x = "", y = n_countries, fill = lifeExpGrouped)) +
  geom_col(colour = "white", position = "fill") +
  coord_polar(theta = "y") +
  theme_af(grid = "none", axis = "none", ticks = "none") +
  theme(axis.text = element_blank()) +
  scale_fill_discrete_af() +
  labs(
    x = NULL,
    y = NULL,
    fill = NULL,
  )
```

<div class="chart-title">How life expectancy varies in Europe</div>
<div class="chart-subtitle">Percentage of countries by life expectancy band, 2007</div>
![](/home/hornik/tmp/R.check/r-devel-gcc/Work/PKGS/afcharts.Rcheck/vign_test/afcharts/vignettes/cookbook_files/figure-html/unnamed-chunk-13-1.png)<!-- -->

This pie chart uses the afcharts theme, showing the proportions of European countries with a life expectancy under and over 75. The segment colours are from the Analysis Function categorical2 palette, with the smaller under 75 segment in dark blue, and the larger over 75 segment in orange. This is indicated by a legend to the right of the pie chart. There is whitespace separating the segments from each other.

## Focus charts


``` r
pop_bar_data |>
  ggplot(aes(x = reorder(country, -pop), y = pop, fill = country == "Brazil")) +
  geom_col() +
  theme_af(legend = "none") +
  scale_y_continuous(
    limits = c(0, 350E6),
    labels = scales::label_number(scale = 1E-6),
    expand = expansion(mult = c(0, 0.1))
  ) +
  scale_fill_discrete_af("focus", reverse = TRUE) +
  labs(
    x = NULL,
    y = NULL,
  )
```

<div class="chart-title">Brazil has the second highest population in the Americas</div>
<div class="chart-subtitle">Population of countries in the Americas (millions), 2007</div>

![](/home/hornik/tmp/R.check/r-devel-gcc/Work/PKGS/afcharts.Rcheck/vign_test/afcharts/vignettes/cookbook_files/figure-html/unnamed-chunk-14-1.png)<!-- -->

This bar chart uses the afcharts theme, and shows the populations of five countries of the Americas in descending order. The country names are given on the x axis, with all chart text in black in a sans serif font. Four of the bars on the chart are light grey, and the bar for Brazil is filled in dark blue to highlight it.

## Interactive charts

To make a `ggplot2` chart interactive, use `ggplotly()` from the `plotly` package. Note however that `ggplotly()` has a number of 'quirks', including the following:

* afcharts uses the 'sans' font family, however `plotly` does not recognise this font. To work around this you should add a further call to `theme` to set the font family for text to `""`. 

* Subtitles and captions are not supported in `ggplotly()`. As stated elsewhere in this guidance, titles and subtitles should ideally be included in the body of text surrounding a chart rather than embedded in the chart itself, and so this is hopefully not a big issue.

Please note, interactive charts may not meet all accessibility requirements and we advise you to test any interactive charts with users to ensure they work correctly.


``` r
p <-
  pop_bar_data |>
  # Format text for tooltips
  mutate(
    tooltip = paste0(
      "Country: ", country, "\n",
      "Population (millions): ", round(pop / 10 ^ 6, 1)
    )
  ) |>
  ggplot(aes(x = reorder(country, -pop), y = pop, text = tooltip)) +
  geom_col(fill = af_colour_values["dark-blue"]) +
  theme_af(ticks = "x") +
  theme(text = element_text(family = "")) +
  scale_y_continuous(
    limits = c(0, 350E6),
    labels = scales::label_number(scale = 1E-6),
    expand = expansion(mult = c(0, 0.1))
  ) +
  labs(
    x = NULL,
    y = NULL
  )

plotly::ggplotly(p, tooltip = "text") |>
  plotly::config(
    modeBarButtons = list(list("resetViews")),
    displaylogo = FALSE
  )
```
<div class="chart-title">The U.S.A. is the most populous country in the Americas</div>
<div class="chart-subtitle">Population of countries in the Americas (millions), 2007</div>




Quitting from ./cookbook/_chart-types.Rmd:397-399 [unnamed-chunk-15]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<error/rlang_error>
Error in `loadNamespace()`:
! there is no package called 'plotly'
---
Backtrace:
    ▆
 1. └─base::loadNamespace(x)
 2.   └─base::withRestarts(stop(cond), retry_loadNamespace = function() NULL)
 3.     └─base (local) withOneRestart(expr, restarts[[1L]])
 4.       └─base (local) doWithOneRestart(return(expr), restart)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Quitting from cookbook.Rmd:63-64 [chart-types]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<error/rlang_error>
Error in `loadNamespace()`:
! there is no package called 'plotly'
---
Backtrace:
    ▆
 1. └─base::loadNamespace(x)
 2.   └─base::withRestarts(stop(cond), retry_loadNamespace = function() NULL)
 3.     └─base (local) withOneRestart(expr, restarts[[1L]])
 4.       └─base (local) doWithOneRestart(return(expr), restart)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Error: processing vignette 'cookbook.Rmd' failed with diagnostics:
there is no package called 'plotly'
--- failed re-building ‘cookbook.Rmd’

--- re-building ‘saving.Rmd’ using rmarkdown
--- finished re-building ‘saving.Rmd’

SUMMARY: processing the following file failed:
  ‘cookbook.Rmd’

Error: Vignette re-building failed.
Execution halted