diff --git a/DESCRIPTION b/DESCRIPTION
index 7c49cfb..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"))
@@ -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/NEWS.md b/NEWS.md
index 1d43d76..5c31126 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,3 +1,9 @@
+# sgplot 0.2.0
+
+* Add [Scottish Government Design System colour palettes](https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes)
+* Reduce `base_line_size` in `theme_sg()`
+* Remove default dark blue outline from `geom_col` and `geom_bar` when using `use_sgplot()`.
+
# sgplot 0.1.0
* First package release
diff --git a/R/colour_table.R b/R/colour_table.R
new file mode 100644
index 0000000..5d27b4a
--- /dev/null
+++ b/R/colour_table.R
@@ -0,0 +1,41 @@
+#' @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/data.R b/R/data.R
index 5e598cc..9046908 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/guidance/charts/data-visualisation-colour-palettes}{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/guidance/charts/data-visualisation-colour-palettes}{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..468f854 100644
--- a/R/scale_colour_continuous_sg.R
+++ b/R/scale_colour_continuous_sg.R
@@ -1,7 +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 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.
@@ -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..1e04f4f 100644
--- a/R/scale_colour_discrete_sg.R
+++ b/R/scale_colour_discrete_sg.R
@@ -1,7 +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 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.
@@ -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..cb6bf28 100644
--- a/R/scale_fill_continuous_sg.R
+++ b/R/scale_fill_continuous_sg.R
@@ -1,7 +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 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.
@@ -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..4cd7d37 100644
--- a/R/scale_fill_discrete_sg.R
+++ b/R/scale_fill_discrete_sg.R
@@ -1,7 +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 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.
@@ -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..3ef279d 100644
--- a/R/sg_palette.R
+++ b/R/sg_palette.R
@@ -5,47 +5,66 @@
#' 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"))
# 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]|-extended)")
+ )
+
# 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" = 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 `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/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/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/R/use_sgplot.R b/R/use_sgplot.R
index 01c33a9..2e0d10c 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
@@ -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
diff --git a/README.Rmd b/README.Rmd
index abcb627..1829421 100644
--- a/README.Rmd
+++ b/README.Rmd
@@ -34,9 +34,12 @@ 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/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 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 29b7c6a..a09a902 100644
--- a/README.md
+++ b/README.md
@@ -16,19 +16,20 @@ 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/).
+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). 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/_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"
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..22aa222 100644
--- a/data-raw/sg_colours.R
+++ b/data-raw/sg_colours.R
@@ -1,28 +1,27 @@
-# 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/guidance/
+# charts/data-visualisation-colour-palettes
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"
+ `dark-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")],
- `main6` = sg_colour_values[c(
- "dark-blue", "turquoise", "dark-pink",
- "orange", "dark-grey", "light-purple"
+ `main` = sg_colour_values[c("dark-blue", "teal", "purple", "orange")],
+ `main-extended` = sg_colour_values[c(
+ "dark-blue", "teal", "purple",
+ "orange", "dark-green", "green",
+ "brown", "pink"
)],
`sequential` = sg_colour_values[c("dark-blue", "mid-blue", "light-blue")],
`focus` = sg_colour_values[c("dark-blue", "grey")]
diff --git a/data/af_colour_palettes.rda b/data/af_colour_palettes.rda
new file mode 100644
index 0000000..e3c473a
Binary files /dev/null and b/data/af_colour_palettes.rda differ
diff --git a/data/af_colour_values.rda b/data/af_colour_values.rda
new file mode 100644
index 0000000..35cefb7
Binary files /dev/null and b/data/af_colour_values.rda differ
diff --git a/data/sg_colour_palettes.rda b/data/sg_colour_palettes.rda
index fdbbd4a..9f01ecc 100644
Binary files a/data/sg_colour_palettes.rda and b/data/sg_colour_palettes.rda differ
diff --git a/data/sg_colour_values.rda b/data/sg_colour_values.rda
index a001045..70d96f0 100644
Binary files a/data/sg_colour_values.rda and b/data/sg_colour_values.rda differ
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/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/figures/README-ex2-1.svg b/man/figures/README-ex2-1.svg
index d3708e3..0f77f28 100644
--- a/man/figures/README-ex2-1.svg
+++ b/man/figures/README-ex2-1.svg
@@ -220,7 +220,7 @@
-
+
@@ -232,81 +232,51 @@
-
-
-
-
-
-
-
+
-
-
-
-
+
-
-
-
-
+
-
-
-
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -327,17 +297,17 @@
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
diff --git a/man/scale_colour_continuous_sg.Rd b/man/scale_colour_continuous_sg.Rd
index 0a5d67e..d266b12 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",
@@ -13,8 +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{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..6e45d85 100644
--- a/man/scale_colour_discrete_sg.Rd
+++ b/man/scale_colour_discrete_sg.Rd
@@ -4,11 +4,19 @@
\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}{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{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..77b2389 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",
@@ -13,8 +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{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..8010194 100644
--- a/man/scale_fill_discrete_sg.Rd
+++ b/man/scale_fill_discrete_sg.Rd
@@ -4,11 +4,19 @@
\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}{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{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..4b2295b 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/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 96090cf..ac8993c 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/guidance/charts/data-visualisation-colour-palettes}{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/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}
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"),
diff --git a/man/use_sgplot.Rd b/man/use_sgplot.Rd
index f0aa31c..61055a1 100644
--- a/man/use_sgplot.Rd
+++ b/man/use_sgplot.Rd
@@ -8,7 +8,7 @@ use_sgplot(default_colour = sgplot::sg_colour_values["dark-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-colour_table.R b/tests/testthat/test-colour_table.R
new file mode 100644
index 0000000..3839c1d
--- /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)
+ )
+
+})
diff --git a/tests/testthat/test-sg_palette.R b/tests/testthat/test-sg_palette.R
index 5927e16..a694c9c 100644
--- a/tests/testthat/test-sg_palette.R
+++ b/tests/testthat/test-sg_palette.R
@@ -3,10 +3,13 @@ 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("main6", colour_names = TRUE)(5),
- sg_colour_palettes$main6[1:5]
+ sg_palette(palette_type = "af")(4),
+ unname(af_colour_palettes$main)
+ )
+ expect_equal(
+ sg_palette("main-extended", colour_names = TRUE)(5),
+ sg_colour_palettes$`main-extended`[1:5]
)
expect_equal(
sg_palette("sequential", colour_names = TRUE)(3),
@@ -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/accessibility.Rmd b/vignettes/accessibility.Rmd
index dc04e97..4985cc0 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/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()`
* `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/colours.Rmd b/vignettes/colours.Rmd
index d86ee6e..d53e40c 100644
--- a/vignettes/colours.Rmd
+++ b/vignettes/colours.Rmd
@@ -14,122 +14,170 @@ knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
message = FALSE,
- warning = FALSE
+ warning = FALSE,
+ eval = TRUE,
+ echo = FALSE
+)
+
+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."
)
```
-```{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 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.
+
+
+## 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/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:
+
+* Only use colour if absolutely necessary
+
+* Do not rely on colour alone to communicate a message
+
+* Use colour consistently when producing a series of plots
+
+* 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
+
+## 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.
+
+`r table_note`
+
+```{r}
+sgplot:::colour_table(sgplot::sg_colour_palettes$main)
+```
+
+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`
+
+```{r}
+sgplot:::colour_table(sgplot::sg_colour_palettes$`main-extended`)
```
-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.
-The following are a few key considerations when using colour in plots:
+### Sequential palette
-* 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
+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)
+```
+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.
-## Main Palettes
-There are three palettes available for use with categorical data:
+### Focus palette
-```{r main-palettes, echo = FALSE, fig.height = 4, fig.alt = col_tiles_alt("main")}
+The `focus` palette should be used to highlight specific elements of a plot.
-dat %>%
- filter(grepl("main", name)) %>%
- col_tiles()
+`r table_note`
+```{r}
+sgplot:::colour_table(sgplot::sg_colour_palettes$focus)
```
-* `main` is the default palette for discrete colour scales.
+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).
-* `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.
-* `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.
+## Analysis Function colour palettes
+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).
-## Other Palettes
-There are two other colour palettes available:
+### Main palette
-```{r other-palettes, echo = FALSE, fig.height = 3.5, fig.alt = col_tiles_alt("other")}
+The `main` palette should be used with categorical data.
-dat %>%
- filter(!grepl("main", name)) %>%
- col_tiles()
+`r table_note`
+```{r}
+sgplot:::colour_table(sgplot::af_colour_palettes$main)
```
-* `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 `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.
-* `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.
+`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 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.
-## A note on choice of colour palettes
-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.
+### Focus 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 +185,10 @@ 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
```
+
+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.
diff --git a/vignettes/cookbook/_annotations.Rmd b/vignettes/cookbook/_annotations.Rmd
index c87354f..387edcb 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)) +
diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/cookbook/_chart-types.Rmd
index c5c9d38..3c60b75 100644
--- a/vignettes/cookbook/_chart-types.Rmd
+++ b/vignettes/cookbook/_chart-types.Rmd
@@ -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)) +
@@ -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,14 +130,14 @@ 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 |>
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")
@@ -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,
@@ -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,
diff --git a/vignettes/cookbook/_colour-palettes.Rmd b/vignettes/cookbook/_colour-palettes.Rmd
index 3c72e2f..4ab5ef3 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/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).
-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."
diff --git a/vignettes/cookbook/_customisations.Rmd b/vignettes/cookbook/_customisations.Rmd
index 6d5c45f..0943fa5 100644
--- a/vignettes/cookbook/_customisations.Rmd
+++ b/vignettes/cookbook/_customisations.Rmd
@@ -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() +
@@ -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")
)
```