From d617341804643b5fd148422d0c6396fa951a9d52 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Fri, 20 Oct 2023 14:35:08 +0100 Subject: [PATCH 1/5] Fixes #26 --- R/scale_colour_continuous_sg.R | 7 +++++-- R/scale_fill_continuous_sg.R | 7 +++++-- R/sg_palette.R | 6 +++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/R/scale_colour_continuous_sg.R b/R/scale_colour_continuous_sg.R index 468f854..c258a68 100644 --- a/R/scale_colour_continuous_sg.R +++ b/R/scale_colour_continuous_sg.R @@ -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)) { diff --git a/R/scale_fill_continuous_sg.R b/R/scale_fill_continuous_sg.R index cb6bf28..ef16e74 100644 --- a/R/scale_fill_continuous_sg.R +++ b/R/scale_fill_continuous_sg.R @@ -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)) { diff --git a/R/sg_palette.R b/R/sg_palette.R index 3ef279d..09c23d1 100644 --- a/R/sg_palette.R +++ b/R/sg_palette.R @@ -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)) { From 460b41a4b32beb2ee68aca4329bdd656a4f6fcb7 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Fri, 20 Oct 2023 15:05:35 +0100 Subject: [PATCH 2/5] Add link to theme_sg in use_sgplot help file --- R/use_sgplot.R | 2 +- man/use_sgplot.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/use_sgplot.R b/R/use_sgplot.R index 2e0d10c..44b2fe8 100644 --- a/R/use_sgplot.R +++ b/R/use_sgplot.R @@ -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) diff --git a/man/use_sgplot.Rd b/man/use_sgplot.Rd index 61055a1..ca67dc5 100644 --- a/man/use_sgplot.Rd +++ b/man/use_sgplot.Rd @@ -10,7 +10,7 @@ use_sgplot(default_colour = sgplot::sg_colour_values["dark-blue"], ...) \item{default_colour}{Default colour/fill for geoms. Default value is 'blue' from \code{sgplot::sg_colour_values}.} -\item{...}{Arguments passed to \code{theme_sg}.} +\item{...}{Arguments passed to \code{sgplot::theme_sg()}.} } \description{ Set sgplot theme, colour palette and geom aesthetic From 0b4ec4f5ca0caaa7a91f8fd101c89167367c116e Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Fri, 20 Oct 2023 15:05:54 +0100 Subject: [PATCH 3/5] Add section for colour values --- _pkgdown.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/_pkgdown.yml b/_pkgdown.yml index d579f47..c9c8348 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -62,6 +62,9 @@ reference: - title: Colours contents: - starts_with("scale") + +- title: Colour values + contents: - starts_with("sg_colour") - starts_with("af_colour") From 574af13f84849a865f11d9d255fac48feabda171 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Fri, 20 Oct 2023 16:04:53 +0100 Subject: [PATCH 4/5] Add examples of how to use non-default colour palettes (closes #24) --- vignettes/colours.Rmd | 6 +-- vignettes/cookbook/_colour-palettes.Rmd | 72 +++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/vignettes/colours.Rmd b/vignettes/colours.Rmd index d53e40c..1b509fb 100644 --- a/vignettes/colours.Rmd +++ b/vignettes/colours.Rmd @@ -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). @@ -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). diff --git a/vignettes/cookbook/_colour-palettes.Rmd b/vignettes/cookbook/_colour-palettes.Rmd index 4ab5ef3..6227fa2 100644 --- a/vignettes/cookbook/_colour-palettes.Rmd +++ b/vignettes/cookbook/_colour-palettes.Rmd @@ -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. @@ -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). From 21cedcc9703611254cea9eecb542c41058dda631 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Fri, 20 Oct 2023 16:18:07 +0100 Subject: [PATCH 5/5] Increment version number to 0.2.1 --- DESCRIPTION | 2 +- NEWS.md | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4ec3522..673cc87 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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")) diff --git a/NEWS.md b/NEWS.md index 5c31126..89ed7fe 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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)