-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from ScotGovAnalysis/refactor-sg-palette
Refactor sg_palette and improve handling of palette_type in other fns
- Loading branch information
Showing
14 changed files
with
324 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: sgplot | ||
Title: Graphic Styles and Colours for Scottish Government Plots | ||
Version: 0.3.0 | ||
Version: 0.3.0.9000 | ||
Authors@R: c( | ||
person("Scottish Government", , , "[email protected]", role = c("cph", "fnd")), | ||
person("Alice", "Hannah", , "[email protected]", c("aut", "cre")) | ||
|
@@ -13,7 +13,7 @@ URL: | |
BugReports: https://github.com/ScotGovAnalysis/sgplot/issues | ||
Encoding: UTF-8 | ||
LazyData: true | ||
RoxygenNote: 7.3.1 | ||
RoxygenNote: 7.3.2 | ||
Depends: | ||
R (>= 2.10) | ||
Imports: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,100 @@ | ||
#' Return function to use Scottish Government colour palette | ||
#' Return function to use sgplot colour palette | ||
#' | ||
#' @param palette Name of palette to use. | ||
#' @param reverse Boolean value to indicate whether the palette should be | ||
#' reversed. | ||
#' @param colour_names Boolean value to indicate whether colour names should be | ||
#' included. | ||
#' @param palette_type Either "sg" to use Scottish Government palettes | ||
#' (default), "sss" to use Social Security Scotland palettes or "af" to use | ||
#' Analysis Function palettes. | ||
#' @param palette_type Name of palette type to use. Defaults to "sg". For all | ||
#' available palette types, run `available_palette_types()`. | ||
#' | ||
#' @return Function with one argument, `n`. | ||
#' | ||
#' @noRd | ||
|
||
sg_palette <- function(palette = "main", | ||
reverse = FALSE, | ||
colour_names = FALSE, | ||
palette_type = c("sg", "sss", "af"), | ||
palette_type = "sg", | ||
error_call = rlang::caller_env(), | ||
error_arg = rlang::caller_arg(palette)) { | ||
|
||
palette_type <- rlang::arg_match(palette_type) | ||
check_palette(palette_type, palette) | ||
|
||
palette_list <- switch( | ||
palette_type, | ||
af = sgplot::af_colour_palettes, | ||
sss = sgplot::sss_colour_palettes, | ||
sg = sgplot::sg_colour_palettes | ||
) | ||
function(n) { | ||
pal <- get_colours(palette_type, palette, n) | ||
|
||
# Check valid palette name | ||
if (!palette %in% names(palette_list)) { | ||
cli::cli_abort( | ||
c("x" = paste("{.str {palette}} is not a valid palette name ", | ||
"for {.str {palette_type}} palette type."), | ||
"i" = "Available palette{?s}: {.str {names(palette_list)}}."), | ||
call = error_call | ||
) | ||
if (reverse) pal <- rev(pal) | ||
|
||
if (!colour_names) pal <- as.vector(pal) | ||
|
||
pal | ||
} | ||
|
||
function(n) { | ||
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) | ||
) { | ||
palette <- "main2" | ||
cli::cli_warn(c( | ||
"!" = "Using {.str 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" = paste("{.arg {error_arg}} must contain at least", | ||
"{n} colours."), | ||
"i" = paste("The {.str {palette}} palette from the ", | ||
"{.str {palette_type}} palette type", | ||
"only contains {n_available} colours."), | ||
if (n > 4) { | ||
c("i" = paste("Accessibility guidance recommends a limit of four", | ||
"colours per chart. If more than four colours are", | ||
"required, first consider chart redesign.")) | ||
}, | ||
if (n > 4 & !is.null(ext_palettes)) { | ||
c("i" = paste("If it is essential to use more than four colours,", | ||
"the {.str {ext_palettes}} palette{?s} can be used.")) | ||
} | ||
), | ||
call = error_call | ||
) | ||
} | ||
|
||
pal <- palette_list[[palette]][seq_len(n)] | ||
#' Get colours | ||
#' | ||
#' @description This function checks that the required number of colours are | ||
#' available from the selected palette and palette type, and if so returns them. | ||
#' If not, a helpful error is returned. | ||
#' | ||
#' @param palette_type,palette String. | ||
#' @param n Number of colours required. | ||
#' | ||
#' @return Named character vector of colours, length `n`. | ||
#' | ||
#' @noRd | ||
|
||
get_colours <- function(palette_type, palette, n) { | ||
|
||
if (reverse) pal <- rev(pal) | ||
all_palettes <- eval(parse( | ||
text = paste0("sgplot::", palette_type, "_colour_palettes") | ||
)) | ||
|
||
n_available <- length(all_palettes[[palette]]) | ||
|
||
if (is.null(n)) n <- n_available | ||
|
||
# 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 {.str main2} as only two colours are required." | ||
)) | ||
} | ||
|
||
# Error if more colours requested than exist in palette | ||
if (n > n_available) { | ||
ext_palettes <- grep("^main([5-9]|-extended)", | ||
available_palettes(palette_type), | ||
value = TRUE) | ||
|
||
if (colour_names) { | ||
pal | ||
} else { | ||
as.vector(pal) | ||
} | ||
cli::cli_abort( | ||
c( | ||
"x" = "{.arg {palette}} must contain at least {n} colours.", | ||
"i" = paste("The {.str {palette}} palette from the ", | ||
"{.str {palette_type}} palette type", | ||
"only contains {n_available} colours."), | ||
if (n > 4) { | ||
c("i" = paste("Accessibility guidance recommends a limit of four", | ||
"colours per chart. If more than four colours are", | ||
"required, first consider chart redesign.")) | ||
}, | ||
if (n > 4 & !is.null(ext_palettes)) { | ||
c("i" = paste("If it is essential to use more than four colours,", | ||
"the {.str {ext_palettes}} palette{?s} can be used.")) | ||
} | ||
) | ||
) | ||
} | ||
|
||
all_palettes[[palette]][seq_len(n)] | ||
|
||
} |
Oops, something went wrong.