Skip to content

Commit

Permalink
Merge pull request #46 from ScotGovAnalysis/dev
Browse files Browse the repository at this point in the history
Final changes for v0.3.0
  • Loading branch information
alice-hannah authored Oct 29, 2024
2 parents 9a74fca + be84076 commit 391cfb8
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 275 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sgplot
Title: Graphic Styles and Colours for Scottish Government Plots
Version: 0.2.2.9000
Version: 0.3.0
Authors@R: c(
person("Scottish Government", , , "statistics.enquiries@gov.scot", role = c("cph", "fnd")),
person("Alice", "Hannah", , "alice.hannah@gov.scot", c("aut", "cre"))
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# sgplot (development version)
# sgplot 0.3.0

* Add Social Security Scotland colours (`sss_colour_values`) and palettes (`sss_colour_palettes`)

* Fix bug in `use_sgplot()` when passing arguments to `theme_sg()`

# sgplot 0.2.2

* Fix links to contributing guidance in vignettes
Expand Down
10 changes: 7 additions & 3 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# nolint: start
#' @title Scottish Government colour names and hex codes
#' @title Scottish Government colours and palettes
#'
#' @description
#' * \code{sg_colour_values} is a vector containing colour names and their corresponding
Expand All @@ -16,12 +16,16 @@
"sg_colour_palettes"


#' @title Social Security Scotland colour names and hex codes
#' @title Social Security Scotland colours and palettes
#'
#' @description
#' * \code{sss_colour_values} is a vector containing colour names and their corresponding
#' hex codes.
#' * \code{sss_colour_palettes} is a list grouping colours into palettes.
#'
#' @source Contact the \href{mailto:MI@socialsecurity.gov.scot}{Social Security Scotland Statistics mailbox}
#' with any queries about these colours and palettes.
#'
#' @md

"sss_colour_values"
Expand All @@ -31,7 +35,7 @@


# nolint start
#' @title Analysis Function colour names and hex codes
#' @title Government Analysis Function colours and palettes
#'
#' @description
#' * \code{af_colour_values} is a vector containing colour names and their corresponding
Expand Down
36 changes: 0 additions & 36 deletions R/scale_colour_discrete_sg.R

This file was deleted.

52 changes: 44 additions & 8 deletions R/scale_colour_continuous_sg.R → R/scale_continuous_sg.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
#' @title Continuous colour scales for Scottish Government plots
#' @title Continuous colour/fill scales for Scottish Government plots
#'
#' @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, `sss`
#' to use Social Security Scotland 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.
#' @param guide A name or function used to create guide. Default is "colourbar".
#' @param ... Additional arguments passed to scale type.
#' @inheritParams scale_colour_discrete_sg
#'
#' @examples
#' library(ggplot2)
Expand All @@ -18,6 +13,10 @@
#' geom_point() +
#' scale_colour_continuous_sg()
#'
#' ggplot(faithfuld, aes(x = waiting, y = eruptions, fill = density)) +
#' geom_raster() +
#' scale_fill_continuous_sg()
#'
#' @export

scale_colour_continuous_sg <- function(palette = "sequential",
Expand Down Expand Up @@ -48,7 +47,44 @@ scale_colour_continuous_sg <- function(palette = "sequential",

ggplot2::continuous_scale(
aesthetics = "colour",
scale_name = paste0(palette_type, "_continuous"),
palette = scales::gradient_n_pal(colours, values = NULL, "Lab"),
na.value = na_colour,
guide = guide,
...
)
}

#' @export
#' @rdname scale_colour_continuous_sg

scale_fill_continuous_sg <- function(palette = "sequential",
palette_type = c("sg", "sss", "af"),
reverse = FALSE,
na_colour = "grey50",
guide = "colourbar",
...) {

palette_type <- match.arg(palette_type)

palette_list <- switch(
palette_type,
af = sgplot::af_colour_palettes,
sg = sgplot::sg_colour_palettes,
sss = sgplot::sss_colour_palettes
)

# Error if palette doesn't exist
if (!palette %in% names(palette_list)) {
cli::cli_abort(c(
"x" = paste("`{palette}` is not a valid palette name in",
"`{palette_type}_colour_palettes`.")
))
}

colours <- as.vector(palette_list[[palette]])

ggplot2::continuous_scale(
aesthetics = "fill",
palette = scales::gradient_n_pal(colours, values = NULL, "Lab"),
na.value = na_colour,
guide = guide,
Expand Down
62 changes: 62 additions & 0 deletions R/scale_discrete_sg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#' @title Discrete colour/fill scales for Scottish Government plots
#'
#' @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
#' (default), "sss" to use Social Security Scotland palettes or "af" to use
#' Analysis Function palettes.
#' @param reverse Boolean value to indicate whether the palette should be
#' reversed.
#' @param ... Additional arguments passed to scale type.
#'
#' @examples
#' library(ggplot2)
#' library(dplyr)
#'
#' economics_long %>%
#' filter(variable %in% c("psavert", "uempmed")) %>%
#' ggplot(aes(x = date, y = value, colour = variable)) +
#' geom_line(linewidth = 1) +
#' scale_colour_discrete_sg()
#'
#' d <- subset(mpg, manufacturer == "ford")
#'
#' ggplot(d, aes(x = class, fill = class)) +
#' geom_bar() +
#' scale_fill_discrete_sg()
#'
#' @export

scale_colour_discrete_sg <- function(palette = "main",
palette_type = c("sg", "sss", "af"),
reverse = FALSE,
...) {

palette_type <- match.arg(palette_type)

ggplot2::discrete_scale(
aesthetics = "colour",
palette = sg_palette(palette, reverse, palette_type = palette_type),
...
)

}


#' @export
#' @rdname scale_colour_discrete_sg

scale_fill_discrete_sg <- function(palette = "main",
palette_type = c("sg", "sss", "af"),
reverse = FALSE,
...) {

palette_type <- match.arg(palette_type)

ggplot2::discrete_scale(
aesthetics = "fill",
palette = sg_palette(palette, reverse, palette_type = palette_type),
...
)

}
57 changes: 0 additions & 57 deletions R/scale_fill_continuous_sg.R

This file was deleted.

35 changes: 0 additions & 35 deletions R/scale_fill_discrete_sg.R

This file was deleted.

Loading

0 comments on commit 391cfb8

Please sign in to comment.