---
title: "Accessibility"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Accessibility}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE
)
```

Accessibility legislation means that all content published on public sector websites, including charts, must meet the level A and AA success criterion in the [Web Content Accessibility Guidelines 2.1](https://www.w3.org/TR/WCAG21/). 


## Resources

The [Government Analysis Function](https://analysisfunction.civilservice.gov.uk/) has published a variety of resources for producing accessible data visualisations:

* [Charts Checklist](https://analysisfunction.civilservice.gov.uk/policy-store/charts-a-checklist/)
* [Charts Guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/)
* [Colours Guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/)
* [Data Visualisation e-Learning](https://analysisfunction.civilservice.gov.uk/support/communicating-analysis/introduction-to-data-visualisation-e-learning/) (in particular the [Accessibility and Colour](https://analysisfunction.civilservice.gov.uk/support/communicating-analysis/introduction-to-data-visualisation-e-learning/data-visualisation-e-learning-module-3-accessibility-and-colour-palettes/) module)

sgplot has been developed following advice in the above guidance, however, using sgplot does not guarantee accessibility. sgplot should therefore be used in conjunction with this guidance.


## Accessibility of charts using sgplot

By applying `theme_sg` to ggplot2 plots, the following best practice is followed:

* Plot background is white.

* Axis lines and ticks are light grey (#595959). By default, only the x axis is shown but these can be controlled using the `axis` and `ticks` arguments.

* Grid lines are light grey (#595959) and kept to a minimum; by default only major y axis lines are shown, but this can be controlled using the `grid` argument.

* Text is black, uses a Sans Serif font and is size 12 by default. Text size can be controlled using the `base_size` argument.

* All text is horizontal.

* Line thickness is relative to font size.

sgplot provides colour palettes as set out by the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes). 
These palettes have been developed to meet the [Web Content Accessibility Guidelines 2.1 for graphical objects](https://www.w3.org/WAI/WCAG21/Understanding/non-text-contrast.html) and can be applied to charts by using one of the four colour/fill functions:

* `scale_colour_discrete_sg()`
* `scale_fill_discrete_sg()`
* `scale_colour_continuous_sg()`
* `scale_fill_continuous_sg()`

More information on sgplot colours can be found at `vignette("colours")`.


## Other accessibility considerations

Using sgplot does not guarantee accessibility. The Analysis Function [chart guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/) contains detailed information on other considerations that should be made. 
Not all of these are repeated here, but we think the following are a good place to start:

* Consider what type of chart is best to convey your message. Keep it as simple as possible.

* Plot titles should be provided in the body of the page, not embedded within the image of the plot. This does pose the risk of the plot image being re-shared without the title and losing context, however it is usually preferable to [prioritise accessibility](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/#section-6).

* All plots must have alt text (this may be in the code behind an image of the plot, but could also be a table of data or a text description of the message the chart is presenting).

* Take great care if using a [stacked bar chart](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/#section-10). Keep the number of categories to a minimum, and carefully consider the order of categories and bars.

* If horizontal text is difficult to read (for example, if axis labels are overlapping), consider a different chart orientation or type.

* If using a bar chart, the gap between bars should be narrower than the width of a single bar. The [R Graphics cookbook](https://r-graphics.org/RECIPE-BAR-GRAPH-ADJUST-WIDTH.html) describes how to do this.

* Use commas as thousands separators in axis labels or text annotations.

* [Ask for feedback](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/#section-15) from colleagues and users.
