Skip to content

Commit 391cfb8

Browse files
authored
Merge pull request #46 from ScotGovAnalysis/dev
Final changes for v0.3.0
2 parents 9a74fca + be84076 commit 391cfb8

22 files changed

+212
-275
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: sgplot
22
Title: Graphic Styles and Colours for Scottish Government Plots
3-
Version: 0.2.2.9000
3+
Version: 0.3.0
44
Authors@R: c(
55
person("Scottish Government", , , "statistics.enquiries@gov.scot", role = c("cph", "fnd")),
66
person("Alice", "Hannah", , "alice.hannah@gov.scot", c("aut", "cre"))

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# sgplot (development version)
1+
# sgplot 0.3.0
22

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

5+
* Fix bug in `use_sgplot()` when passing arguments to `theme_sg()`
6+
57
# sgplot 0.2.2
68

79
* Fix links to contributing guidance in vignettes

R/data.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# nolint: start
2-
#' @title Scottish Government colour names and hex codes
2+
#' @title Scottish Government colours and palettes
33
#'
44
#' @description
55
#' * \code{sg_colour_values} is a vector containing colour names and their corresponding
@@ -16,12 +16,16 @@
1616
"sg_colour_palettes"
1717

1818

19-
#' @title Social Security Scotland colour names and hex codes
19+
#' @title Social Security Scotland colours and palettes
2020
#'
2121
#' @description
2222
#' * \code{sss_colour_values} is a vector containing colour names and their corresponding
2323
#' hex codes.
2424
#' * \code{sss_colour_palettes} is a list grouping colours into palettes.
25+
#'
26+
#' @source Contact the \href{mailto:MI@socialsecurity.gov.scot}{Social Security Scotland Statistics mailbox}
27+
#' with any queries about these colours and palettes.
28+
#'
2529
#' @md
2630

2731
"sss_colour_values"
@@ -31,7 +35,7 @@
3135

3236

3337
# nolint start
34-
#' @title Analysis Function colour names and hex codes
38+
#' @title Government Analysis Function colours and palettes
3539
#'
3640
#' @description
3741
#' * \code{af_colour_values} is a vector containing colour names and their corresponding

R/scale_colour_discrete_sg.R

Lines changed: 0 additions & 36 deletions
This file was deleted.

R/scale_colour_continuous_sg.R renamed to R/scale_continuous_sg.R

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
#' @title Continuous colour scales for Scottish Government plots
1+
#' @title Continuous colour/fill scales for Scottish Government plots
22
#'
33
#' @param palette Name of palette to use; e.g. "main", "sequential", "focus".
44
#' Default value is "sequential".
5-
#' @param palette_type Either `sg` to use Scottish Government palettes, `sss`
6-
#' to use Social Security Scotland palettes or `af` to use Analysis Function
7-
#' palettes. Defaults to `sg`.
8-
#' @param reverse Boolean value to indicate whether the palette should be
9-
#' reversed.
105
#' @param na_colour Colour to set for missing values.
116
#' @param guide A name or function used to create guide. Default is "colourbar".
12-
#' @param ... Additional arguments passed to scale type.
7+
#' @inheritParams scale_colour_discrete_sg
138
#'
149
#' @examples
1510
#' library(ggplot2)
@@ -18,6 +13,10 @@
1813
#' geom_point() +
1914
#' scale_colour_continuous_sg()
2015
#'
16+
#' ggplot(faithfuld, aes(x = waiting, y = eruptions, fill = density)) +
17+
#' geom_raster() +
18+
#' scale_fill_continuous_sg()
19+
#'
2120
#' @export
2221

2322
scale_colour_continuous_sg <- function(palette = "sequential",
@@ -48,7 +47,44 @@ scale_colour_continuous_sg <- function(palette = "sequential",
4847

4948
ggplot2::continuous_scale(
5049
aesthetics = "colour",
51-
scale_name = paste0(palette_type, "_continuous"),
50+
palette = scales::gradient_n_pal(colours, values = NULL, "Lab"),
51+
na.value = na_colour,
52+
guide = guide,
53+
...
54+
)
55+
}
56+
57+
#' @export
58+
#' @rdname scale_colour_continuous_sg
59+
60+
scale_fill_continuous_sg <- function(palette = "sequential",
61+
palette_type = c("sg", "sss", "af"),
62+
reverse = FALSE,
63+
na_colour = "grey50",
64+
guide = "colourbar",
65+
...) {
66+
67+
palette_type <- match.arg(palette_type)
68+
69+
palette_list <- switch(
70+
palette_type,
71+
af = sgplot::af_colour_palettes,
72+
sg = sgplot::sg_colour_palettes,
73+
sss = sgplot::sss_colour_palettes
74+
)
75+
76+
# Error if palette doesn't exist
77+
if (!palette %in% names(palette_list)) {
78+
cli::cli_abort(c(
79+
"x" = paste("`{palette}` is not a valid palette name in",
80+
"`{palette_type}_colour_palettes`.")
81+
))
82+
}
83+
84+
colours <- as.vector(palette_list[[palette]])
85+
86+
ggplot2::continuous_scale(
87+
aesthetics = "fill",
5288
palette = scales::gradient_n_pal(colours, values = NULL, "Lab"),
5389
na.value = na_colour,
5490
guide = guide,

R/scale_discrete_sg.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#' @title Discrete colour/fill scales for Scottish Government plots
2+
#'
3+
#' @param palette Name of palette to use; e.g. "main", "sequential", "focus".
4+
#' Default value is "main".
5+
#' @param palette_type Either "sg" to use Scottish Government palettes
6+
#' (default), "sss" to use Social Security Scotland palettes or "af" to use
7+
#' Analysis Function palettes.
8+
#' @param reverse Boolean value to indicate whether the palette should be
9+
#' reversed.
10+
#' @param ... Additional arguments passed to scale type.
11+
#'
12+
#' @examples
13+
#' library(ggplot2)
14+
#' library(dplyr)
15+
#'
16+
#' economics_long %>%
17+
#' filter(variable %in% c("psavert", "uempmed")) %>%
18+
#' ggplot(aes(x = date, y = value, colour = variable)) +
19+
#' geom_line(linewidth = 1) +
20+
#' scale_colour_discrete_sg()
21+
#'
22+
#' d <- subset(mpg, manufacturer == "ford")
23+
#'
24+
#' ggplot(d, aes(x = class, fill = class)) +
25+
#' geom_bar() +
26+
#' scale_fill_discrete_sg()
27+
#'
28+
#' @export
29+
30+
scale_colour_discrete_sg <- function(palette = "main",
31+
palette_type = c("sg", "sss", "af"),
32+
reverse = FALSE,
33+
...) {
34+
35+
palette_type <- match.arg(palette_type)
36+
37+
ggplot2::discrete_scale(
38+
aesthetics = "colour",
39+
palette = sg_palette(palette, reverse, palette_type = palette_type),
40+
...
41+
)
42+
43+
}
44+
45+
46+
#' @export
47+
#' @rdname scale_colour_discrete_sg
48+
49+
scale_fill_discrete_sg <- function(palette = "main",
50+
palette_type = c("sg", "sss", "af"),
51+
reverse = FALSE,
52+
...) {
53+
54+
palette_type <- match.arg(palette_type)
55+
56+
ggplot2::discrete_scale(
57+
aesthetics = "fill",
58+
palette = sg_palette(palette, reverse, palette_type = palette_type),
59+
...
60+
)
61+
62+
}

R/scale_fill_continuous_sg.R

Lines changed: 0 additions & 57 deletions
This file was deleted.

R/scale_fill_discrete_sg.R

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)