Skip to content

Commit

Permalink
Add examples of how to use non-default colour palettes (closes #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-hannah committed Oct 20, 2023
1 parent 0b4ec4f commit 574af13
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
6 changes: 3 additions & 3 deletions vignettes/colours.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ Follow the [advice for using the focus palette](https://analysisfunction.civilse
The [Scottish Government focus palette](#focus-chart-palette) does not have this issue, so you may wish to use this instead.


## Using a different colour palette
## Using your own colour palette

There may be instances where you'd like to use a different colour palette.
If so, this should be carefully considered to ensure it meets accessibility requirements.
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.

An example of how to use an alternative colour palette is provided in the [cookbook](https://datasciencescotland.github.io/sgplot/articles/cookbook.html#using-a-different-colour-palette).
An example of how to use an alternative colour palette is provided in the [cookbook](https://datasciencescotland.github.io/sgplot/articles/cookbook.html#using-your-own-colour-palette).
However, 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).


Expand All @@ -191,4 +191,4 @@ sgplot::sg_colour_palettes$main
sgplot::af_colour_palettes
```

The reference files for `scale_` [colour functions](https://datasciencescotland.github.io/sgplot/reference/index.html#colours) provide examples of how to apply these palettes to ggplot2 charts.
Examples of how to apply these palettes to ggplot2 charts are available in both the [cookbook](https://datasciencescotland.github.io/sgplot/articles/cookbook.html) and the reference files for `scale_` [colour functions](https://datasciencescotland.github.io/sgplot/reference/index.html#colours).
72 changes: 68 additions & 4 deletions vignettes/cookbook/_colour-palettes.Rmd
Original file line number Diff line number Diff line change
@@ -1,15 +1,78 @@
## Using a different colour palette
## Using different colour palettes

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

The Analysis Function guidance also contains [suggested colour palettes](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-4).
These are also provided in sgplot, however the Scottish Government palettes are used by default.
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).
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.

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

There may be instances where you'd like to use a different colour palette.

### Using non-default sgplot colour palettes

There may be instances where you want to use an sgplot colour palette other than the default.
The full list of available palettes can be found by running either `sgplot::sg_colour_palettes` or `sgplot::af_colour_palettes`.

To use the Scottish Government `main-extended` palette:

```{r main-extended, fig.height = 5}
#| fig.alt = "A multiple line chart using sgplot theme and SG main-extended colour palette."
gapminder |>
filter(country %in% c("United Kingdom", "China", "India",
"Sweden", "Namibia", "Brazil")) |>
ggplot(aes(x = year, y = lifeExp, colour = country)) +
geom_line(linewidth = 1) +
theme_sg(legend = "bottom") +
scale_colour_discrete_sg("main-extended") +
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
)
```

Note: This chart is for demonstration purposes only. Accessibility guidance recommends using a maximum of four colours to avoid clutter.

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).
For example, to use the Analysis Function `main2` palette:

```{r af-palette, fig.height = 5}
#| fig.alt = "A multiple line chart using sgplot theme and AF main colour palette."
gapminder |>
filter(country %in% c("United Kingdom", "China")) |>
ggplot(aes(x = year, y = lifeExp, colour = country)) +
geom_line(linewidth = 1) +
theme_sg(legend = "bottom") +
scale_colour_discrete_sg("main2", palette_type = "af") +
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
)
```


### Using your own colour palette

There may be instances where you'd like to use a colour palette that is not available in sgplot.
If so, this should be carefully considered to ensure it meets accessibility requirements.
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.

Expand Down Expand Up @@ -59,6 +122,7 @@ gapminder |>
)
```

### Adding a new colour palette to sgplot

#### Adding a new colour palette to sgplot

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 comments on commit 574af13

Please sign in to comment.