Skip to content

Commit

Permalink
Merge pull request #27 from DataScienceScotland/bug-fix
Browse files Browse the repository at this point in the history
Bug fix and guidance edits
  • Loading branch information
alice-hannah authored Oct 24, 2023
2 parents 9799517 + 21cedcc commit a1b6e67
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sgplot
Title: Graphic Styles and Colours for Scottish Government Plots
Version: 0.2.0
Version: 0.2.1
Authors@R: c(
person("Scottish Government", , , "statistics.enquiries@gov.scot", role = c("cph", "fnd")),
person("Alice", "Byers", , "alice.byers@gov.scot", c("aut", "cre"))
Expand Down
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
# sgplot 0.2.1

* Fix issue where `scale_` functions didn't work without the package being loaded (#26)

* Add [examples of using non-default colour palettes](https://datasciencescotland.github.io/sgplot/articles/cookbook.html#using-different-colour-palettes) (#24)

* Add link to `theme_sg()` in `use_sgplot()` help file

# sgplot 0.2.0

* Add [Scottish Government Design System colour palettes](https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes)

* Reduce `base_line_size` in `theme_sg()`

* Remove default dark blue outline from `geom_col` and `geom_bar` when using `use_sgplot()`.

# sgplot 0.1.0

* First package release

* Add functions to create accessible plots using `ggplot2`

* Add pkgdown site (https://datasciencescotland.github.io/sgplot) including [cookbook](https://datasciencescotland.github.io/sgplot/articles/cookbook.html)
7 changes: 5 additions & 2 deletions R/scale_colour_continuous_sg.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ scale_colour_continuous_sg <- function(palette = "sequential",

palette_type <- match.arg(palette_type)

palette_list <- get(paste0(palette_type, "_colour_palettes"),
as.environment("package:sgplot"))
palette_list <- switch(
palette_type,
af = sgplot::af_colour_palettes,
sg = sgplot::sg_colour_palettes
)

# Error if palette doesn't exist
if (!palette %in% names(palette_list)) {
Expand Down
7 changes: 5 additions & 2 deletions R/scale_fill_continuous_sg.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ scale_fill_continuous_sg <- function(palette = "sequential",

palette_type <- match.arg(palette_type)

palette_list <- get(paste0(palette_type, "_colour_palettes"),
as.environment("package:sgplot"))
palette_list <- switch(
palette_type,
af = sgplot::af_colour_palettes,
sg = sgplot::sg_colour_palettes
)

# Error if palette doesn't exist
if (!palette %in% names(palette_list)) {
Expand Down
6 changes: 5 additions & 1 deletion R/sg_palette.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ sg_palette <- function(palette = "main",

palette_type <- match.arg(palette_type)

palette_list <- get(paste0(palette_type, "_colour_palettes"))
palette_list <- switch(
palette_type,
af = sgplot::af_colour_palettes,
sg = sgplot::sg_colour_palettes
)

# Check valid palette name
if (!palette %in% names(palette_list)) {
Expand Down
2 changes: 1 addition & 1 deletion R/use_sgplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#'
#' @param default_colour Default colour/fill for geoms. Default value is
#' 'blue' from \code{sgplot::sg_colour_values}.
#' @param ... Arguments passed to \code{theme_sg}.
#' @param ... Arguments passed to \code{sgplot::theme_sg()}.
#'
#' @examples
#' library(ggplot2)
Expand Down
3 changes: 3 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ reference:
- title: Colours
contents:
- starts_with("scale")

- title: Colour values
contents:
- starts_with("sg_colour")
- starts_with("af_colour")

Expand Down
2 changes: 1 addition & 1 deletion man/use_sgplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 a1b6e67

Please sign in to comment.