Skip to content

Commit a1b6e67

Browse files
authored
Merge pull request #27 from DataScienceScotland/bug-fix
Bug fix and guidance edits
2 parents 9799517 + 21cedcc commit a1b6e67

10 files changed

+104
-15
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: sgplot
22
Title: Graphic Styles and Colours for Scottish Government Plots
3-
Version: 0.2.0
3+
Version: 0.2.1
44
Authors@R: c(
55
person("Scottish Government", , , "statistics.enquiries@gov.scot", role = c("cph", "fnd")),
66
person("Alice", "Byers", , "alice.byers@gov.scot", c("aut", "cre"))

NEWS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
# sgplot 0.2.1
2+
3+
* Fix issue where `scale_` functions didn't work without the package being loaded (#26)
4+
5+
* Add [examples of using non-default colour palettes](https://datasciencescotland.github.io/sgplot/articles/cookbook.html#using-different-colour-palettes) (#24)
6+
7+
* Add link to `theme_sg()` in `use_sgplot()` help file
8+
19
# sgplot 0.2.0
210

311
* Add [Scottish Government Design System colour palettes](https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes)
12+
413
* Reduce `base_line_size` in `theme_sg()`
14+
515
* Remove default dark blue outline from `geom_col` and `geom_bar` when using `use_sgplot()`.
616

717
# sgplot 0.1.0
818

919
* First package release
20+
1021
* Add functions to create accessible plots using `ggplot2`
22+
1123
* Add pkgdown site (https://datasciencescotland.github.io/sgplot) including [cookbook](https://datasciencescotland.github.io/sgplot/articles/cookbook.html)

R/scale_colour_continuous_sg.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ scale_colour_continuous_sg <- function(palette = "sequential",
2828

2929
palette_type <- match.arg(palette_type)
3030

31-
palette_list <- get(paste0(palette_type, "_colour_palettes"),
32-
as.environment("package:sgplot"))
31+
palette_list <- switch(
32+
palette_type,
33+
af = sgplot::af_colour_palettes,
34+
sg = sgplot::sg_colour_palettes
35+
)
3336

3437
# Error if palette doesn't exist
3538
if (!palette %in% names(palette_list)) {

R/scale_fill_continuous_sg.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ scale_fill_continuous_sg <- function(palette = "sequential",
2828

2929
palette_type <- match.arg(palette_type)
3030

31-
palette_list <- get(paste0(palette_type, "_colour_palettes"),
32-
as.environment("package:sgplot"))
31+
palette_list <- switch(
32+
palette_type,
33+
af = sgplot::af_colour_palettes,
34+
sg = sgplot::sg_colour_palettes
35+
)
3336

3437
# Error if palette doesn't exist
3538
if (!palette %in% names(palette_list)) {

R/sg_palette.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ sg_palette <- function(palette = "main",
1515

1616
palette_type <- match.arg(palette_type)
1717

18-
palette_list <- get(paste0(palette_type, "_colour_palettes"))
18+
palette_list <- switch(
19+
palette_type,
20+
af = sgplot::af_colour_palettes,
21+
sg = sgplot::sg_colour_palettes
22+
)
1923

2024
# Check valid palette name
2125
if (!palette %in% names(palette_list)) {

R/use_sgplot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#'
66
#' @param default_colour Default colour/fill for geoms. Default value is
77
#' 'blue' from \code{sgplot::sg_colour_values}.
8-
#' @param ... Arguments passed to \code{theme_sg}.
8+
#' @param ... Arguments passed to \code{sgplot::theme_sg()}.
99
#'
1010
#' @examples
1111
#' library(ggplot2)

_pkgdown.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ reference:
6262
- title: Colours
6363
contents:
6464
- starts_with("scale")
65+
66+
- title: Colour values
67+
contents:
6568
- starts_with("sg_colour")
6669
- starts_with("af_colour")
6770

man/use_sgplot.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/colours.Rmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ Follow the [advice for using the focus palette](https://analysisfunction.civilse
163163
The [Scottish Government focus palette](#focus-chart-palette) does not have this issue, so you may wish to use this instead.
164164

165165

166-
## Using a different colour palette
166+
## Using your own colour palette
167167

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

172-
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).
172+
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).
173173
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).
174174

175175

@@ -191,4 +191,4 @@ sgplot::sg_colour_palettes$main
191191
sgplot::af_colour_palettes
192192
```
193193

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

vignettes/cookbook/_colour-palettes.Rmd

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,78 @@
1-
## Using a different colour palette
1+
## Using different colour palettes
22

33
sgplot provides colour palettes as set out by the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes).
44
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).
55

66
The Analysis Function guidance also contains [suggested colour palettes](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-4).
77
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.
99

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

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.
1376
If so, this should be carefully considered to ensure it meets accessibility requirements.
1477
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.
1578

@@ -59,6 +122,7 @@ gapminder |>
59122
)
60123
```
61124

62-
### Adding a new colour palette to sgplot
125+
126+
#### Adding a new colour palette to sgplot
63127

64128
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

Comments
 (0)