From 4a403f326f04e4f1698818b723a2896db9ca3111 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Wed, 26 Jul 2023 09:15:52 +0100 Subject: [PATCH 01/27] Add new SG palettes and rename AF palettes --- R/data.R | 29 +- R/scale_colour_continuous_sg.R | 18 +- R/scale_colour_discrete_sg.R | 12 +- R/scale_fill_continuous_sg.R | 18 +- R/scale_fill_discrete_sg.R | 10 +- R/sg_palette.R | 40 +- R/use_sgplot.R | 4 +- README.md | 7 +- data-raw/af_colours.R | 31 ++ data-raw/sg_colours.R | 44 +- data/af_colour_palettes.rda | Bin 0 -> 336 bytes data/af_colour_values.rda | Bin 0 -> 239 bytes data/sg_colour_palettes.rda | Bin 327 -> 344 bytes data/sg_colour_values.rda | Bin 238 -> 245 bytes man/af_colour_palettes.Rd | 19 + man/af_colour_values.Rd | 20 + man/figures/README-ex1-1.svg | 546 ++++++++++++------------- man/figures/README-ex2-1.svg | 524 ++++++++++++------------ man/scale_colour_continuous_sg.Rd | 4 + man/scale_colour_discrete_sg.Rd | 12 +- man/scale_fill_continuous_sg.Rd | 4 + man/scale_fill_discrete_sg.Rd | 10 +- man/sg_colour_palettes.Rd | 2 +- man/sg_colour_values.Rd | 2 +- man/sg_palette.Rd | 10 +- man/use_sgplot.Rd | 4 +- tests/testthat/test-sg_palette.R | 21 +- vignettes/cookbook/_annotations.Rmd | 12 +- vignettes/cookbook/_chart-types.Rmd | 28 +- vignettes/cookbook/_customisations.Rmd | 12 +- 30 files changed, 810 insertions(+), 633 deletions(-) create mode 100644 data-raw/af_colours.R create mode 100644 data/af_colour_palettes.rda create mode 100644 data/af_colour_values.rda create mode 100644 man/af_colour_palettes.Rd create mode 100644 man/af_colour_values.Rd diff --git a/R/data.R b/R/data.R index 5e598cc..5f9bbf1 100644 --- a/R/data.R +++ b/R/data.R @@ -5,7 +5,7 @@ #' hex code. #' #' @format A character vector -#' @source \href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance} +#' @source \href{https://designsystem.gov.scot/styles/colour}{Scottish Government Design System} # nolint end "sg_colour_values" @@ -17,7 +17,32 @@ #' @description A list grouping colours into palettes. #' #' @format A character list -#' @source \href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance} +#' @source \href{https://designsystem.gov.scot/styles/colour}{Scottish Government Design System} # nolint end "sg_colour_palettes" + + +# nolint start +#' @title Analysis Function colour names and hex codes +#' +#' @description A vector containing colour names and their corresponding +#' hex code. +#' +#' @format A character vector +#' @source \href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance} +# nolint end + +"af_colour_values" + + +# nolint start +#' @title Analysis Function colour palettes +#' +#' @description A list grouping colours into palettes. +#' +#' @format A character list +#' @source \href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance} +# nolint end + +"af_colour_palettes" diff --git a/R/scale_colour_continuous_sg.R b/R/scale_colour_continuous_sg.R index b0996b8..f3dd7b0 100644 --- a/R/scale_colour_continuous_sg.R +++ b/R/scale_colour_continuous_sg.R @@ -2,6 +2,8 @@ #' #' @param palette Name of palette to use from `sg_colour_palettes`; e.g. main, #' sequential, focus. Default value is 'sequential'. +#' @param palette_type Either `sg` to use Scottish Government palettes, or `af` +#' to use Analysis Function palettes. Defaults to `sg`. #' @param reverse Boolean value to indicate whether the palette should be #' reversed. #' @param na_colour Colour to set for missing values. @@ -18,22 +20,30 @@ #' @export scale_colour_continuous_sg <- function(palette = "sequential", + palette_type = c("sg", "af"), reverse = FALSE, na_colour = "grey50", guide = "colourbar", ...) { + + palette_type <- match.arg(palette_type) + + palette_list <- get(paste0(palette_type, "_colour_palettes"), + as.environment("package:sgplot")) + # Error if palette doesn't exist - if (!palette %in% names(sgplot::sg_colour_palettes)) { + if (!palette %in% names(palette_list)) { cli::cli_abort(c( - "x" = paste(palette, "is not a valid palette name.") + "x" = paste("`{palette}` is not a valid palette name in", + "`{palette_type}_colour_palettes`.") )) } - colours <- as.vector(sgplot::sg_colour_palettes[[palette]]) + colours <- as.vector(palette_list[[palette]]) ggplot2::continuous_scale( aesthetics = "colour", - scale_name = "sg_continuous", + scale_name = paste0(palette_type, "_continuous"), palette = scales::gradient_n_pal(colours, values = NULL, "Lab"), na.value = na_colour, guide = guide, diff --git a/R/scale_colour_discrete_sg.R b/R/scale_colour_discrete_sg.R index 60a8d7e..0ad4edb 100644 --- a/R/scale_colour_discrete_sg.R +++ b/R/scale_colour_discrete_sg.R @@ -2,6 +2,8 @@ #' #' @param palette Name of palette to use from `sg_colour_palettes`; e.g. main, #' sequential, focus. Default value is 'main'. +#' @param palette_type Either `sg` to use Scottish Government palettes, or `af` +#' to use Analysis Function palettes. Defaults to `sg`. #' @param reverse Boolean value to indicate whether the palette should be #' reversed. #' @param ... Additional arguments passed to scale type. @@ -14,16 +16,20 @@ #' filter(variable %in% c("psavert", "uempmed")) %>% #' ggplot(aes(x = date, y = value, colour = variable)) + #' geom_line(linewidth = 1) + -#' scale_colour_discrete_sg("main2") +#' scale_colour_discrete_sg() #' #' @export scale_colour_discrete_sg <- function(palette = "main", + palette_type = c("sg", "af"), reverse = FALSE, ...) { + + palette_type <- match.arg(palette_type) + ggplot2::discrete_scale("colour", - paste0("sg_", palette), - palette = sg_palette(palette, reverse), + paste0(palette_type, "_", palette), + palette = sg_palette(palette, reverse, palette_type = palette_type), ... ) } diff --git a/R/scale_fill_continuous_sg.R b/R/scale_fill_continuous_sg.R index 488ee96..1c9a123 100644 --- a/R/scale_fill_continuous_sg.R +++ b/R/scale_fill_continuous_sg.R @@ -2,6 +2,8 @@ #' #' @param palette Name of palette to use from `sg_colour_palettes`; e.g. main, #' sequential, focus. Default value is 'sequential'. +#' @param palette_type Either `sg` to use Scottish Government palettes, or `af` +#' to use Analysis Function palettes. Defaults to `sg`. #' @param reverse Boolean value to indicate whether the palette should be #' reversed. #' @param na_colour Colour to set for missing values. @@ -18,22 +20,30 @@ #' @export scale_fill_continuous_sg <- function(palette = "sequential", + palette_type = c("sg", "af"), reverse = FALSE, na_colour = "grey50", guide = "colourbar", ...) { + + palette_type <- match.arg(palette_type) + + palette_list <- get(paste0(palette_type, "_colour_palettes"), + as.environment("package:sgplot")) + # Error if palette doesn't exist - if (!palette %in% names(sgplot::sg_colour_palettes)) { + if (!palette %in% names(palette_list)) { cli::cli_abort(c( - "x" = paste(palette, "is not a valid palette name.") + "x" = paste("`{palette}` is not a valid palette name in", + "`{palette_type}_colour_palettes`.") )) } - colours <- as.vector(sgplot::sg_colour_palettes[[palette]]) + colours <- as.vector(palette_list[[palette]]) ggplot2::continuous_scale( aesthetics = "fill", - scale_name = "sg_continuous", + scale_name = paste0(palette_type, "_continuous"), palette = scales::gradient_n_pal(colours, values = NULL, "Lab"), na.value = na_colour, guide = guide, diff --git a/R/scale_fill_discrete_sg.R b/R/scale_fill_discrete_sg.R index 62d2c08..f95b043 100644 --- a/R/scale_fill_discrete_sg.R +++ b/R/scale_fill_discrete_sg.R @@ -2,6 +2,8 @@ #' #' @param palette Name of palette to use from `sg_colour_palettes`; e.g. main, #' sequential, focus. Default value is 'main'. +#' @param palette_type Either `sg` to use Scottish Government palettes, or `af` +#' to use Analysis Function palettes. Defaults to `sg`. #' @param reverse Boolean value to indicate whether the palette should be #' reversed. #' @param ... Additional arguments passed to scale type. @@ -18,11 +20,15 @@ #' @export scale_fill_discrete_sg <- function(palette = "main", + palette_type = c("sg", "af"), reverse = FALSE, ...) { + + palette_type <- match.arg(palette_type) + ggplot2::discrete_scale("fill", - paste0("sg_", palette), - palette = sg_palette(palette, reverse), + paste0(palette_type, "_", palette), + palette = sg_palette(palette, reverse, palette_type = palette_type), ... ) } diff --git a/R/sg_palette.R b/R/sg_palette.R index d04caae..2f796dc 100644 --- a/R/sg_palette.R +++ b/R/sg_palette.R @@ -5,47 +5,63 @@ #' reversed #' @param colour_names Boolean value to indicate whether colour names should be #' included +#' @param palette_type Either `sg` to use Scottish Government palettes, or `af` +#' to use Analysis Function palettes. Defaults to `sg`. sg_palette <- function(palette = "main", reverse = FALSE, - colour_names = FALSE) { + colour_names = FALSE, + palette_type = c("sg", "af")) { + + palette_type <- match.arg(palette_type) + + palette_list <- get(paste0(palette_type, "_colour_palettes"), + as.environment("package:sgplot")) # Check valid palette name - if (!palette %in% names(sgplot::sg_colour_palettes)) { + if (!palette %in% names(palette_list)) { cli::cli_abort(c( - "x" = paste0("`", palette, "` is not a valid palette name.") + "x" = paste("`{palette}` is not a valid palette name ", + "in `{palette_type}_colour_palettes`.") )) } function(n) { - n_available <- length(sgplot::sg_colour_palettes[[palette]]) + n_available <- length(palette_list[[palette]]) - # Use 'main2' if main palette used and only 2 colours required - if (n == 2 && palette != "main2" && grepl("main", palette)) { + # Use 'main2' if AF main palette used and only 2 colours required + if (palette_type == "af" && + n == 2 && palette != "main2" && grepl("main", palette)) { palette <- "main2" cli::cli_warn(c( "!" = "Using `main2` as only two colours are required." )) } + ext_palettes <- subset( + names(palette_list), + stringr::str_detect(names(palette_list), "^main[5-9]") + ) + # Error if more colours requested than exist in palette if (n > n_available) { cli::cli_abort(c( - "x" = paste0( - "There are not enough colours available in the `", - palette, "` palette (", n_available, " available)." + "x" = paste( + "There are not enough colours available in the `{palette}`", + "palette from `{palette_type}_colour_palettes`", + "({n_available} available)." ), "i" = paste( "Accessibility guidance recommends a limit of four", "colours per chart. If more than four colours are", "required, first consider chart redesign. If it is", - "essential to use more than four colours, the `main6`", - "palette can be used." + "essential to use more than four colours, the {ext_palettes}", + "palette{?s} can be used." ) )) } - pal <- sgplot::sg_colour_palettes[[palette]][seq_len(n)] + pal <- palette_list[[palette]][seq_len(n)] if (reverse) pal <- rev(pal) diff --git a/R/use_sgplot.R b/R/use_sgplot.R index 01c33a9..75f608e 100644 --- a/R/use_sgplot.R +++ b/R/use_sgplot.R @@ -4,7 +4,7 @@ #' defaults for ggplot2 charts. #' #' @param default_colour Default colour/fill for geoms. Default value is -#' 'dark-blue' from \code{sgplot::sg_colour_values}. +#' 'blue' from \code{sgplot::sg_colour_values}. #' @param ... Arguments passed to \code{theme_sg}. #' #' @examples @@ -23,7 +23,7 @@ #' @export -use_sgplot <- function(default_colour = sgplot::sg_colour_values["dark-blue"], +use_sgplot <- function(default_colour = sgplot::sg_colour_values["blue"], ...) { # Use sgplot theme ---- diff --git a/README.md b/README.md index 29b7c6a..9899e9c 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,7 @@ # sgplot sgplot logo - -[![GitHub release (latest by -date)](https://img.shields.io/github/v/release/DataScienceScotland/sgplot)](https://github.com/DataScienceScotland/sgplot/releases/latest) -[![R build -status](https://github.com/DataScienceScotland/sgplot/workflows/R-CMD-check/badge.svg)](https://github.com/DataScienceScotland/sgplot/actions) - + sgplot is an R package for creating accessible plots in the Scottish diff --git a/data-raw/af_colours.R b/data-raw/af_colours.R new file mode 100644 index 0000000..30dea24 --- /dev/null +++ b/data-raw/af_colours.R @@ -0,0 +1,31 @@ +# Government Analysis Function (AF) colours and palettes +# Source: https://analysisfunction.civilservice.gov.uk/policy-store/ +# data-visualisation-colours-in-charts/ + +af_colour_values <- c( + `dark-blue` = "#12436D", + `turquoise` = "#28A197", + `dark-pink` = "#801650", + `orange` = "#F46A25", + `dark-grey` = "#3D3D3D", + `light-purple` = "#A285D1", + `mid-blue` = "#2073BC", + `light-blue` = "#6BACE6", + `grey` = "#BFBFBF" +) + +af_colour_palettes <- list( + `main` = af_colour_values[c("dark-blue", "turquoise", "dark-pink", "orange")], + `main2` = af_colour_values[c("dark-blue", "orange")], + `main6` = af_colour_values[c( + "dark-blue", "turquoise", "dark-pink", + "orange", "dark-grey", "light-purple" + )], + `sequential` = af_colour_values[c("dark-blue", "mid-blue", "light-blue")], + `focus` = af_colour_values[c("dark-blue", "grey")] +) + +usethis::use_data(af_colour_values, af_colour_palettes, overwrite = TRUE) + + +### END OF SCRIPT ### diff --git a/data-raw/sg_colours.R b/data-raw/sg_colours.R index 719c662..5a1d315 100644 --- a/data-raw/sg_colours.R +++ b/data-raw/sg_colours.R @@ -1,31 +1,33 @@ -# This script defines the colours and colour palettes for use in Scottish -# Government plots. -# -# Colours are taken from Government Analysis Function guidance: -# https://analysisfunction.civilservice.gov.uk/policy-store/ -# data-visualisation-colours-in-charts/ +# Scottish Government (SG) Design System colours and palettes ---- +# Source: https://designsystem.gov.scot/styles/colour sg_colour_values <- c( - `dark-blue` = "#12436D", - `turquoise` = "#28A197", - `dark-pink` = "#801650", - `orange` = "#F46A25", - `dark-grey` = "#3D3D3D", - `light-purple` = "#A285D1", - `mid-blue` = "#2073BC", - `light-blue` = "#6BACE6", - `grey` = "#BFBFBF" + `blue` = "#002d54", + `teal` = "#2b9c93", + `purple` = "#6a2063", + `orange` = "#e5682a", + `dark-green` = "#0b4c0b", + `green` = "#5d9f3c", + `brown` = "592c20", + `pink` = "#ca72a2", + `mid-blue` = "#0065bd", + `light-blue` = "#55a8f2", + `grey` = "#949494" ) sg_colour_palettes <- list( - `main` = sg_colour_values[c("dark-blue", "turquoise", "dark-pink", "orange")], - `main2` = sg_colour_values[c("dark-blue", "orange")], + `main` = sg_colour_values[c("blue", "teal", "purple", "orange")], `main6` = sg_colour_values[c( - "dark-blue", "turquoise", "dark-pink", - "orange", "dark-grey", "light-purple" + "blue", "teal", "purple", + "orange", "dark-green", "green" )], - `sequential` = sg_colour_values[c("dark-blue", "mid-blue", "light-blue")], - `focus` = sg_colour_values[c("dark-blue", "grey")] + `main8` = sg_colour_values[c( + "blue", "teal", "purple", + "orange", "dark-green", "green", + "brown", "pink" + )], + `sequential` = sg_colour_values[c("blue", "mid-blue", "light-blue")], + `focus` = sg_colour_values[c("blue", "grey")] ) usethis::use_data(sg_colour_values, sg_colour_palettes, overwrite = TRUE) diff --git a/data/af_colour_palettes.rda b/data/af_colour_palettes.rda new file mode 100644 index 0000000000000000000000000000000000000000..e3c473aefbee1253a71030bad7621e42eec0b6b8 GIT binary patch literal 336 zcmV-W0k8f-T4*^jL0KkKSzq#P4gdi{e}Mn=SO5qDf8aj=5J120{vZGWumL+pAtaJ| zBTrH4X&Y12$&fN)VIG4`AZV!5)Y%}>wJ|b!gF)z+2mlf#H7A2q^$#f1L7)M&fHWIY z35BdPT3ti~7=X!sG9wV%m8msz%``|7pF9jin1}!Z42YDT-Wa~JGw$A%O(dy6hJ>0?2!;w8NCO@CR19sgvFSiev$iUQb-7Zu}Ao50v ziYrEx)^d91X=1BVF*m3zTZLtcTH>jalwO|24mbMCGGpUsg?;!~thT4E09KbPX5t6+ zyjjV=M+0Eg_!d_ceBT%sr<}Z~B2GXHmm4LL_Dcjwdty>ZoN}wgx1yq|Ol@_<(NvHE iMD}PH`<_(4%)*vRdMPiY*p|e9i@744C`d2)HirOE7n4!| literal 0 HcmV?d00001 diff --git a/data/af_colour_values.rda b/data/af_colour_values.rda new file mode 100644 index 0000000000000000000000000000000000000000..35cefb77cba0a8905bc34b582317e767f2cd3e77 GIT binary patch literal 239 zcmV;K?Xn?WIaFt02oIMWEnyS%KQ1o9SlJ+ zE%J1W1L%EFRYYz;T#`{`5N`lN1fa~wG6a*ZmJlI~f%1MRH1BlTrm1njFh-aP12jT~ zTGTme_`uEynB=V!Dg+}5B+!D-Nt8jih6Okz|Jgs4r# pvN*1XEsNLQ`RJXDlH~x&p}`JdsV*UUC literal 0 HcmV?d00001 diff --git a/data/sg_colour_palettes.rda b/data/sg_colour_palettes.rda index fdbbd4a7d8db16f2452ee29ce8f6c3495e8a7b23..7614271339b925d4b57b6ce6947b1dbe6099047f 100644 GIT binary patch literal 344 zcmV-e0jK^#T4*^jL0KkKS(gKO#Q*_se}Mn+SO5qDU*H4)5J120{-6K>umLzAHmMmT z&m%&fr?g1ZLFx=l&>?^tXwYeh000I+2$YGUdNk58G-MhyVgMRY#$$nl8kbc?ghhl- zN7QLe+-JAP}%cib_WB7+ik~wVmx@8TDH;1 z8?Vl`!A9gp0!5`7KxqL);iidHl0r!*c&F}Ng`OjoD9)0j*FH+<-tV#Di6gD%DZwGA z%9wP$c4DmTv@2>UYl)jtilO7y?Z11tb&^#Or!zZ_o;paYl6j|>xS9EqlA~FMeKp%a zps-!AVq3;cnW)IQDJ0$)yTXr~71}W?onECE!XC$-s+8$YGTZM5(w)BU>?|DOY4Y2V qTzw%h(?&5og#}wjF_v}g@>kIi?@WtkcM#$)@pmLsg$W68H=IzIouK^y literal 327 zcmV-N0l5A`T4*^jL0KkKS(ix}SpWe-e}Mn=SO5qDf8aj=5J120{vZGWumL)Vgpx_* zr-EWNJd;PL8X65WJq=GtFwoEd02%-Q002mo)SeAZL&`J;fCkb4&}~R27O>4}br1|< z110##j6-f#q}9(f(ID$@H(w2JKm-{jESPb=&Jb%Yx?^gqZo(tMnphG^T!n^=_oY)w zDo`PzCX_-Uf`*a+$9fe58*FTPP#bSYyHSDSRey3YH5^wFieHcXT66c>3wXo+})#%&!CSEHaPe59zlvlW!aiM#s)? zeswABo?=O5IV#FYN&wB7wDE>1QBe+6D56e2^03Q-uT`do=y4q?(r_YAW}MNNGgZ*m ZQkF|UB_rXOc!rM1&yf zHeEVRKuBSUH*jcL@8xy=f1Dq6=zT!mhzLEcRVXtvA9;Bbhx9a+Vj?g&b|vpBW-Nw- vsjC#Tp+6W*VTM}5qtnhVZDLSs=2MMn;7|A7DWKmZ5=f8aj=5J0}~{~!PXFaWu*X(pzR zPfBEZfM@^!&>qmC6+JRB0%BkQCMEzA0f|ICG(nICjSo-&01O`+$TI{FmG|?EdRT&F zTqWry2vZ88s+YL|a#@9xLA(J75`!}!$P!M>%pyY>1LYi$Y9Q(J&r-v5V29UA12f`z zOA|S2t#OEPzXiQMS%zD5-5Xa{1!q_n?ZOhKJzI&`z#`iEvy|Hz{&(-!zH`) o=tvRsUUVT-s#2$Gg#@thX%Us6Nb!8mVdMC_k}1N3fHM7?$SspzkN^Mx diff --git a/man/af_colour_palettes.Rd b/man/af_colour_palettes.Rd new file mode 100644 index 0000000..f40e328 --- /dev/null +++ b/man/af_colour_palettes.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{af_colour_palettes} +\alias{af_colour_palettes} +\title{Analysis Function colour palettes} +\format{ +A character list +} +\source{ +\href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance} +} +\usage{ +af_colour_palettes +} +\description{ +A list grouping colours into palettes. +} +\keyword{datasets} diff --git a/man/af_colour_values.Rd b/man/af_colour_values.Rd new file mode 100644 index 0000000..779bf5a --- /dev/null +++ b/man/af_colour_values.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{af_colour_values} +\alias{af_colour_values} +\title{Analysis Function colour names and hex codes} +\format{ +A character vector +} +\source{ +\href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance} +} +\usage{ +af_colour_values +} +\description{ +A vector containing colour names and their corresponding +hex code. +} +\keyword{datasets} diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index c0d844f..cd2b694 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -3,532 +3,532 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index d3708e3..ea1c6c0 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -3,500 +3,500 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/man/scale_colour_continuous_sg.Rd b/man/scale_colour_continuous_sg.Rd index 0a5d67e..e5ff381 100644 --- a/man/scale_colour_continuous_sg.Rd +++ b/man/scale_colour_continuous_sg.Rd @@ -6,6 +6,7 @@ \usage{ scale_colour_continuous_sg( palette = "sequential", + palette_type = c("sg", "af"), reverse = FALSE, na_colour = "grey50", guide = "colourbar", @@ -16,6 +17,9 @@ scale_colour_continuous_sg( \item{palette}{Name of palette to use from `sg_colour_palettes`; e.g. main, sequential, focus. Default value is 'sequential'.} +\item{palette_type}{Either `sg` to use Scottish Government palettes, or `af` +to use Analysis Function palettes. Defaults to `sg`.} + \item{reverse}{Boolean value to indicate whether the palette should be reversed.} diff --git a/man/scale_colour_discrete_sg.Rd b/man/scale_colour_discrete_sg.Rd index 04ca343..ff30150 100644 --- a/man/scale_colour_discrete_sg.Rd +++ b/man/scale_colour_discrete_sg.Rd @@ -4,12 +4,20 @@ \alias{scale_colour_discrete_sg} \title{Discrete colour scales for Scottish Government plots} \usage{ -scale_colour_discrete_sg(palette = "main", reverse = FALSE, ...) +scale_colour_discrete_sg( + palette = "main", + palette_type = c("sg", "af"), + reverse = FALSE, + ... +) } \arguments{ \item{palette}{Name of palette to use from `sg_colour_palettes`; e.g. main, sequential, focus. Default value is 'main'.} +\item{palette_type}{Either `sg` to use Scottish Government palettes, or `af` +to use Analysis Function palettes. Defaults to `sg`.} + \item{reverse}{Boolean value to indicate whether the palette should be reversed.} @@ -26,6 +34,6 @@ economics_long \%>\% filter(variable \%in\% c("psavert", "uempmed")) \%>\% ggplot(aes(x = date, y = value, colour = variable)) + geom_line(linewidth = 1) + - scale_colour_discrete_sg("main2") + scale_colour_discrete_sg() } diff --git a/man/scale_fill_continuous_sg.Rd b/man/scale_fill_continuous_sg.Rd index 6c4dcc8..5296d03 100644 --- a/man/scale_fill_continuous_sg.Rd +++ b/man/scale_fill_continuous_sg.Rd @@ -6,6 +6,7 @@ \usage{ scale_fill_continuous_sg( palette = "sequential", + palette_type = c("sg", "af"), reverse = FALSE, na_colour = "grey50", guide = "colourbar", @@ -16,6 +17,9 @@ scale_fill_continuous_sg( \item{palette}{Name of palette to use from `sg_colour_palettes`; e.g. main, sequential, focus. Default value is 'sequential'.} +\item{palette_type}{Either `sg` to use Scottish Government palettes, or `af` +to use Analysis Function palettes. Defaults to `sg`.} + \item{reverse}{Boolean value to indicate whether the palette should be reversed.} diff --git a/man/scale_fill_discrete_sg.Rd b/man/scale_fill_discrete_sg.Rd index 216ce9e..7369c14 100644 --- a/man/scale_fill_discrete_sg.Rd +++ b/man/scale_fill_discrete_sg.Rd @@ -4,12 +4,20 @@ \alias{scale_fill_discrete_sg} \title{Discrete colour fill scales for Scottish Government plots} \usage{ -scale_fill_discrete_sg(palette = "main", reverse = FALSE, ...) +scale_fill_discrete_sg( + palette = "main", + palette_type = c("sg", "af"), + reverse = FALSE, + ... +) } \arguments{ \item{palette}{Name of palette to use from `sg_colour_palettes`; e.g. main, sequential, focus. Default value is 'main'.} +\item{palette_type}{Either `sg` to use Scottish Government palettes, or `af` +to use Analysis Function palettes. Defaults to `sg`.} + \item{reverse}{Boolean value to indicate whether the palette should be reversed.} diff --git a/man/sg_colour_palettes.Rd b/man/sg_colour_palettes.Rd index 1ff3eb5..d2dbd34 100644 --- a/man/sg_colour_palettes.Rd +++ b/man/sg_colour_palettes.Rd @@ -8,7 +8,7 @@ A character list } \source{ -\href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance} +\href{https://designsystem.gov.scot/styles/colour}{Scottish Government Design System} } \usage{ sg_colour_palettes diff --git a/man/sg_colour_values.Rd b/man/sg_colour_values.Rd index 96090cf..2da6f93 100644 --- a/man/sg_colour_values.Rd +++ b/man/sg_colour_values.Rd @@ -8,7 +8,7 @@ A character vector } \source{ -\href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance} +\href{https://designsystem.gov.scot/styles/colour}{Scottish Government Design System} } \usage{ sg_colour_values diff --git a/man/sg_palette.Rd b/man/sg_palette.Rd index 851ef51..21c3764 100644 --- a/man/sg_palette.Rd +++ b/man/sg_palette.Rd @@ -4,7 +4,12 @@ \alias{sg_palette} \title{Return function to use Scottish Government colour palette} \usage{ -sg_palette(palette = "main", reverse = FALSE, colour_names = FALSE) +sg_palette( + palette = "main", + reverse = FALSE, + colour_names = FALSE, + palette_type = c("sg", "af") +) } \arguments{ \item{palette}{Name of palette from `sg_colour_palettes`} @@ -14,6 +19,9 @@ reversed} \item{colour_names}{Boolean value to indicate whether colour names should be included} + +\item{palette_type}{Either `sg` to use Scottish Government palettes, or `af` +to use Analysis Function palettes. Defaults to `sg`.} } \description{ Return function to use Scottish Government colour palette diff --git a/man/use_sgplot.Rd b/man/use_sgplot.Rd index f0aa31c..585f3db 100644 --- a/man/use_sgplot.Rd +++ b/man/use_sgplot.Rd @@ -4,11 +4,11 @@ \alias{use_sgplot} \title{Use sgplot defaults.} \usage{ -use_sgplot(default_colour = sgplot::sg_colour_values["dark-blue"], ...) +use_sgplot(default_colour = sgplot::sg_colour_values["blue"], ...) } \arguments{ \item{default_colour}{Default colour/fill for geoms. Default value is -'dark-blue' from \code{sgplot::sg_colour_values}.} +'blue' from \code{sgplot::sg_colour_values}.} \item{...}{Arguments passed to \code{theme_sg}.} } diff --git a/tests/testthat/test-sg_palette.R b/tests/testthat/test-sg_palette.R index 5927e16..ab65bfc 100644 --- a/tests/testthat/test-sg_palette.R +++ b/tests/testthat/test-sg_palette.R @@ -3,7 +3,10 @@ test_that("Correct value returned", { sg_palette()(4), unname(sg_colour_palettes$main) ) - expect_equal(sg_palette("main2")(2), unname(sg_colour_palettes$main2)) + expect_equal( + sg_palette(palette_type = "af")(4), + unname(af_colour_palettes$main) + ) expect_equal( sg_palette("main6", colour_names = TRUE)(5), sg_colour_palettes$main6[1:5] @@ -18,20 +21,22 @@ test_that("Correct value returned", { ) }) +test_that("Error if invalid palette name", { + expect_error(sg_palette("main2")(2)) + expect_error(sg_palette("af_main_palette")(2)) +}) + test_that("Error if too many colours requested", { expect_error(sg_palette("main")(5)) expect_error(sg_palette("sequential")(10)) expect_error(sg_palette("focus")(3)) + expect_error(sg_palette("main", palette_type = "af")(5)) }) test_that("Use `main2` if two colours required.", { + expect_warning(sg_palette("main", palette_type = "af")(2)) expect_equal( - suppressWarnings(sg_palette("main")(2)), - unname(sg_colour_palettes$main2) - ) - expect_equal( - suppressWarnings(sg_palette("main")(2)), - sg_palette("main2")(2) + suppressWarnings(sg_palette("main", palette_type = "af")(2)), + unname(af_colour_palettes$main2) ) - expect_warning(sg_palette("main")(2)) }) diff --git a/vignettes/cookbook/_annotations.Rmd b/vignettes/cookbook/_annotations.Rmd index c87354f..ca33fde 100644 --- a/vignettes/cookbook/_annotations.Rmd +++ b/vignettes/cookbook/_annotations.Rmd @@ -10,24 +10,24 @@ ann_data <- gapminder |> ``` ```{r annotations-1} -#| fig.alt = "A multiple line chart with colour co-ordinated line labels using sgplot theme and main2 palette." +#| fig.alt = "A multiple line chart with colour co-ordinated line labels using sgplot theme and main palette." ann_data |> ggplot(aes(x = year, y = lifeExp, colour = country)) + geom_line(linewidth = 1) + theme_sg(legend = "none") + - scale_colour_discrete_sg("main2") + + scale_colour_discrete_sg() + scale_y_continuous(limits = c(0, 82), breaks = seq(0, 80, 20), expand = c(0, 0)) + scale_x_continuous(limits = c(1952, 2017), breaks = seq(1952, 2017, 5)) + annotate(geom = "label", x = 2008, y = 73, label = "China", - colour = sg_colour_values["dark-blue"], + colour = sg_colour_values[1], label.size = NA, hjust = 0, vjust = 0.5) + annotate(geom = "label", x = 2008, y = 79.4, label = "United Kingdom", - colour = sg_colour_values["orange"], + colour = sg_colour_values[2], label.size = NA, hjust = 0, vjust = 0.5) + labs( @@ -59,7 +59,7 @@ ann_data |> ggplot(aes(x = year, y = lifeExp, colour = country)) + geom_line(linewidth = 1) + theme_sg(legend = "none") + - scale_colour_discrete_sg("main2") + + scale_colour_discrete_sg() + scale_y_continuous(limits = c(0, 82), breaks = seq(0, 80, 20), expand = c(0, 0)) + @@ -86,7 +86,7 @@ Annotations may also be used to add value labels to a bar chart. Note that `geom #| fig.alt = "A bar chart with white text labels at the end of each bar." ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + - geom_col(fill = sg_colour_values["dark-blue"]) + + geom_col(fill = sg_colour_values["blue"]) + geom_text(aes(label = round(lifeExp, 1)), nudge_y = -5, colour = "white") + theme_sg() + diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd index c5c9d38..ae25834 100644 --- a/vignettes/cookbook/_chart-types.Rmd +++ b/vignettes/cookbook/_chart-types.Rmd @@ -8,7 +8,7 @@ gapminder |> filter(country == "United Kingdom") |> ggplot(aes(x = year, y = lifeExp)) + - geom_line(linewidth = 1, colour = sg_colour_values["dark-blue"]) + + geom_line(linewidth = 1, colour = sg_colour_values["blue"]) + theme_sg() + scale_y_continuous(limits = c(0, 82), breaks = seq(0, 80, 20), @@ -26,14 +26,14 @@ gapminder |> ### Line chart with multiple lines ```{r line-charts-2, fig.height = 5} -#| fig.alt = "A multiple line chart using sgplot theme and main2 colour palette." +#| fig.alt = "A multiple line chart using sgplot theme and 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") + + scale_colour_discrete_sg() + scale_y_continuous(limits = c(0, 82), breaks = seq(0, 80, 20), expand = c(0, 0)) + @@ -64,7 +64,7 @@ bar_data <- #| fig.alt = "A bar chart using sgplot theme and dark blue colour." ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + - geom_col(fill = sg_colour_values["dark-blue"]) + + geom_col(fill = sg_colour_values["blue"]) + theme_sg() + scale_y_continuous(expand = c(0, 0)) + labs( @@ -82,7 +82,7 @@ A bar chart can sometimes look better with horizontal bars. This can also be a g #| fig.alt = "A horizontal bar chart using sgplot theme and dark blue colour." ggplot(bar_data, aes(x = lifeExp, y = reorder(country, lifeExp))) + - geom_col(fill = sg_colour_values["dark-blue"]) + + geom_col(fill = sg_colour_values["blue"]) + theme_sg(grid = "x", axis = "y") + scale_x_continuous(expand = c(0, 0)) + labs( @@ -100,7 +100,7 @@ ggplot(bar_data, aes(x = lifeExp, y = reorder(country, lifeExp))) + To create a grouped bar chart, set `stat = "identity"` and `position = "dodge"` in the call to `geom_bar()`. Also assign a variable to `fill` within `aes()` to determine what variable is used to create bars within groups. The `legend` argument in `theme_sg()` can be used to set the position of the legend. ```{r grouped-bar-chart, fig.height = 5.5} -#| fig.alt = "A grouped bar chart using sgplot theme and colours from main2 palette" +#| fig.alt = "A grouped bar chart using sgplot theme and colours from main palette" grouped_bar_data <- gapminder |> @@ -112,7 +112,7 @@ ggplot(grouped_bar_data, geom_bar(stat = "identity", position = "dodge") + scale_y_continuous(expand = c(0, 0)) + theme_sg(legend = "bottom") + - scale_fill_discrete_sg("main2") + + scale_fill_discrete_sg() + labs( x = "Country", y = NULL, @@ -130,7 +130,7 @@ To create a stacked bar chart, set `stat = "identity` and `position = "fill"` in Caution should be taken when producing stacked bar charts. They can quickly become difficult to interpret if plotting non part-to-whole data, and/or if plotting more than two categories per stack. First and last categories in the stack will always be easier to compare across bars than those in the middle. Think carefully about the story you are trying to tell with your chart. ```{r stacked-bar-chart, fig.height = 5.5} -#| fig.alt = "A stacked bar chart using sgplot theme and colours from main2 palette" +#| fig.alt = "A stacked bar chart using sgplot theme and colours from main palette" stacked_bar_data <- gapminder |> @@ -147,7 +147,7 @@ ggplot(stacked_bar_data, theme_sg(legend = "bottom") + scale_y_continuous(expand = c(0, 0), labels = scales::percent) + coord_cartesian(clip = "off") + - scale_fill_discrete_sg("main2") + + scale_fill_discrete_sg() + labs( x = NULL, y = NULL, @@ -169,7 +169,7 @@ gapminder |> ggplot(aes(x = lifeExp)) + geom_histogram(binwidth = 5, colour = "white", - fill = sg_colour_values["dark-blue"]) + + fill = sg_colour_values["blue"]) + theme_sg() + scale_y_continuous(expand = c(0, 0)) + labs( @@ -190,7 +190,7 @@ gapminder |> gapminder |> filter(year == 2007) |> ggplot(aes(x = gdpPercap, y = lifeExp, size = pop)) + - geom_point(colour = sg_colour_values["dark-blue"]) + + geom_point(colour = sg_colour_values["blue"]) + theme_sg(axis = "none", grid = "xy") + scale_x_continuous( labels = function(x) scales::dollar(x, prefix = "£") @@ -240,7 +240,7 @@ gapminder |> ## Pie charts ```{r pie-chart} -#| fig.alt = "A pie chart using sgplot theme and main2 palette." +#| fig.alt = "A pie chart using sgplot theme and main palette." stacked_bar_data |> filter(continent == "Europe") |> @@ -249,7 +249,7 @@ stacked_bar_data |> coord_polar(theta = "y") + theme_sg(grid = "none", axis = "none", ticks = "none") + theme(axis.text = element_blank()) + - scale_fill_discrete_sg("main2") + + scale_fill_discrete_sg() + labs( x = NULL, y = NULL, @@ -305,7 +305,7 @@ p <- "Life Expectancy: ", round(lifeExp, 1) )) |> ggplot(aes(x = reorder(country, -lifeExp), y = lifeExp, text = tooltip)) + - geom_col(fill = sg_colour_values["dark-blue"]) + + geom_col(fill = sg_colour_values["blue"]) + theme_sg(ticks = "x") + theme(text = element_text(family = "")) + scale_y_continuous(expand = c(0, 0)) + diff --git a/vignettes/cookbook/_customisations.Rmd b/vignettes/cookbook/_customisations.Rmd index 6d5c45f..d506935 100644 --- a/vignettes/cookbook/_customisations.Rmd +++ b/vignettes/cookbook/_customisations.Rmd @@ -11,7 +11,7 @@ To control the order of bars in a chart, wrap the variable you want to arrange w bar_data |> ggplot(aes(x = lifeExp, y = reorder(country, lifeExp))) + - geom_col(fill = sg_colour_values["dark-blue"]) + + geom_col(fill = sg_colour_values["blue"]) + theme_sg(axis = "y", grid = "x") ``` @@ -78,7 +78,7 @@ stacked_bar_data |> geom_bar(stat = "identity", position = "fill") + theme_sg(legend = "bottom") + scale_y_continuous(expand = c(0, 0), labels = scales::percent) + - scale_fill_discrete_sg("main2") + + scale_fill_discrete_sg() + labs( x = NULL, y = NULL, @@ -111,8 +111,8 @@ To add a horizontal or vertical line across the whole plot, use `geom_hline()` o gapminder |> filter(country == "United Kingdom") |> ggplot(aes(x = year, y = lifeExp)) + - geom_line(linewidth = 1, colour = sg_colour_values["dark-blue"]) + - geom_hline(yintercept = 75, colour = sg_colour_values["orange"], + geom_line(linewidth = 1, colour = sg_colour_values[1]) + + geom_hline(yintercept = 75, colour = sg_colour_values[2], linewidth = 1, linetype = "dashed") + annotate(geom = "text", x = 2007, y = 70, label = "Age 70") + theme_sg() + @@ -139,7 +139,7 @@ If text is too long, it may be cut off or distort the dimensions of the chart. plot <- ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + - geom_col(fill = sg_colour_values["dark-blue"]) + + geom_col(fill = sg_colour_values["blue"]) + theme_sg() + scale_y_continuous(expand = c(0, 0)) + labs( @@ -181,7 +181,7 @@ If you find you need to adjust theme elements for your chart, this can be done u #| fig.alt = "A bar chart using sgplot theme and dark blue colour, with axis lines and ticks coloured black." ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + - geom_col(fill = sg_colour_values["dark-blue"]) + + geom_col(fill = sg_colour_values["blue"]) + theme_sg(axis = "xy") + theme(axis.line = element_line(colour = "black"), axis.ticks = element_line(colour = "black")) + From eb127659b37a07ae90b9d3db0b22ca6743052b4f Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Wed, 26 Jul 2023 08:28:32 +0000 Subject: [PATCH 02/27] Re-build README.md --- README.md | 7 +- man/figures/README-ex1-1.svg | 546 +++++++++++++++++------------------ man/figures/README-ex2-1.svg | 524 ++++++++++++++++----------------- 3 files changed, 541 insertions(+), 536 deletions(-) diff --git a/README.md b/README.md index 9899e9c..29b7c6a 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,12 @@ # sgplot sgplot logo - + +[![GitHub release (latest by +date)](https://img.shields.io/github/v/release/DataScienceScotland/sgplot)](https://github.com/DataScienceScotland/sgplot/releases/latest) +[![R build +status](https://github.com/DataScienceScotland/sgplot/workflows/R-CMD-check/badge.svg)](https://github.com/DataScienceScotland/sgplot/actions) + sgplot is an R package for creating accessible plots in the Scottish diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index cd2b694..c0d844f 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -3,532 +3,532 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index ea1c6c0..3c656f3 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -3,500 +3,500 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + From 87fe0217e51a0eef73e97f15a3fdddf72dfc0947 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Mon, 7 Aug 2023 09:12:55 +0100 Subject: [PATCH 03/27] Remove envir call in get() --- R/sg_palette.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/sg_palette.R b/R/sg_palette.R index 2f796dc..dccf8ea 100644 --- a/R/sg_palette.R +++ b/R/sg_palette.R @@ -15,8 +15,7 @@ sg_palette <- function(palette = "main", palette_type <- match.arg(palette_type) - palette_list <- get(paste0(palette_type, "_colour_palettes"), - as.environment("package:sgplot")) + palette_list <- get(paste0(palette_type, "_colour_palettes")) # Check valid palette name if (!palette %in% names(palette_list)) { From ed3abea0224707dc07603073a8eceb281badaf7b Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Mon, 7 Aug 2023 09:13:13 +0100 Subject: [PATCH 04/27] Merge main6 and main8 into main-extended --- data-raw/sg_colours.R | 6 +----- data/sg_colour_palettes.rda | Bin 344 -> 328 bytes tests/testthat/test-sg_palette.R | 4 ++-- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/data-raw/sg_colours.R b/data-raw/sg_colours.R index 5a1d315..080f901 100644 --- a/data-raw/sg_colours.R +++ b/data-raw/sg_colours.R @@ -17,11 +17,7 @@ sg_colour_values <- c( sg_colour_palettes <- list( `main` = sg_colour_values[c("blue", "teal", "purple", "orange")], - `main6` = sg_colour_values[c( - "blue", "teal", "purple", - "orange", "dark-green", "green" - )], - `main8` = sg_colour_values[c( + `main-extended` = sg_colour_values[c( "blue", "teal", "purple", "orange", "dark-green", "green", "brown", "pink" diff --git a/data/sg_colour_palettes.rda b/data/sg_colour_palettes.rda index 7614271339b925d4b57b6ce6947b1dbe6099047f..714f063d42d0df9822cf5a5ae0d4cb17ba4900f2 100644 GIT binary patch literal 328 zcmV-O0k{4_T4*^jL0KkKS-jL6X#fEpe}Mn;SO5qDU*H4)5J120{@?%sumL+(7)pX_ zdY+9FG?VnvBTWscZ4mSX5~rw>$eL&dfDHkN00AUaWDir+000dD8UsflSYj-tfPit4 zLFI>exwq;dh9c703SybF=6Z0!jIfx-C{J=%c4)e+HAF-`@Q&2TuDS9fSaKxae|7NhPEM+>&F4#umY%o!szE6G}>6RTv_n%KHS8)D4M_jWan5 z6h2`qq`YM0`EAE8U1Exd)0v&8O^!8dC>{Om3H;x7j0tDc3n0*@Q0*k6Au3DF=aff_ zXB*b&x8q&fY9O}Cx*q+;$?94bCflwNM>cWq5(E^%YR7gZmF=ZO!*HyMA7sN4n04(z a;`uhQ9xA-GQWJ`B{x0N-aG@c2s5a8;bdq`i literal 344 zcmV-e0jK^#T4*^jL0KkKS(gKO#Q*_se}Mn+SO5qDU*H4)5J120{-6K>umLzAHmMmT z&m%&fr?g1ZLFx=l&>?^tXwYeh000I+2$YGUdNk58G-MhyVgMRY#$$nl8kbc?ghhl- zN7QLe+-JAP}%cib_WB7+ik~wVmx@8TDH;1 z8?Vl`!A9gp0!5`7KxqL);iidHl0r!*c&F}Ng`OjoD9)0j*FH+<-tV#Di6gD%DZwGA z%9wP$c4DmTv@2>UYl)jtilO7y?Z11tb&^#Or!zZ_o;paYl6j|>xS9EqlA~FMeKp%a zps-!AVq3;cnW)IQDJ0$)yTXr~71}W?onECE!XC$-s+8$YGTZM5(w)BU>?|DOY4Y2V qTzw%h(?&5og#}wjF_v}g@>kIi?@WtkcM#$)@pmLsg$W68H=IzIouK^y diff --git a/tests/testthat/test-sg_palette.R b/tests/testthat/test-sg_palette.R index ab65bfc..a694c9c 100644 --- a/tests/testthat/test-sg_palette.R +++ b/tests/testthat/test-sg_palette.R @@ -8,8 +8,8 @@ test_that("Correct value returned", { unname(af_colour_palettes$main) ) expect_equal( - sg_palette("main6", colour_names = TRUE)(5), - sg_colour_palettes$main6[1:5] + sg_palette("main-extended", colour_names = TRUE)(5), + sg_colour_palettes$`main-extended`[1:5] ) expect_equal( sg_palette("sequential", colour_names = TRUE)(3), From a3a581e2cc4689ffd50ba699c4acbc3f632bf4b6 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Mon, 7 Aug 2023 10:02:03 +0100 Subject: [PATCH 05/27] Add missing # in hex code --- data-raw/sg_colours.R | 2 +- data/sg_colour_palettes.rda | Bin 328 -> 328 bytes data/sg_colour_values.rda | Bin 245 -> 246 bytes 3 files changed, 1 insertion(+), 1 deletion(-) diff --git a/data-raw/sg_colours.R b/data-raw/sg_colours.R index 080f901..174a99b 100644 --- a/data-raw/sg_colours.R +++ b/data-raw/sg_colours.R @@ -8,7 +8,7 @@ sg_colour_values <- c( `orange` = "#e5682a", `dark-green` = "#0b4c0b", `green` = "#5d9f3c", - `brown` = "592c20", + `brown` = "#592c20", `pink` = "#ca72a2", `mid-blue` = "#0065bd", `light-blue` = "#55a8f2", diff --git a/data/sg_colour_palettes.rda b/data/sg_colour_palettes.rda index 714f063d42d0df9822cf5a5ae0d4cb17ba4900f2..1a4d451b6be604913e79183a5c30a0b0bee3e00f 100644 GIT binary patch literal 328 zcmV-O0k{4_T4*^jL0KkKS;BBvPyhiQ|A7DUSO5qDU*H4)5J120{@?%sumL+$7-|Wr z>S^RpO%uXE(V#R9G|&>7iKeHikRt*ffDDMhCK5$ML5)BFXbk`w14kqyVho~?Ksd;x z^25CR+x0Rbh%{1#QBL1~Q>TnGK{GOrE7U)mh5WF*c?eG$DMgl9q?=2MR8WPnM#w=Z z2w?*W-8%?&5CO{Tq3Izcl}rbTOC)tMbQ~-X<@0i991ate+5}cf)q*nK2;&qY>rC;* z6AAR0NJy6&w(Mv-*xkh?|gubi*z3qNTh+l7P#=#U_#Z_HFb aMhRdi>EbChVu4T}#oUoj6eKX571RLpMTvX> literal 328 zcmV-O0k{4_T4*^jL0KkKS-jL6X#fEpe}Mn;SO5qDU*H4)5J120{@?%sumL+(7)pX_ zdY+9FG?VnvBTWscZ4mSX5~rw>$eL&dfDHkN00AUaWDir+000dD8UsflSYj-tfPit4 zLFI>exwq;dh9c703SybF=6Z0!jIfx-C{J=%c4)e+HAF-`@Q&2TuDS9fSaKxae|7NhPEM+>&F4#umY%o!szE6G}>6RTv_n%KHS8)D4M_jWan5 z6h2`qq`YM0`EAE8U1Exd)0v&8O^!8dC>{Om3H;x7j0tDc3n0*@Q0*k6Au3DF=aff_ zXB*b&x8q&fY9O}Cx*q+;$?94bCflwNM>cWq5(E^%YR7gZmF=ZO!*HyMA7sN4n04(z a;`uhQ9xA-GQWJ`B{x0N-aG@c2s5a8;bdq`i diff --git a/data/sg_colour_values.rda b/data/sg_colour_values.rda index afa6e3cd6fe81283aa9e37799a8235dcdb43b694..d7aaf9e7a6eabe9084148d8cb0efa58f4be01afc 100644 GIT binary patch literal 246 zcmVT4*^jL0KkKSqu8j&HwGdcj3 zu<`E9Uwa`>31b-5B~NAIdJGJNQUYNNJG&0RAdIeyO|2u{6G^ag1_ft!q^`Rp)-yCr zLQhtatw>lB7?CFG3e4}frT#w@A9#@ZhKU#rt*lijDrXOc!rM1&yf zHeEVRKuBSUH*jcL@8xy=f1Dq6=zT!mhzLEcRVXtvA9;Bbhx9a+Vj?g&b|vpBW-Nw- vsjC#Tp+6W*VTM}5qtnhVZDLSs=2MM Date: Mon, 7 Aug 2023 10:51:36 +0100 Subject: [PATCH 06/27] Add function to produce colour palette table --- DESCRIPTION | 10 ++++++---- NAMESPACE | 1 + R/colour_table.R | 40 ++++++++++++++++++++++++++++++++++++++++ R/sgplot-package.R | 7 +++++++ man/colour_table.Rd | 18 ++++++++++++++++++ man/sgplot-package.Rd | 29 +++++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 R/colour_table.R create mode 100644 R/sgplot-package.R create mode 100644 man/colour_table.Rd create mode 100644 man/sgplot-package.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 7c49cfb..19ad645 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,9 +17,10 @@ RoxygenNote: 7.2.3 Depends: R (>= 2.10) Imports: - ggplot2, - scales, - cli + ggplot2, + scales, + cli, + rlang Suggests: dplyr, knitr, @@ -31,5 +32,6 @@ Suggests: gapminder, stringr, testthat (>= 2.1.0), - plotly + plotly, + gt VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index 1caecf9..c23d5b0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,3 +7,4 @@ export(scale_fill_continuous_sg) export(scale_fill_discrete_sg) export(theme_sg) export(use_sgplot) +importFrom(rlang,.data) diff --git a/R/colour_table.R b/R/colour_table.R new file mode 100644 index 0000000..ece2786 --- /dev/null +++ b/R/colour_table.R @@ -0,0 +1,40 @@ +#' @title Create \code{gt} table of colour palette +#' +#' @param pal Named vector of colour palette; +#' e.g. \code{sgplot::sg_colour_palettes[["main"]]} +#' +#' @return An object of class \code{gt_tbl}. + +colour_table <- function(pal) { + + tibble::enframe(pal) |> + dplyr::mutate(example = "") |> + dplyr::mutate(value = factor( + pal, + ordered = TRUE, + levels = unname(pal) + )) |> + gt::gt() |> + gt::data_color( + columns = .data$value, + target_columns = .data$example, + fn = scales::col_factor( + palette = pal, + domain = NULL + )) |> + gt::cols_width( + name ~ gt::px(110), + value ~ gt::px(100), + example ~ gt::px(150) + ) |> + gt::cols_align( + align = "left", + columns = dplyr::everything() + ) |> + gt::cols_label( + name = "Colour name", + value = "Hex code", + example = "Example of colour" + ) + +} diff --git a/R/sgplot-package.R b/R/sgplot-package.R new file mode 100644 index 0000000..52c0c07 --- /dev/null +++ b/R/sgplot-package.R @@ -0,0 +1,7 @@ +#' @keywords internal +"_PACKAGE" + +## usethis namespace: start +#' @importFrom rlang .data +## usethis namespace: end +NULL diff --git a/man/colour_table.Rd b/man/colour_table.Rd new file mode 100644 index 0000000..2176c99 --- /dev/null +++ b/man/colour_table.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/colour_table.R +\name{colour_table} +\alias{colour_table} +\title{Create \code{gt} table of colour palette} +\usage{ +colour_table(pal) +} +\arguments{ +\item{pal}{Named vector of colour palette; +e.g. \code{sgplot::sg_colour_palettes[["main"]]}} +} +\value{ +An object of class \code{gt_tbl}. +} +\description{ +Create \code{gt} table of colour palette +} diff --git a/man/sgplot-package.Rd b/man/sgplot-package.Rd new file mode 100644 index 0000000..2d93734 --- /dev/null +++ b/man/sgplot-package.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sgplot-package.R +\docType{package} +\name{sgplot-package} +\alias{sgplot} +\alias{sgplot-package} +\title{sgplot: Graphic Styles and Colours for Scottish Government Plots} +\description{ +Standard ggplot theme and colour palettes for use in Scottish Government. +} +\seealso{ +Useful links: +\itemize{ + \item \url{https://github.com/DataScienceScotland/sgplot} + \item \url{https://DataScienceScotland.github.io/sgplot} + \item Report bugs at \url{https://github.com/DataScienceScotland/sgplot/issues} +} + +} +\author{ +\strong{Maintainer}: Alice Byers \email{alice.byers@gov.scot} + +Other contributors: +\itemize{ + \item Scottish Government \email{statistics.enquiries@gov.scot} [copyright holder, funder] +} + +} +\keyword{internal} From 7c1bd635659678eefb6616d06c78d7a02a03dcdc Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Mon, 7 Aug 2023 11:20:18 +0100 Subject: [PATCH 07/27] Test colour_table --- tests/testthat/test-colour_table.R | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/testthat/test-colour_table.R diff --git a/tests/testthat/test-colour_table.R b/tests/testthat/test-colour_table.R new file mode 100644 index 0000000..16b2292 --- /dev/null +++ b/tests/testthat/test-colour_table.R @@ -0,0 +1,49 @@ +cols <- c(blue = "#002d54", teal = "#2b9c93") +tab <- colour_table(cols) + +test_that("Correct object class returned", { + expect_s3_class(tab, "gt_tbl") +}) + +test_that("Correct data used in table", { + expect_identical( + tab$`_data`, + tibble::tibble( + name = names(cols), + value = factor(cols, ordered = TRUE), + example = c("", "") + ) + ) +}) + +test_that("Example column filled with correct colours", { + + fill <- c() + for(i in tab$`_styles`$styles) { + if(names(i) == "cell_fill") fill <- c(fill, i$cell_fill$color) + } + + expect_identical(toupper(unname(cols)), fill) + +}) + +test_that("Column formats correct", { + + cols <- tab$`_boxhead` + + expect_identical( + unlist(cols$column_label), + c("Colour name", "Hex code", "Example of colour") + ) + + expect_identical( + unlist(cols$column_width), + c("110px", "100px", "150px") + ) + + expect_identical( + unlist(cols$column_align), + rep("left", 3) + ) + +}) From 2131fb9c644d0db74c1309d8a536a4ebd9768073 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Mon, 7 Aug 2023 11:30:38 +0100 Subject: [PATCH 08/27] Fix lintr errors --- R/sg_palette.R | 8 ++++---- tests/testthat/test-colour_table.R | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/sg_palette.R b/R/sg_palette.R index dccf8ea..3dc10ef 100644 --- a/R/sg_palette.R +++ b/R/sg_palette.R @@ -39,22 +39,22 @@ sg_palette <- function(palette = "main", ext_palettes <- subset( names(palette_list), - stringr::str_detect(names(palette_list), "^main[5-9]") + stringr::str_detect(names(palette_list), "^main([5-9]|-extended)") ) # Error if more colours requested than exist in palette if (n > n_available) { cli::cli_abort(c( - "x" = paste( + "x" = glue::glue( "There are not enough colours available in the `{palette}`", "palette from `{palette_type}_colour_palettes`", "({n_available} available)." ), - "i" = paste( + "i" = glue::glue( "Accessibility guidance recommends a limit of four", "colours per chart. If more than four colours are", "required, first consider chart redesign. If it is", - "essential to use more than four colours, the {ext_palettes}", + "essential to use more than four colours, the `{ext_palettes}`", "palette{?s} can be used." ) )) diff --git a/tests/testthat/test-colour_table.R b/tests/testthat/test-colour_table.R index 16b2292..3839c1d 100644 --- a/tests/testthat/test-colour_table.R +++ b/tests/testthat/test-colour_table.R @@ -19,8 +19,8 @@ test_that("Correct data used in table", { test_that("Example column filled with correct colours", { fill <- c() - for(i in tab$`_styles`$styles) { - if(names(i) == "cell_fill") fill <- c(fill, i$cell_fill$color) + for (i in tab$`_styles`$styles) { + if (names(i) == "cell_fill") fill <- c(fill, i$cell_fill$color) } expect_identical(toupper(unname(cols)), fill) From 2d5e5c30193786da2253d4fe8086808c25d6d6c8 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Mon, 7 Aug 2023 16:42:39 +0100 Subject: [PATCH 09/27] Change colour name to dark-blue --- R/use_sgplot.R | 2 +- data-raw/sg_colours.R | 10 +++++----- data/sg_colour_palettes.rda | Bin 328 -> 338 bytes data/sg_colour_values.rda | Bin 246 -> 249 bytes man/use_sgplot.Rd | 2 +- vignettes/cookbook/_annotations.Rmd | 2 +- vignettes/cookbook/_chart-types.Rmd | 12 ++++++------ vignettes/cookbook/_customisations.Rmd | 6 +++--- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/R/use_sgplot.R b/R/use_sgplot.R index 75f608e..32057f6 100644 --- a/R/use_sgplot.R +++ b/R/use_sgplot.R @@ -23,7 +23,7 @@ #' @export -use_sgplot <- function(default_colour = sgplot::sg_colour_values["blue"], +use_sgplot <- function(default_colour = sgplot::sg_colour_values["dark-blue"], ...) { # Use sgplot theme ---- diff --git a/data-raw/sg_colours.R b/data-raw/sg_colours.R index 174a99b..ef68d09 100644 --- a/data-raw/sg_colours.R +++ b/data-raw/sg_colours.R @@ -2,7 +2,7 @@ # Source: https://designsystem.gov.scot/styles/colour sg_colour_values <- c( - `blue` = "#002d54", + `dark-blue` = "#002d54", `teal` = "#2b9c93", `purple` = "#6a2063", `orange` = "#e5682a", @@ -16,14 +16,14 @@ sg_colour_values <- c( ) sg_colour_palettes <- list( - `main` = sg_colour_values[c("blue", "teal", "purple", "orange")], + `main` = sg_colour_values[c("dark-blue", "teal", "purple", "orange")], `main-extended` = sg_colour_values[c( - "blue", "teal", "purple", + "dark-blue", "teal", "purple", "orange", "dark-green", "green", "brown", "pink" )], - `sequential` = sg_colour_values[c("blue", "mid-blue", "light-blue")], - `focus` = sg_colour_values[c("blue", "grey")] + `sequential` = sg_colour_values[c("dark-blue", "mid-blue", "light-blue")], + `focus` = sg_colour_values[c("dark-blue", "grey")] ) usethis::use_data(sg_colour_values, sg_colour_palettes, overwrite = TRUE) diff --git a/data/sg_colour_palettes.rda b/data/sg_colour_palettes.rda index 1a4d451b6be604913e79183a5c30a0b0bee3e00f..9f01eccbe85e55a6f25fd5a595ff9746c7ffe0fe 100644 GIT binary patch delta 325 zcmV-L0lNOk0@4BzLRx4!F+o`-Q(2~If6D*?AdwLme*q~9dY-1Iv{Urbn>2_Rnlf!e z^-n+&nus(Inr50bo{b|$$OLH6Nfiw=Jx@>o8UsK84IF`Ch_aFbNyb4!^3L<}Z`45y zMWnJ6#XDxQMDd1b(=$yDt`|j_nje{gz8Ng-Omc&sO#((Yr>94yb}Rw$fsI7nXrMdZ%B5oSP_K%}apLRx4!F+o`-Q(3}rS5N=}9+43ke;8^Bsp@IuPfZiTK+&Ky4K&aa znu(^TsgNTA9)Jvpz$Ow!LqUx|0B8*W8UsfpBVr7qkU%)dr1HbO{M+?1A&4|mg;7r5 ze^aN7GeI*ljw{qZn}z(aym<&u8YxAVS)`jwid0aAu|~*2C!Il( zB$Z4De~C*Zbun}tEDz=La%UV46P4NoR!P-@GTsQ|6e8Df^qELVmm0S5{qc#e zl-%ksXu`QhdV++$tN^|3en^O4g>*F_O$Orvlc*_*v1riXuykD$%H;2<`DYC}V8tzV z4LPSd4hu`(2(M_n>w`l~fOBxO0Yf%SVIWOxAr2zwkRYXR%v3){31BDb;wd#^flwdC N+>uTcBru#6)Bp@@g3|y1 diff --git a/data/sg_colour_values.rda b/data/sg_colour_values.rda index d7aaf9e7a6eabe9084148d8cb0efa58f4be01afc..70d96f0dbdeb143a98b9db5d7ec87005422ffc81 100644 GIT binary patch literal 249 zcmVF4(p#T7x|A7DaKmZ5=U*H4)5J120-=F{iFaX)L5kS*M zjR1Op4FCWGRP@qKl=5k*>M;XAWc2`TAd)I+qiSf-4F*6xL7)SWk_jSn!~OkrNtw_D z&BwbjJK2(+6GkzrTAs3eGKGK$bT4*^jL0KkKSqu8j&HwGdcj3 zu<`E9Uwa`>31b-5B~NAIdJGJNQUYNNJG&0RAdIeyO|2u{6G^ag1_ft!q^`Rp)-yCr zLQhtatw>lB7?CFG3e4}frT#w@A9#@ZhKU#rt*lijD filter(country == "United Kingdom") |> ggplot(aes(x = year, y = lifeExp)) + - geom_line(linewidth = 1, colour = sg_colour_values["blue"]) + + geom_line(linewidth = 1, colour = sg_colour_values["dark-blue"]) + theme_sg() + scale_y_continuous(limits = c(0, 82), breaks = seq(0, 80, 20), @@ -64,7 +64,7 @@ bar_data <- #| fig.alt = "A bar chart using sgplot theme and dark blue colour." ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + - geom_col(fill = sg_colour_values["blue"]) + + geom_col(fill = sg_colour_values["dark-blue"]) + theme_sg() + scale_y_continuous(expand = c(0, 0)) + labs( @@ -82,7 +82,7 @@ A bar chart can sometimes look better with horizontal bars. This can also be a g #| fig.alt = "A horizontal bar chart using sgplot theme and dark blue colour." ggplot(bar_data, aes(x = lifeExp, y = reorder(country, lifeExp))) + - geom_col(fill = sg_colour_values["blue"]) + + geom_col(fill = sg_colour_values["dark-blue"]) + theme_sg(grid = "x", axis = "y") + scale_x_continuous(expand = c(0, 0)) + labs( @@ -169,7 +169,7 @@ gapminder |> ggplot(aes(x = lifeExp)) + geom_histogram(binwidth = 5, colour = "white", - fill = sg_colour_values["blue"]) + + fill = sg_colour_values["dark-blue"]) + theme_sg() + scale_y_continuous(expand = c(0, 0)) + labs( @@ -190,7 +190,7 @@ gapminder |> gapminder |> filter(year == 2007) |> ggplot(aes(x = gdpPercap, y = lifeExp, size = pop)) + - geom_point(colour = sg_colour_values["blue"]) + + geom_point(colour = sg_colour_values["dark-blue"]) + theme_sg(axis = "none", grid = "xy") + scale_x_continuous( labels = function(x) scales::dollar(x, prefix = "£") @@ -305,7 +305,7 @@ p <- "Life Expectancy: ", round(lifeExp, 1) )) |> ggplot(aes(x = reorder(country, -lifeExp), y = lifeExp, text = tooltip)) + - geom_col(fill = sg_colour_values["blue"]) + + geom_col(fill = sg_colour_values["dark-blue"]) + theme_sg(ticks = "x") + theme(text = element_text(family = "")) + scale_y_continuous(expand = c(0, 0)) + diff --git a/vignettes/cookbook/_customisations.Rmd b/vignettes/cookbook/_customisations.Rmd index d506935..9d7c2e2 100644 --- a/vignettes/cookbook/_customisations.Rmd +++ b/vignettes/cookbook/_customisations.Rmd @@ -11,7 +11,7 @@ To control the order of bars in a chart, wrap the variable you want to arrange w bar_data |> ggplot(aes(x = lifeExp, y = reorder(country, lifeExp))) + - geom_col(fill = sg_colour_values["blue"]) + + geom_col(fill = sg_colour_values["dark-blue"]) + theme_sg(axis = "y", grid = "x") ``` @@ -139,7 +139,7 @@ If text is too long, it may be cut off or distort the dimensions of the chart. plot <- ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + - geom_col(fill = sg_colour_values["blue"]) + + geom_col(fill = sg_colour_values["dark-blue"]) + theme_sg() + scale_y_continuous(expand = c(0, 0)) + labs( @@ -181,7 +181,7 @@ If you find you need to adjust theme elements for your chart, this can be done u #| fig.alt = "A bar chart using sgplot theme and dark blue colour, with axis lines and ticks coloured black." ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + - geom_col(fill = sg_colour_values["blue"]) + + geom_col(fill = sg_colour_values["dark-blue"]) + theme_sg(axis = "xy") + theme(axis.line = element_line(colour = "black"), axis.ticks = element_line(colour = "black")) + From adf83337e8f9950875716c986eaa609773a08217 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Mon, 7 Aug 2023 16:57:53 +0100 Subject: [PATCH 10/27] Update colours vignette --- vignettes/colours.Rmd | 207 +++++++++++++++++++++++++----------------- 1 file changed, 126 insertions(+), 81 deletions(-) diff --git a/vignettes/colours.Rmd b/vignettes/colours.Rmd index d86ee6e..46a25a6 100644 --- a/vignettes/colours.Rmd +++ b/vignettes/colours.Rmd @@ -14,122 +14,164 @@ knitr::opts_chunk$set( collapse = TRUE, comment = "#>", message = FALSE, - warning = FALSE + warning = FALSE, + eval = TRUE, + echo = FALSE ) + +table_note <- "Note: the cells in the ‘Example of colour’ column may appear blank to screen reader software. They contain a colour fill, but no data." ``` -```{r colour-tiles, include = FALSE, echo = FALSE} - -library(ggplot2) -library(dplyr) -library(tibble) -library(tidyr) - -dat <- - enframe(sgplot::sg_colour_palettes) %>% - unnest(value) %>% - mutate(name = sprintf("%-10s", name)) %>% - group_by(name) %>% - mutate(col = paste0("col", row_number())) %>% - ungroup() %>% - mutate(name = factor(name, levels = unique(.$name))) %>% - complete(name, col, fill = list(value = NA)) - -col_tiles <- function(colour_data, scale_ratio = 1) { - ggplot(colour_data, aes(x = .data$col, y = .data$name, fill = .data$value)) + - geom_tile(color = "white", - linewidth = 3, - linetype = 1) + - scale_fill_identity() + - scale_y_discrete(limits = rev) + - theme_void() + - theme(axis.text.y = element_text(size = 12, hjust = 0)) + - coord_equal(ratio = scale_ratio) -} - -col_tiles_alt <- function(pal = c("main", "other")) { - - pal <- match.arg(pal) - - pattern <- if (pal == "main") "main" else "[^(main)\\d*]" - - pal_names <- grep(pattern, names(sgplot::sg_colour_palettes), - value = TRUE, perl = TRUE) - - paste( - purrr::map_chr( - pal_names, - ~ paste0( - "The ", .x, " palette contains ", - glue::glue_collapse( - gsub(pattern = "-", replacement = " ", - x = names(sgplot::sg_colour_palettes[[.x]])), - sep = ", ", last = " and "), - ".") - ), - collapse = " " - ) - -} +sgplot uses colour palettes from the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). +These palettes have been developed to... -``` +* meet [accessibility legislation](https://www.w3.org/TR/WCAG21/) applicable to the public sector. what considerations have been made? (e.g. colour blindness, greyscale, white background, contrast) + +* ensure consistency + +sgplot uses the Scottish Government palettes by default, however colour palettes from the [Government Analysis Function colours guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/) are also available to use. + +Both the Scottish Government Design System and the Analysis Function have published guidance for using colour effectively and accessibly in data visualisations. sgplot should be used in conjunction with this guidance. + +* [Scottish Government guidance](https://designsystem.gov.scot/guidance/charts) -sgplot uses colour palettes from the [Government Analysis Function colours guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/). This guidance contains lots of information on best practice for using colour and how to ensure content meets the [accessibility legislation](https://www.w3.org/TR/WCAG21/) applicable to the public sector. sgplot should be used in conjunction with this guidance. +* [Analysis Function guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/) The following are a few key considerations when using colour in plots: -* Only use colour if absolutely necessary -* Limit the number of different colours you use; ideally an absolute maximum of four -* Use colour consistently when producing a series of plots -* Do not rely on colour alone to communicate a message +* Only use colour if absolutely necessary +* Limit the number of different colours you use; ideally an absolute maximum of four +* Use colour palettes in the order presented to ensure adjacent colours have sufficient contrast with each other +* Use colour consistently when producing a series of plots +* Do not rely on colour alone to communicate a message -## Main Palettes +## Scottish Government colour palettes -There are three palettes available for use with categorical data: +### Main palette -```{r main-palettes, echo = FALSE, fig.height = 4, fig.alt = col_tiles_alt("main")} +The `main` palette should be used with categorical data. -dat %>% - filter(grepl("main", name)) %>% - col_tiles() +`r table_note` +```{r} +sgplot:::colour_table(sgplot::sg_colour_palettes$main) ``` -* `main` is the default palette for discrete colour scales. +You should try to use an absolute maximum of four colours in a data visualisation. If you feel you need more than four, think about how you could redesign your chart to avoid this. If you absolutely must use more than four colours, the `main-extended` palette can be used. -* `main2` should be used if only two colours are required as the dark blue and orange colours have the best colour contrast. If only two colours are required and another palette from the `main` family is used, `main2` will be used instead. +`r table_note` -* `main6` is also available to use, however it should be avoided where possible, as it's generally considered that use of four or more colours in a plot is excessive. Plot redesign should be considered where possible and `main6` used as a last resort. +```{r} +sgplot:::colour_table(sgplot::sg_colour_palettes$`main-extended`) +``` + + +### Sequential data palette +The `sequential` colour palette should be used for data where the order has some meaning. + +`r table_note` + +```{r} +sgplot:::colour_table(sgplot::sg_colour_palettes$sequential) +``` -## Other Palettes +As this palette is restricted to tints of one colour, there are issues with colour contrast. +Neither `dark-blue` or `light-blue` have sufficient colour contrast with `mid-blue`. +Follow the [advice for using the sequential palette](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-6) in the Analysis Function colour guidance when using this palette. -There are two other colour palettes available: -```{r other-palettes, echo = FALSE, fig.height = 3.5, fig.alt = col_tiles_alt("other")} +### Focus chart palette -dat %>% - filter(!grepl("main", name)) %>% - col_tiles() +The `focus` palette should be used to highlight specific elements of a plot. +`r table_note` + +```{r} +sgplot:::colour_table(sgplot::sg_colour_palettes$focus) ``` -* `sequential` should be used for data where the order has some meaning. Note that not all colours in this palette have sufficient colour contrast against a white background or with each other. The Analysis Function colour guidance has some specific [advice for using the sequential palette](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-6) which should be considered. +The `grey` in this palette does not meet the required colour contrast with an off-white background (#f8f8f8), therefore data visualisations using this palette should only use a white background (#ffffff). + + +## Analysis Function colour palettes -* `focus` should be used to highlight specific elements of a plot. The light grey colour in this palette does not have sufficient colour contrast against a white background and therefore this palette should only be used when it is essential to communicate your message. The Analysis Function colour guidance has some [advice for using the focus palette](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-7) which should be considered. +Scottish Government colour palettes are used by default in sgplot. If you wish 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). +### Main palette -## A note on choice of colour palettes +The main palette should be used with categorical data. + +`r table_note` + +```{r} +sgplot:::colour_table(sgplot::af_colour_palettes$main) +``` -Although Scottish Government [colour palettes](https://designsystem.gov.scot/styles/colour-customisation/) do exist, they have been developed primarily for web development. The colours do not have sufficient contrast with each other and therefore are not suitable for use in charts. It is for this reason that sgplot uses colour palettes from the [Analysis Function guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/). sgplot palettes may be updated in future when Scottish Government colour palettes for charts are developed. +The `main2` palette should be used if only two colours are required as the dark blue and orange colours have the best colour contrast with each other. +If only two colours are required and another palette from the `main` family is used, `main2` will be used instead. + +`r table_note` + +```{r} +sgplot:::colour_table(sgplot::af_colour_palettes$main2) +``` + +The `main6` palette is available if an extended palette is required. + +`r table_note` + +```{r} +sgplot:::colour_table(sgplot::af_colour_palettes$main6) +``` + + +### Sequential data palette + +The sequential colour palette should be used for data where the order has some meaning. + +`r table_note` + +```{r} +sgplot:::colour_table(sgplot::af_colour_palettes$sequential) +``` + +As this palette is restricted to tints of one colour, there are issues with colour contrast. +Follow the [advice for using the sequential palette](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-6) in the Analysis Function colour guidance when using this palette. + + +### Focus chart palette + +The focus palette should be used to highlight specific elements of a plot. + +`r table_note` + +```{r} +sgplot:::colour_table(sgplot::af_colour_palettes$focus) +``` + +The light grey colour in this palette does not have sufficient colour contrast against a white background and therefore this palette should only be used when it is essential to communicate your message. +Follow the [advice for using the focus palette](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-7) in the Analysis Function colour guidance when using this palette. + +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 + +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). +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). ## Viewing Palettes in R Names and hex codes for available colours and palettes can be viewed by running the following code: -```{r view-colours, eval = FALSE} +```{r view-colours, eval = FALSE, echo = TRUE} # View names and hex codes for all colours sgplot::sg_colour_values @@ -137,5 +179,8 @@ sgplot::sg_colour_values sgplot::sg_colour_palettes # View names and hex codes for `main` colour palette -sgplot::sg_colour_palettes[["main"]] +sgplot::sg_colour_palettes$main + +# For Analysis Function palettes, use the af_ prefix +sgplot::af_colour_palettes ``` From ca555f9dd65d3a8bf1c248272d09cc1acde88911 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Tue, 8 Aug 2023 16:08:59 +0100 Subject: [PATCH 11/27] Finish redraft of colours vignette --- vignettes/colours.Rmd | 49 ++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/vignettes/colours.Rmd b/vignettes/colours.Rmd index 46a25a6..797762b 100644 --- a/vignettes/colours.Rmd +++ b/vignettes/colours.Rmd @@ -22,32 +22,34 @@ knitr::opts_chunk$set( table_note <- "Note: the cells in the ‘Example of colour’ column may appear blank to screen reader software. They contain a colour fill, but no data." ``` -sgplot uses colour palettes from the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). -These palettes have been developed to... +sgplot provides colour palettes as set out by the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). -* meet [accessibility legislation](https://www.w3.org/TR/WCAG21/) applicable to the public sector. what considerations have been made? (e.g. colour blindness, greyscale, white background, contrast) +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 colours in the [main palette for categorical data](#main-palette) all have at least a 3:1 colour contrast ratio with white (#ffffff) and off-white (#f8f8f8) backgrounds, as well as with adjacent colours in the palette. -* ensure consistency -sgplot uses the Scottish Government palettes by default, however colour palettes from the [Government Analysis Function colours guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/) are also available to use. +## Things to consider when using colour -Both the Scottish Government Design System and the Analysis Function have published guidance for using colour effectively and accessibly in data visualisations. sgplot should be used in conjunction with this guidance. +Using sgplot colour palettes does not guarantee that your charts will be accessible. +Both the [Scottish Government ](https://designsystem.gov.scot/guidance/charts) and the [Analysis Function](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/) have published guidance on other steps you should take to ensure the accessible use of colour. +sgplot should be used in conjunction with the advice given in these guidance documents. -* [Scottish Government guidance](https://designsystem.gov.scot/guidance/charts) +The following are a few key things to consider when using colour in data visualisations: -* [Analysis Function guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/) +* Only use colour if absolutely necessary -The following are a few key considerations when using colour in plots: +* Do not rely on colour alone to communicate a message + +* Use colour consistently when producing a series of plots -* Only use colour if absolutely necessary * Limit the number of different colours you use; ideally an absolute maximum of four + * Use colour palettes in the order presented to ensure adjacent colours have sufficient contrast with each other -* Use colour consistently when producing a series of plots -* Do not rely on colour alone to communicate a message ## Scottish Government colour palettes +The Scottish Government colour palettes are used by default in sgplot. + ### Main palette The `main` palette should be used with categorical data. @@ -58,7 +60,7 @@ The `main` palette should be used with categorical data. sgplot:::colour_table(sgplot::sg_colour_palettes$main) ``` -You should try to use an absolute maximum of four colours in a data visualisation. If you feel you need more than four, think about how you could redesign your chart to avoid this. If you absolutely must use more than four colours, the `main-extended` palette can be used. +You should try to use an absolute maximum of four colours in a chart. If you feel you need more than four, think about how you could redesign your chart to avoid this. If you absolutely must use more than four colours, the `main-extended` palette can be used. `r table_note` @@ -67,7 +69,7 @@ sgplot:::colour_table(sgplot::sg_colour_palettes$`main-extended`) ``` -### Sequential data palette +### Sequential palette The `sequential` colour palette should be used for data where the order has some meaning. @@ -82,7 +84,7 @@ Neither `dark-blue` or `light-blue` have sufficient colour contrast with `mid-bl Follow the [advice for using the sequential palette](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-6) in the Analysis Function colour guidance when using this palette. -### Focus chart palette +### Focus palette The `focus` palette should be used to highlight specific elements of a plot. @@ -92,16 +94,17 @@ The `focus` palette should be used to highlight specific elements of a plot. sgplot:::colour_table(sgplot::sg_colour_palettes$focus) ``` -The `grey` in this palette does not meet the required colour contrast with an off-white background (#f8f8f8), therefore data visualisations using this palette should only use a white background (#ffffff). +The `grey` in this palette does not meet the required colour contrast with an off-white background (#f8f8f8), therefore charts using this palette should only use a white background (#ffffff). ## Analysis Function colour palettes -Scottish Government colour palettes are used by default in sgplot. If you wish 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 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). + ### Main palette -The main palette should be used with categorical data. +The `main` palette should be used with categorical data. `r table_note` @@ -127,9 +130,9 @@ sgplot:::colour_table(sgplot::af_colour_palettes$main6) ``` -### Sequential data palette +### Sequential palette -The sequential colour palette should be used for data where the order has some meaning. +The `sequential` colour palette should be used for data where the order has some meaning. `r table_note` @@ -141,9 +144,9 @@ As this palette is restricted to tints of one colour, there are issues with colo Follow the [advice for using the sequential palette](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-6) in the Analysis Function colour guidance when using this palette. -### Focus chart palette +### Focus palette -The focus palette should be used to highlight specific elements of a plot. +The `focus` palette should be used to highlight specific elements of a plot. `r table_note` @@ -184,3 +187,5 @@ sgplot::sg_colour_palettes$main # For Analysis Function palettes, use the af_ prefix 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. From e40472c5b5dd0bb927f2227ee4075866992351fa Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Wed, 9 Aug 2023 09:51:12 +0100 Subject: [PATCH 12/27] Reduce base_line_size --- R/theme_sg.R | 4 ++-- man/theme_sg.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/theme_sg.R b/R/theme_sg.R index b91f551..1f54da9 100644 --- a/R/theme_sg.R +++ b/R/theme_sg.R @@ -23,8 +23,8 @@ theme_sg <- function(base_size = 12, - base_line_size = base_size / 22, - base_rect_size = base_size / 22, + base_line_size = base_size / 24, + base_rect_size = base_size / 24, grid = c("y", "x", "xy", "none"), axis = c("x", "y", "xy", "none"), ticks = c("xy", "x", "y", "none"), diff --git a/man/theme_sg.Rd b/man/theme_sg.Rd index df7fc79..80334e3 100644 --- a/man/theme_sg.Rd +++ b/man/theme_sg.Rd @@ -6,8 +6,8 @@ \usage{ theme_sg( base_size = 12, - base_line_size = base_size/22, - base_rect_size = base_size/22, + base_line_size = base_size/24, + base_rect_size = base_size/24, grid = c("y", "x", "xy", "none"), axis = c("x", "y", "xy", "none"), ticks = c("xy", "x", "y", "none"), From 8009cac51ee6da4b6771ddaf39acf821f01c1e67 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Wed, 9 Aug 2023 12:17:21 +0100 Subject: [PATCH 13/27] Update refs to analysis function colours --- README.Rmd | 3 +- README.md | 13 +- man/figures/README-ex1-1.svg | 546 ++++++++++++------------ man/figures/README-ex2-1.svg | 524 +++++++++++------------ vignettes/accessibility.Rmd | 8 +- vignettes/cookbook/_colour-palettes.Rmd | 13 +- 6 files changed, 559 insertions(+), 548 deletions(-) diff --git a/README.Rmd b/README.Rmd index abcb627..3eec2a6 100644 --- a/README.Rmd +++ b/README.Rmd @@ -34,7 +34,8 @@ ex2_alt <- paste("A bar chart with white background,", sgplot is an R package for creating accessible plots in the Scottish Government. Currently, functions are available for styling ggplot2 plots. -The package has been developed using the [Government Analysis Function](https://analysisfunction.civilservice.gov.uk/) Data Visualisation guidance for [charts](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/) and [colours](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/). sgplot should be used in conjunction with these guidance documents. +The package has been developed using the [Government Analysis Function Data Visualisation guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/) and uses accessible colour palettes from the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). +sgplot should be used in conjunction with these guidance documents. More information about the package and its functions can be found on the [sgplot website](https://DataScienceScotland.github.io/sgplot). In particular, the [cookbook](https://DataScienceScotland.github.io/sgplot/articles/cookbook.html) contains lots of examples. diff --git a/README.md b/README.md index 29b7c6a..239fc66 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,12 @@ sgplot is an R package for creating accessible plots in the Scottish Government. Currently, functions are available for styling ggplot2 plots. -The package has been developed using the [Government Analysis -Function](https://analysisfunction.civilservice.gov.uk/) Data -Visualisation guidance for -[charts](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/) -and -[colours](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/). -sgplot should be used in conjunction with these guidance documents. +The package has been developed using the [Government Analysis Function +Data Visualisation +guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/) +and uses accessible colour palettes from the [Scottish Government Design +System](https://designsystem.gov.scot/guidance/charts). sgplot should be +used in conjunction with these guidance documents. More information about the package and its functions can be found on the [sgplot website](https://DataScienceScotland.github.io/sgplot). In diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index c0d844f..cd2b694 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -3,532 +3,532 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index 3c656f3..ea1c6c0 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -3,500 +3,500 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/vignettes/accessibility.Rmd b/vignettes/accessibility.Rmd index dc04e97..471d608 100644 --- a/vignettes/accessibility.Rmd +++ b/vignettes/accessibility.Rmd @@ -23,7 +23,7 @@ Accessibility legislation means that all content published on public sector webs ## Resources -The [Government Analysis Function](https://analysisfunction.civilservice.gov.uk/) has published a variety of resources for producing data visualisations: +The [Government Analysis Function](https://analysisfunction.civilservice.gov.uk/) has published a variety of resources for producing accessible data visualisations: * [Charts Checklist](https://analysisfunction.civilservice.gov.uk/policy-store/charts-a-checklist/) * [Charts Guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/) @@ -49,7 +49,8 @@ By applying `theme_sg` to ggplot2 plots, the following best practice is followed * Line thickness is relative to font size. -Colour palettes from the Analysis Function [colours guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/) have been used in sgplot. The colours in these palettes all have sufficient colour contrast against a white background and take into account the general considerations for colour set out in accessibility legislation. These palettes can be applied to charts by using one of the four colour/fill functions: +sgplot provides colour palettes as set out by the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). +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) and can be applied to charts by using one of the four colour/fill functions: * `scale_colour_discrete_sg()` * `scale_fill_discrete_sg()` @@ -61,7 +62,8 @@ More information on sgplot colours can be found at `vignette("colours")`. ## Other accessibility considerations -Using sgplot does not guarantee accessibility. The Analysis Function [chart guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts) contains detailed information on other considerations that should be made. Not all of these are repeated here, but we think the following are a good place to start: +Using sgplot does not guarantee accessibility. The Analysis Function [chart guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts) contains detailed information on other considerations that should be made. +Not all of these are repeated here, but we think the following are a good place to start: * Consider what type of chart is best to convey your message. Keep it as simple as possible. diff --git a/vignettes/cookbook/_colour-palettes.Rmd b/vignettes/cookbook/_colour-palettes.Rmd index 3c72e2f..343e6af 100644 --- a/vignettes/cookbook/_colour-palettes.Rmd +++ b/vignettes/cookbook/_colour-palettes.Rmd @@ -1,8 +1,17 @@ ## Using a different colour palette -Colour palettes from the Analysis Function [colours guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/) have been used in sgplot. The colours in these palettes all have sufficient colour contrast against a white background and take into account the general considerations for colour set out in accessibility legislation. More information on the colours used in sgplot can be found in the [Colours article](https://datasciencescotland.github.io/sgplot/articles/colours.html). +sgplot provides colour palettes as set out by the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). +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). -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 previously mentioned 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. +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). + +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. +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. ```{r different-colour-palette-1} #| fig.alt = "A line chart using sgplot theme and first colour from custom palette." From 5276d98547f1e2070950115fdc03e0737a8f8929 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Wed, 9 Aug 2023 12:39:55 +0100 Subject: [PATCH 14/27] Update roxygen docs --- R/scale_colour_continuous_sg.R | 8 ++++---- R/scale_colour_discrete_sg.R | 8 ++++---- R/scale_fill_continuous_sg.R | 8 ++++---- R/scale_fill_discrete_sg.R | 8 ++++---- man/scale_colour_continuous_sg.Rd | 8 ++++---- man/scale_colour_discrete_sg.Rd | 8 ++++---- man/scale_fill_continuous_sg.Rd | 8 ++++---- man/scale_fill_discrete_sg.Rd | 8 ++++---- 8 files changed, 32 insertions(+), 32 deletions(-) diff --git a/R/scale_colour_continuous_sg.R b/R/scale_colour_continuous_sg.R index f3dd7b0..468f854 100644 --- a/R/scale_colour_continuous_sg.R +++ b/R/scale_colour_continuous_sg.R @@ -1,9 +1,9 @@ #' @title Continuous colour scales for Scottish Government plots #' -#' @param palette Name of palette to use from `sg_colour_palettes`; e.g. main, -#' sequential, focus. Default value is 'sequential'. -#' @param palette_type Either `sg` to use Scottish Government palettes, or `af` -#' to use Analysis Function palettes. Defaults to `sg`. +#' @param palette Name of palette to use; e.g. "main", "sequential", "focus". +#' Default value is "sequential". +#' @param palette_type Either "sg" to use Scottish Government palettes, or "af" +#' to use Analysis Function palettes. Defaults to "sg". #' @param reverse Boolean value to indicate whether the palette should be #' reversed. #' @param na_colour Colour to set for missing values. diff --git a/R/scale_colour_discrete_sg.R b/R/scale_colour_discrete_sg.R index 0ad4edb..1e04f4f 100644 --- a/R/scale_colour_discrete_sg.R +++ b/R/scale_colour_discrete_sg.R @@ -1,9 +1,9 @@ #' @title Discrete colour scales for Scottish Government plots #' -#' @param palette Name of palette to use from `sg_colour_palettes`; e.g. main, -#' sequential, focus. Default value is 'main'. -#' @param palette_type Either `sg` to use Scottish Government palettes, or `af` -#' to use Analysis Function palettes. Defaults to `sg`. +#' @param palette Name of palette to use; e.g. "main", "sequential", "focus." +#' Default value is "main". +#' @param palette_type Either "sg" to use Scottish Government palettes, or "af" +#' to use Analysis Function palettes. Defaults to "sg". #' @param reverse Boolean value to indicate whether the palette should be #' reversed. #' @param ... Additional arguments passed to scale type. diff --git a/R/scale_fill_continuous_sg.R b/R/scale_fill_continuous_sg.R index 1c9a123..cb6bf28 100644 --- a/R/scale_fill_continuous_sg.R +++ b/R/scale_fill_continuous_sg.R @@ -1,9 +1,9 @@ #' @title Continuous colour fill scales for Scottish Government plots #' -#' @param palette Name of palette to use from `sg_colour_palettes`; e.g. main, -#' sequential, focus. Default value is 'sequential'. -#' @param palette_type Either `sg` to use Scottish Government palettes, or `af` -#' to use Analysis Function palettes. Defaults to `sg`. +#' @param palette Name of palette to use; e.g. "main", "sequential", "focus." +#' Default value is "sequential". +#' @param palette_type Either "sg" to use Scottish Government palettes, or "af" +#' to use Analysis Function palettes. Defaults to "sg". #' @param reverse Boolean value to indicate whether the palette should be #' reversed. #' @param na_colour Colour to set for missing values. diff --git a/R/scale_fill_discrete_sg.R b/R/scale_fill_discrete_sg.R index f95b043..4cd7d37 100644 --- a/R/scale_fill_discrete_sg.R +++ b/R/scale_fill_discrete_sg.R @@ -1,9 +1,9 @@ #' @title Discrete colour fill scales for Scottish Government plots #' -#' @param palette Name of palette to use from `sg_colour_palettes`; e.g. main, -#' sequential, focus. Default value is 'main'. -#' @param palette_type Either `sg` to use Scottish Government palettes, or `af` -#' to use Analysis Function palettes. Defaults to `sg`. +#' @param palette Name of palette to use; e.g. "main", "sequential", "focus." +#' Default value is "main." +#' @param palette_type Either "sg" to use Scottish Government palettes, or "af" +#' to use Analysis Function palettes. Defaults to "sg". #' @param reverse Boolean value to indicate whether the palette should be #' reversed. #' @param ... Additional arguments passed to scale type. diff --git a/man/scale_colour_continuous_sg.Rd b/man/scale_colour_continuous_sg.Rd index e5ff381..d266b12 100644 --- a/man/scale_colour_continuous_sg.Rd +++ b/man/scale_colour_continuous_sg.Rd @@ -14,11 +14,11 @@ scale_colour_continuous_sg( ) } \arguments{ -\item{palette}{Name of palette to use from `sg_colour_palettes`; e.g. main, -sequential, focus. Default value is 'sequential'.} +\item{palette}{Name of palette to use; e.g. "main", "sequential", "focus". +Default value is "sequential".} -\item{palette_type}{Either `sg` to use Scottish Government palettes, or `af` -to use Analysis Function palettes. Defaults to `sg`.} +\item{palette_type}{Either "sg" to use Scottish Government palettes, or "af" +to use Analysis Function palettes. Defaults to "sg".} \item{reverse}{Boolean value to indicate whether the palette should be reversed.} diff --git a/man/scale_colour_discrete_sg.Rd b/man/scale_colour_discrete_sg.Rd index ff30150..6e45d85 100644 --- a/man/scale_colour_discrete_sg.Rd +++ b/man/scale_colour_discrete_sg.Rd @@ -12,11 +12,11 @@ scale_colour_discrete_sg( ) } \arguments{ -\item{palette}{Name of palette to use from `sg_colour_palettes`; e.g. main, -sequential, focus. Default value is 'main'.} +\item{palette}{Name of palette to use; e.g. "main", "sequential", "focus." +Default value is "main".} -\item{palette_type}{Either `sg` to use Scottish Government palettes, or `af` -to use Analysis Function palettes. Defaults to `sg`.} +\item{palette_type}{Either "sg" to use Scottish Government palettes, or "af" +to use Analysis Function palettes. Defaults to "sg".} \item{reverse}{Boolean value to indicate whether the palette should be reversed.} diff --git a/man/scale_fill_continuous_sg.Rd b/man/scale_fill_continuous_sg.Rd index 5296d03..77b2389 100644 --- a/man/scale_fill_continuous_sg.Rd +++ b/man/scale_fill_continuous_sg.Rd @@ -14,11 +14,11 @@ scale_fill_continuous_sg( ) } \arguments{ -\item{palette}{Name of palette to use from `sg_colour_palettes`; e.g. main, -sequential, focus. Default value is 'sequential'.} +\item{palette}{Name of palette to use; e.g. "main", "sequential", "focus." +Default value is "sequential".} -\item{palette_type}{Either `sg` to use Scottish Government palettes, or `af` -to use Analysis Function palettes. Defaults to `sg`.} +\item{palette_type}{Either "sg" to use Scottish Government palettes, or "af" +to use Analysis Function palettes. Defaults to "sg".} \item{reverse}{Boolean value to indicate whether the palette should be reversed.} diff --git a/man/scale_fill_discrete_sg.Rd b/man/scale_fill_discrete_sg.Rd index 7369c14..8010194 100644 --- a/man/scale_fill_discrete_sg.Rd +++ b/man/scale_fill_discrete_sg.Rd @@ -12,11 +12,11 @@ scale_fill_discrete_sg( ) } \arguments{ -\item{palette}{Name of palette to use from `sg_colour_palettes`; e.g. main, -sequential, focus. Default value is 'main'.} +\item{palette}{Name of palette to use; e.g. "main", "sequential", "focus." +Default value is "main."} -\item{palette_type}{Either `sg` to use Scottish Government palettes, or `af` -to use Analysis Function palettes. Defaults to `sg`.} +\item{palette_type}{Either "sg" to use Scottish Government palettes, or "af" +to use Analysis Function palettes. Defaults to "sg".} \item{reverse}{Boolean value to indicate whether the palette should be reversed.} From f1bf3b4db7a2c3f0d928fb98a4af411f52461e7a Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Wed, 9 Aug 2023 12:40:23 +0100 Subject: [PATCH 15/27] Update reference contents --- _pkgdown.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/_pkgdown.yml b/_pkgdown.yml index c355037..d579f47 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -62,7 +62,8 @@ reference: - title: Colours contents: - starts_with("scale") - - matches("colour") + - starts_with("sg_colour") + - starts_with("af_colour") - title: Helpers contents: @@ -71,6 +72,7 @@ reference: - title: internal contents: - sg_palette + - colour_table figures: dev: "svg" From aa3d9ba6b53c8a839b40b8daa407386f204e4842 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Wed, 9 Aug 2023 12:43:35 +0100 Subject: [PATCH 16/27] Fix format of string --- vignettes/colours.Rmd | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vignettes/colours.Rmd b/vignettes/colours.Rmd index 797762b..02a186c 100644 --- a/vignettes/colours.Rmd +++ b/vignettes/colours.Rmd @@ -19,7 +19,10 @@ knitr::opts_chunk$set( echo = FALSE ) -table_note <- "Note: the cells in the ‘Example of colour’ column may appear blank to screen reader software. They contain a colour fill, but no data." +table_note <- paste( + "Note: the cells in the ‘Example of colour’ column may appear blank", + "to screen reader software. They contain a colour fill, but no data." +) ``` sgplot provides colour palettes as set out by the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). From e3d5f0c008dc86d64fd54059c0ba196633f4b6ed Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Wed, 9 Aug 2023 12:45:15 +0100 Subject: [PATCH 17/27] Update package version and news --- DESCRIPTION | 2 +- NEWS.md | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 19ad645..4ec3522 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: sgplot Title: Graphic Styles and Colours for Scottish Government Plots -Version: 0.1.0 +Version: 0.2.0 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 1d43d76..e47c567 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# sgplot 0.2.0 + +* Add [Scottish Government Design System colour palettes](https://designsystem.gov.scot/guidance/charts) +* Reduce `base_line_size` in `theme_sg()` + # sgplot 0.1.0 * First package release From ce4a2ead700cdb6b4d8baafd9205c34ee5b84c9e Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Wed, 9 Aug 2023 11:47:39 +0000 Subject: [PATCH 18/27] Re-build README.md --- man/figures/README-ex1-1.svg | 546 +++++++++++++++++------------------ man/figures/README-ex2-1.svg | 526 ++++++++++++++++----------------- 2 files changed, 536 insertions(+), 536 deletions(-) diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index cd2b694..c0d844f 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -3,532 +3,532 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index ea1c6c0..a50bdd7 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -3,500 +3,500 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + From 45b5f273ccc015103b9d048f3832985232eaa581 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Wed, 9 Aug 2023 13:40:14 +0100 Subject: [PATCH 19/27] Add link to slides (closes #21) --- README.Rmd | 4 +- README.md | 4 +- man/figures/README-ex1-1.svg | 546 +++++++++++++++++------------------ man/figures/README-ex2-1.svg | 526 ++++++++++++++++----------------- 4 files changed, 542 insertions(+), 538 deletions(-) diff --git a/README.Rmd b/README.Rmd index 3eec2a6..e674d22 100644 --- a/README.Rmd +++ b/README.Rmd @@ -37,7 +37,9 @@ sgplot is an R package for creating accessible plots in the Scottish Government. The package has been developed using the [Government Analysis Function Data Visualisation guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/) and uses accessible colour palettes from the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). sgplot should be used in conjunction with these guidance documents. -More information about the package and its functions can be found on the [sgplot website](https://DataScienceScotland.github.io/sgplot). In particular, the [cookbook](https://DataScienceScotland.github.io/sgplot/articles/cookbook.html) contains lots of examples. +More information about the package and its functions can be found on the [sgplot website](https://DataScienceScotland.github.io/sgplot). +In particular, the [cookbook](https://DataScienceScotland.github.io/sgplot/articles/cookbook.html) contains lots of examples. +Slides are also available for an internal [presentation given to statisticians and analysts](https://alicebyers5.github.io/presentations-and-blogs/2023-06-19_sgplot/slides.html). ## Installation diff --git a/README.md b/README.md index 239fc66..ff0de62 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,9 @@ More information about the package and its functions can be found on the [sgplot website](https://DataScienceScotland.github.io/sgplot). In particular, the [cookbook](https://DataScienceScotland.github.io/sgplot/articles/cookbook.html) -contains lots of examples. +contains lots of examples. Slides are also available for an internal +[presentation given to statisticians and +analysts](https://alicebyers5.github.io/presentations-and-blogs/2023-06-19_sgplot/slides.html). ## Installation diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index c0d844f..cd2b694 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -3,532 +3,532 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index a50bdd7..ea1c6c0 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -3,500 +3,500 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + From 309336f8adc8c8527337e2d1e633fbed74e52b58 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Fri, 18 Aug 2023 09:38:59 +0100 Subject: [PATCH 20/27] Remove colour default for bar and col (avoid dark blue outline when filled) (closes #23) --- R/use_sgplot.R | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/R/use_sgplot.R b/R/use_sgplot.R index 32057f6..2e0d10c 100644 --- a/R/use_sgplot.R +++ b/R/use_sgplot.R @@ -79,15 +79,13 @@ use_sgplot <- function(default_colour = sgplot::sg_colour_values["dark-blue"], # Col ggplot2::update_geom_defaults( geom = "col", - new = list(colour = default_colour, - fill = default_colour) + new = list(fill = default_colour) ) # Bar ggplot2::update_geom_defaults( geom = "bar", - new = list(colour = default_colour, - fill = default_colour) + new = list(fill = default_colour) ) # Text From 4f90debe9675801aa46af7b25f40d6012b47124d Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Wed, 9 Aug 2023 12:42:35 +0000 Subject: [PATCH 21/27] Re-build README.md --- man/figures/README-ex1-1.svg | 546 +++++++++++++++++------------------ man/figures/README-ex2-1.svg | 526 ++++++++++++++++----------------- 2 files changed, 536 insertions(+), 536 deletions(-) diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg index cd2b694..c0d844f 100644 --- a/man/figures/README-ex1-1.svg +++ b/man/figures/README-ex1-1.svg @@ -3,532 +3,532 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index ea1c6c0..a50bdd7 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -3,500 +3,500 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + From 04a63b51853481806b1c07ddf34a2483995d677e Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Fri, 18 Aug 2023 09:45:13 +0100 Subject: [PATCH 22/27] Update news --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index e47c567..aee74f5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ * Add [Scottish Government Design System colour palettes](https://designsystem.gov.scot/guidance/charts) * 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 From b7d98ea0ed2812e165b123990c5744c08d3ca4d6 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Fri, 25 Aug 2023 12:34:22 +0100 Subject: [PATCH 23/27] Update link to SG colour palette guidance --- NEWS.md | 2 +- R/data.R | 4 ++-- README.Rmd | 2 +- data-raw/sg_colours.R | 3 ++- man/sg_colour_palettes.Rd | 2 +- man/sg_colour_values.Rd | 2 +- vignettes/accessibility.Rmd | 2 +- vignettes/colours.Rmd | 4 ++-- vignettes/cookbook/_colour-palettes.Rmd | 2 +- 9 files changed, 12 insertions(+), 11 deletions(-) diff --git a/NEWS.md b/NEWS.md index aee74f5..5c31126 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # sgplot 0.2.0 -* Add [Scottish Government Design System colour palettes](https://designsystem.gov.scot/guidance/charts) +* 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()`. diff --git a/R/data.R b/R/data.R index 5f9bbf1..9046908 100644 --- a/R/data.R +++ b/R/data.R @@ -5,7 +5,7 @@ #' hex code. #' #' @format A character vector -#' @source \href{https://designsystem.gov.scot/styles/colour}{Scottish Government Design System} +#' @source \href{https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes}{Scottish Government Design System} # nolint end "sg_colour_values" @@ -17,7 +17,7 @@ #' @description A list grouping colours into palettes. #' #' @format A character list -#' @source \href{https://designsystem.gov.scot/styles/colour}{Scottish Government Design System} +#' @source \href{https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes}{Scottish Government Design System} # nolint end "sg_colour_palettes" diff --git a/README.Rmd b/README.Rmd index e674d22..1829421 100644 --- a/README.Rmd +++ b/README.Rmd @@ -34,7 +34,7 @@ ex2_alt <- paste("A bar chart with white background,", sgplot is an R package for creating accessible plots in the Scottish Government. Currently, functions are available for styling ggplot2 plots. -The package has been developed using the [Government Analysis Function Data Visualisation guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/) and uses accessible colour palettes from the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). +The package has been developed using the [Government Analysis Function Data Visualisation guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/) and uses accessible colour palettes from the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes). sgplot should be used in conjunction with these guidance documents. More information about the package and its functions can be found on the [sgplot website](https://DataScienceScotland.github.io/sgplot). diff --git a/data-raw/sg_colours.R b/data-raw/sg_colours.R index ef68d09..22aa222 100644 --- a/data-raw/sg_colours.R +++ b/data-raw/sg_colours.R @@ -1,5 +1,6 @@ # Scottish Government (SG) Design System colours and palettes ---- -# Source: https://designsystem.gov.scot/styles/colour +# Source: https://designsystem.gov.scot/guidance/ +# charts/data-visualisation-colour-palettes sg_colour_values <- c( `dark-blue` = "#002d54", diff --git a/man/sg_colour_palettes.Rd b/man/sg_colour_palettes.Rd index d2dbd34..4b2295b 100644 --- a/man/sg_colour_palettes.Rd +++ b/man/sg_colour_palettes.Rd @@ -8,7 +8,7 @@ A character list } \source{ -\href{https://designsystem.gov.scot/styles/colour}{Scottish Government Design System} +\href{https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes}{Scottish Government Design System} } \usage{ sg_colour_palettes diff --git a/man/sg_colour_values.Rd b/man/sg_colour_values.Rd index 2da6f93..ac8993c 100644 --- a/man/sg_colour_values.Rd +++ b/man/sg_colour_values.Rd @@ -8,7 +8,7 @@ A character vector } \source{ -\href{https://designsystem.gov.scot/styles/colour}{Scottish Government Design System} +\href{https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes}{Scottish Government Design System} } \usage{ sg_colour_values diff --git a/vignettes/accessibility.Rmd b/vignettes/accessibility.Rmd index 471d608..4985cc0 100644 --- a/vignettes/accessibility.Rmd +++ b/vignettes/accessibility.Rmd @@ -49,7 +49,7 @@ By applying `theme_sg` to ggplot2 plots, the following best practice is followed * Line thickness is relative to font size. -sgplot provides colour palettes as set out by the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). +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) and can be applied to charts by using one of the four colour/fill functions: * `scale_colour_discrete_sg()` diff --git a/vignettes/colours.Rmd b/vignettes/colours.Rmd index 02a186c..d53e40c 100644 --- a/vignettes/colours.Rmd +++ b/vignettes/colours.Rmd @@ -25,7 +25,7 @@ table_note <- paste( ) ``` -sgplot provides colour palettes as set out by the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). +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 colours in the [main palette for categorical data](#main-palette) all have at least a 3:1 colour contrast ratio with white (#ffffff) and off-white (#f8f8f8) backgrounds, as well as with adjacent colours in the palette. @@ -33,7 +33,7 @@ These palettes have been developed to meet the [Web Content Accessibility Guidel ## Things to consider when using colour Using sgplot colour palettes does not guarantee that your charts will be accessible. -Both the [Scottish Government ](https://designsystem.gov.scot/guidance/charts) and the [Analysis Function](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/) have published guidance on other steps you should take to ensure the accessible use of colour. +Both the [Scottish Government ](https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes) and the [Analysis Function](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/) have published guidance on other steps you should take to ensure the accessible use of colour. sgplot should be used in conjunction with the advice given in these guidance documents. The following are a few key things to consider when using colour in data visualisations: diff --git a/vignettes/cookbook/_colour-palettes.Rmd b/vignettes/cookbook/_colour-palettes.Rmd index 343e6af..4ab5ef3 100644 --- a/vignettes/cookbook/_colour-palettes.Rmd +++ b/vignettes/cookbook/_colour-palettes.Rmd @@ -1,6 +1,6 @@ ## Using a different colour palette -sgplot provides colour palettes as set out by the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts). +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). From c6da3197e117269a26f21badce6ac52fdebfb7e9 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Fri, 25 Aug 2023 12:53:35 +0100 Subject: [PATCH 24/27] Fix lintr warnings --- R/colour_table.R | 3 ++- R/sg_palette.R | 10 +++++++--- vignettes/cookbook/_chart-types.Rmd | 4 ++-- vignettes/cookbook/_customisations.Rmd | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/R/colour_table.R b/R/colour_table.R index ece2786..5d27b4a 100644 --- a/R/colour_table.R +++ b/R/colour_table.R @@ -21,7 +21,8 @@ colour_table <- function(pal) { fn = scales::col_factor( palette = pal, domain = NULL - )) |> + ) + ) |> gt::cols_width( name ~ gt::px(110), value ~ gt::px(100), diff --git a/R/sg_palette.R b/R/sg_palette.R index 3dc10ef..ecdf3b4 100644 --- a/R/sg_palette.R +++ b/R/sg_palette.R @@ -21,7 +21,7 @@ sg_palette <- function(palette = "main", if (!palette %in% names(palette_list)) { cli::cli_abort(c( "x" = paste("`{palette}` is not a valid palette name ", - "in `{palette_type}_colour_palettes`.") + "in `{palette_type}_colour_palettes`.") )) } @@ -29,8 +29,12 @@ sg_palette <- function(palette = "main", n_available <- length(palette_list[[palette]]) # Use 'main2' if AF main palette used and only 2 colours required - if (palette_type == "af" && - n == 2 && palette != "main2" && grepl("main", palette)) { + if ( + palette_type == "af" && + n == 2 && + palette != "main2" && + grepl("main", palette) + ) { palette <- "main2" cli::cli_warn(c( "!" = "Using `main2` as only two colours are required." diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd index 2228aa2..3c60b75 100644 --- a/vignettes/cookbook/_chart-types.Rmd +++ b/vignettes/cookbook/_chart-types.Rmd @@ -136,8 +136,8 @@ stacked_bar_data <- gapminder |> filter(year == 2007) |> mutate(lifeExpGrouped = cut(lifeExp, - breaks = c(0, 75, Inf), - labels = c("Under 75", "75+"))) |> + breaks = c(0, 75, Inf), + labels = c("Under 75", "75+"))) |> group_by(continent, lifeExpGrouped) |> summarise(n_countries = n(), .groups = "drop") diff --git a/vignettes/cookbook/_customisations.Rmd b/vignettes/cookbook/_customisations.Rmd index 9d7c2e2..0943fa5 100644 --- a/vignettes/cookbook/_customisations.Rmd +++ b/vignettes/cookbook/_customisations.Rmd @@ -152,7 +152,7 @@ plot + labs( y = "Percentage of countries", title = paste("Iceland has the highest life expectancy in Europe", - "followed closely by Switzerland") + "followed closely by Switzerland") ) ``` From 4ade0a77f50cabf98cf1b0c9862406d57f746506 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Fri, 18 Aug 2023 09:05:23 +0000 Subject: [PATCH 25/27] Re-build README.md --- man/figures/README-ex2-1.svg | 42 ++++++------------------------------ 1 file changed, 6 insertions(+), 36 deletions(-) diff --git a/man/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg index a50bdd7..0f77f28 100644 --- a/man/figures/README-ex2-1.svg +++ b/man/figures/README-ex2-1.svg @@ -232,32 +232,17 @@ - - - - - - - + - - - - + - - - - + - - - @@ -282,32 +267,17 @@ - - - - - - - + - - - - + - - - - + - - - From 885bea89402587b8598023f177003a47e821e22d Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Fri, 25 Aug 2023 12:02:55 +0000 Subject: [PATCH 26/27] Re-build README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ff0de62..a09a902 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ The package has been developed using the [Government Analysis Function Data Visualisation guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/) and uses accessible colour palettes from the [Scottish Government Design -System](https://designsystem.gov.scot/guidance/charts). sgplot should be -used in conjunction with these guidance documents. +System](https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes). +sgplot should be used in conjunction with these guidance documents. More information about the package and its functions can be found on the [sgplot website](https://DataScienceScotland.github.io/sgplot). In From 1916519f18341e22af2e385845d17cfaaabb99e0 Mon Sep 17 00:00:00 2001 From: alicebyers5 Date: Tue, 29 Aug 2023 12:42:22 +0100 Subject: [PATCH 27/27] Fix lintr warning --- R/sg_palette.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/sg_palette.R b/R/sg_palette.R index ecdf3b4..3ef279d 100644 --- a/R/sg_palette.R +++ b/R/sg_palette.R @@ -31,9 +31,9 @@ sg_palette <- function(palette = "main", # Use 'main2' if AF main palette used and only 2 colours required if ( palette_type == "af" && - n == 2 && - palette != "main2" && - grepl("main", palette) + n == 2 && + palette != "main2" && + grepl("main", palette) ) { palette <- "main2" cli::cli_warn(c(