|
1 |
| -## Using a different colour palette |
| 1 | +## Using different colour palettes |
2 | 2 |
|
3 | 3 | sgplot provides colour palettes as set out by the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes).
|
4 | 4 | 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).
|
5 | 5 |
|
6 | 6 | The Analysis Function guidance also contains [suggested colour palettes](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-4).
|
7 | 7 | These are also provided in sgplot, however the Scottish Government palettes are used by default.
|
8 |
| -To use an Analysis Function palette, set `palette_type = "af"` when using any of the `scale_` [colour functions](https://datasciencescotland.github.io/sgplot/reference/index.html#colours). |
| 8 | +The [main palette](https://datasciencescotland.github.io/sgplot/articles/colours.html#main-palette) is the default for discrete colour/fill functions, and the [sequential palette](https://datasciencescotland.github.io/sgplot/articles/colours.html#sequential-palette) for continuous colour/fill functions. |
9 | 9 |
|
10 | 10 | More information on the colours used in sgplot can be found at `vignette("colours")`.
|
11 | 11 |
|
12 |
| -There may be instances where you'd like to use a different colour palette. |
| 12 | + |
| 13 | +### Using non-default sgplot colour palettes |
| 14 | + |
| 15 | +There may be instances where you want to use an sgplot colour palette other than the default. |
| 16 | +The full list of available palettes can be found by running either `sgplot::sg_colour_palettes` or `sgplot::af_colour_palettes`. |
| 17 | + |
| 18 | +To use the Scottish Government `main-extended` palette: |
| 19 | + |
| 20 | +```{r main-extended, fig.height = 5} |
| 21 | +#| fig.alt = "A multiple line chart using sgplot theme and SG main-extended colour palette." |
| 22 | +
|
| 23 | +gapminder |> |
| 24 | + filter(country %in% c("United Kingdom", "China", "India", |
| 25 | + "Sweden", "Namibia", "Brazil")) |> |
| 26 | + ggplot(aes(x = year, y = lifeExp, colour = country)) + |
| 27 | + geom_line(linewidth = 1) + |
| 28 | + theme_sg(legend = "bottom") + |
| 29 | + scale_colour_discrete_sg("main-extended") + |
| 30 | + scale_y_continuous(limits = c(0, 82), |
| 31 | + breaks = seq(0, 80, 20), |
| 32 | + expand = c(0, 0)) + |
| 33 | + scale_x_continuous(breaks = seq(1952, 2007, 5)) + |
| 34 | + labs( |
| 35 | + x = "Year", |
| 36 | + y = NULL, |
| 37 | + title = "Living Longer", |
| 38 | + subtitle = "Life Expectancy in the United Kingdom and China 1952-2007", |
| 39 | + caption = "Source: Gapminder", |
| 40 | + colour = NULL |
| 41 | + ) |
| 42 | +``` |
| 43 | + |
| 44 | +Note: This chart is for demonstration purposes only. Accessibility guidance recommends using a maximum of four colours to avoid clutter. |
| 45 | + |
| 46 | +To use an Analysis Function palette, set `palette_type = "af"` when using any of the `scale_` [colour functions](https://datasciencescotland.github.io/sgplot/reference/index.html#colours). |
| 47 | +For example, to use the Analysis Function `main2` palette: |
| 48 | + |
| 49 | +```{r af-palette, fig.height = 5} |
| 50 | +#| fig.alt = "A multiple line chart using sgplot theme and AF main colour palette." |
| 51 | +
|
| 52 | +gapminder |> |
| 53 | + filter(country %in% c("United Kingdom", "China")) |> |
| 54 | + ggplot(aes(x = year, y = lifeExp, colour = country)) + |
| 55 | + geom_line(linewidth = 1) + |
| 56 | + theme_sg(legend = "bottom") + |
| 57 | + scale_colour_discrete_sg("main2", palette_type = "af") + |
| 58 | + scale_y_continuous(limits = c(0, 82), |
| 59 | + breaks = seq(0, 80, 20), |
| 60 | + expand = c(0, 0)) + |
| 61 | + scale_x_continuous(breaks = seq(1952, 2007, 5)) + |
| 62 | + labs( |
| 63 | + x = "Year", |
| 64 | + y = NULL, |
| 65 | + title = "Living Longer", |
| 66 | + subtitle = "Life Expectancy in the United Kingdom and China 1952-2007", |
| 67 | + caption = "Source: Gapminder", |
| 68 | + colour = NULL |
| 69 | + ) |
| 70 | +``` |
| 71 | + |
| 72 | + |
| 73 | +### Using your own colour palette |
| 74 | + |
| 75 | +There may be instances where you'd like to use a colour palette that is not available in sgplot. |
13 | 76 | If so, this should be carefully considered to ensure it meets accessibility requirements.
|
14 | 77 | The Analysis Function guidance outlines [appropriate steps for choosing your own accessibile colour palette](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-9) and should be used.
|
15 | 78 |
|
@@ -59,6 +122,7 @@ gapminder |>
|
59 | 122 | )
|
60 | 123 | ```
|
61 | 124 |
|
62 |
| -### Adding a new colour palette to sgplot |
| 125 | + |
| 126 | +#### Adding a new colour palette to sgplot |
63 | 127 |
|
64 | 128 | If you use a different palette regularly and feel it would be useful for this to be added to sgplot, please make a suggestion as per the [contributing guidance](https://datasciencescotland.github.io/sgplot/CONTRIBTUING.html).
|
0 commit comments