From 2653e5cf7ddaadea45a0c437da4c91dff902c07c Mon Sep 17 00:00:00 2001 From: Mike Roberts <42875462+mrob95@users.noreply.github.com> Date: Mon, 27 Jan 2020 16:36:03 +0000 Subject: [PATCH 1/4] Add NHSD colour utilities --- R/graph_colours.R | 141 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 R/graph_colours.R diff --git a/R/graph_colours.R b/R/graph_colours.R new file mode 100644 index 0000000..32bdfbe --- /dev/null +++ b/R/graph_colours.R @@ -0,0 +1,141 @@ +#' NHS Digital core colours +#' +#' A large selection of NHS digital core colours, selected from +#' https://digital.nhs.uk/about-nhs-digital/corporate-information-and-documents/nhs-digital-style-guidelines/how-we-look/colour-palette +#' +#' @export +nhsd_core_colours = c( + "chart_grey_3" = "#F8F8F8", + "white" = "#FFFFFF", + "white_tints" = c("#F9FAFB", "#F3F5F6", "#EDEFF1", "#DEF2E5"), + "nhs_blue" = "#005EB8", + "blue_tints" = c("#337EC6", "#ACCAE8", "#D4E4F3", "#E6EFF8"), + "nhs_dark_grey" = "#425563", + "grey_tints" = c( + "#687784", + "#98A4AD", + "#B3BBC1", + "#DFE2E5", + "#EDEFF1", + "#F3F5F6", + "#F9FAFB" + ), + "nhs_mild_grey" = "#768692", + "nhs_warm_yellow" = "#FFB81C", + "warm_yellow_tints" = c("#FFE8B4", "#FFF1CC", "#FFF8E8") +) + + +#' NHS Digital chart colours +#' +#' A list of chart colours constructed from the NHS color palette. +#' Contains the values "light blue", "blue", "dark blue", "warm yellow", "light grey", "grey" and "dark grey". +#' +#' @export +nhsd_chart_colours = c( + `light blue` = "#71CCEF", + `blue` = "#005EB8", + `dark blue` = "#003087", + `warm yellow` = "#FFB81C", + `light grey` = "#D0D5D6", + `grey` = "#84919C", + `dark grey` = "#425563" +) + + +nhsd_cols <- function(...) { + cols <- c(...) + if (is.null(cols)) { + return (nhsd_colors) + } + nhsd_chart_colours[cols] +} + +nhsd_palettes <- list( + `main` = nhsd_cols("warm yellow", "blue", "light blue", "dark blue"), + + `blue` = nhsd_cols("light blue", "blue", "dark blue"), + + `grey` = nhsd_cols("light grey", "grey", "dark grey") +) + +nhsd_pal <- function(palette = "main", reverse = FALSE, ...) { + pal <- nhsd_palettes[[palette]] + if (reverse) { + pal <- rev(pal) + } + grDevices::colorRampPalette(pal, ...) +} + +#' NHSD colour scale +#' +#' A ggplot compatible colour palette for producing NHS digital graphs. +#' +#' @param pallette Which color palette to use, can be "main", "blue", "grey" (default: "main") +#' @param discrete Whether this scale is discrete or continuous (default: TRUE) +#' @param reverse Whether to reverse the scale (default: FALSE) +#' @return A colour pallette, to be added to a ggplot plot object +#' +#' @examples +#' library(gapminder) +#' +#' countries <- c("United Kingdom", "China", "Gambia") +#' +#' gapminder %>% +#' filter(country %in% countries) %>% +#' ggplot(aes(year, lifeExp, color=country)) + +#' geom_smooth(se = FALSE) + +#' labs( +#' title = "Life expectancy", +#' x = "Year", +#' y = "Expectancy", +#' caption = "Source: gapminder (2020)" +#' ) + +#' theme_minimal() + +#' scale_color_nhsd("blue") + +#' ggsave("test.png", +#' type = "cairo", +#' width = 7, +#' height = 4, +#' ) +#' +#' @aliases scale_color_nhsd +#' @export +scale_colour_nhsd <- function(palette = "main", discrete = TRUE, reverse = FALSE, ...) { + pal <- nhsd_pal(palette = palette, reverse = reverse) + + if (discrete) { + ggplot2::discrete_scale("colour", paste0("nhsd_", palette), palette = pal, ...) + } else { + ggplot2::scale_color_gradientn(colours = pal(256), ...) + } +} + +#' @export +scale_color_nhsd <- scale_colour_nhsd + +#' NHSD fill scale +#' +#' A ggplot compatible colour palette for producing NHS digital graphs. +#' +#' @param pallette Which color palette to use, can be "main", "blue", "grey" (default: "main") +#' @param discrete Whether this scale is discrete or continuous (default: FALSE) +#' @param reverse Whether to reverse the scale (default: FALSE) +#' @return A colour pallette +#' +#' @examples +#' ggplot(faithfuld, aes(waiting, eruptions)) + +#' geom_raster(aes(fill = density)) + +#' theme_minimal() + +#' scale_fill_nhsd(discrete=FALSE, reverse=TRUE) +#' +#' @export +scale_fill_nhsd <- function(palette = "main", discrete = TRUE, reverse = FALSE, ...) { + pal <- nhsd_pal(palette = palette, reverse = reverse) + + if (discrete) { + ggplot2::discrete_scale("fill", paste0("nhsd_", palette), palette = pal, ...) + } else { + ggplot2::scale_fill_gradientn(colours = pal(256), ...) + } +} \ No newline at end of file From d39aa3dc6f0431e2fa8d436b386aaf781af115ff Mon Sep 17 00:00:00 2001 From: Mike Roberts <42875462+mrob95@users.noreply.github.com> Date: Mon, 27 Jan 2020 16:38:07 +0000 Subject: [PATCH 2/4] Rebuild documentation --- DESCRIPTION | 2 +- NAMESPACE | 5 +++++ R/graph_colours.R | 12 +++++----- man/nhsd_chart_colours.Rd | 15 +++++++++++++ man/nhsd_core_colours.Rd | 15 +++++++++++++ man/scale_colour_nhsd.Rd | 46 +++++++++++++++++++++++++++++++++++++++ man/scale_fill_nhsd.Rd | 28 ++++++++++++++++++++++++ 7 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 man/nhsd_chart_colours.Rd create mode 100644 man/nhsd_core_colours.Rd create mode 100644 man/scale_colour_nhsd.Rd create mode 100644 man/scale_fill_nhsd.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 5ffbeed..a45a869 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,4 +22,4 @@ Imports: Suggests: testthat NeedsCompilation: no -RoxygenNote: 6.1.1 +RoxygenNote: 7.0.2 diff --git a/NAMESPACE b/NAMESPACE index c4ed42d..15e3bf5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,8 @@ # Generated by roxygen2: do not edit by hand export(age_bands) +export(nhsd_chart_colours) +export(nhsd_core_colours) +export(scale_color_nhsd) +export(scale_colour_nhsd) +export(scale_fill_nhsd) diff --git a/R/graph_colours.R b/R/graph_colours.R index 32bdfbe..e32a670 100644 --- a/R/graph_colours.R +++ b/R/graph_colours.R @@ -28,7 +28,7 @@ nhsd_core_colours = c( #' NHS Digital chart colours #' -#' A list of chart colours constructed from the NHS color palette. +#' A list of chart colours constructed from the NHS colour palette. #' Contains the values "light blue", "blue", "dark blue", "warm yellow", "light grey", "grey" and "dark grey". #' #' @export @@ -46,7 +46,7 @@ nhsd_chart_colours = c( nhsd_cols <- function(...) { cols <- c(...) if (is.null(cols)) { - return (nhsd_colors) + return (nhsd_chart_colours) } nhsd_chart_colours[cols] } @@ -71,7 +71,7 @@ nhsd_pal <- function(palette = "main", reverse = FALSE, ...) { #' #' A ggplot compatible colour palette for producing NHS digital graphs. #' -#' @param pallette Which color palette to use, can be "main", "blue", "grey" (default: "main") +#' @param pallette Which colour palette to use, can be "main", "blue", "grey" (default: "main") #' @param discrete Whether this scale is discrete or continuous (default: TRUE) #' @param reverse Whether to reverse the scale (default: FALSE) #' @return A colour pallette, to be added to a ggplot plot object @@ -83,7 +83,7 @@ nhsd_pal <- function(palette = "main", reverse = FALSE, ...) { #' #' gapminder %>% #' filter(country %in% countries) %>% -#' ggplot(aes(year, lifeExp, color=country)) + +#' ggplot(aes(year, lifeExp, colour=country)) + #' geom_smooth(se = FALSE) + #' labs( #' title = "Life expectancy", @@ -92,7 +92,7 @@ nhsd_pal <- function(palette = "main", reverse = FALSE, ...) { #' caption = "Source: gapminder (2020)" #' ) + #' theme_minimal() + -#' scale_color_nhsd("blue") + +#' scale_colour_nhsd("blue") + #' ggsave("test.png", #' type = "cairo", #' width = 7, @@ -118,7 +118,7 @@ scale_color_nhsd <- scale_colour_nhsd #' #' A ggplot compatible colour palette for producing NHS digital graphs. #' -#' @param pallette Which color palette to use, can be "main", "blue", "grey" (default: "main") +#' @param pallette Which colour palette to use, can be "main", "blue", "grey" (default: "main") #' @param discrete Whether this scale is discrete or continuous (default: FALSE) #' @param reverse Whether to reverse the scale (default: FALSE) #' @return A colour pallette diff --git a/man/nhsd_chart_colours.Rd b/man/nhsd_chart_colours.Rd new file mode 100644 index 0000000..6e0d11f --- /dev/null +++ b/man/nhsd_chart_colours.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/graph_colours.R +\docType{data} +\name{nhsd_chart_colours} +\alias{nhsd_chart_colours} +\title{NHS Digital chart colours} +\format{An object of class \code{character} of length 7.} +\usage{ +nhsd_chart_colours +} +\description{ +A list of chart colours constructed from the NHS colour palette. +Contains the values "light blue", "blue", "dark blue", "warm yellow", "light grey", "grey" and "dark grey". +} +\keyword{datasets} diff --git a/man/nhsd_core_colours.Rd b/man/nhsd_core_colours.Rd new file mode 100644 index 0000000..157d4a8 --- /dev/null +++ b/man/nhsd_core_colours.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/graph_colours.R +\docType{data} +\name{nhsd_core_colours} +\alias{nhsd_core_colours} +\title{NHS Digital core colours} +\format{An object of class \code{character} of length 24.} +\usage{ +nhsd_core_colours +} +\description{ +A large selection of NHS digital core colours, selected from +https://digital.nhs.uk/about-nhs-digital/corporate-information-and-documents/nhs-digital-style-guidelines/how-we-look/colour-palette +} +\keyword{datasets} diff --git a/man/scale_colour_nhsd.Rd b/man/scale_colour_nhsd.Rd new file mode 100644 index 0000000..814ad76 --- /dev/null +++ b/man/scale_colour_nhsd.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/graph_colours.R +\name{scale_colour_nhsd} +\alias{scale_colour_nhsd} +\alias{scale_color_nhsd} +\title{NHSD colour scale} +\usage{ +scale_colour_nhsd(palette = "main", discrete = TRUE, reverse = FALSE, ...) +} +\arguments{ +\item{discrete}{Whether this scale is discrete or continuous (default: TRUE)} + +\item{reverse}{Whether to reverse the scale (default: FALSE)} + +\item{pallette}{Which colour palette to use, can be "main", "blue", "grey" (default: "main")} +} +\value{ +A colour pallette, to be added to a ggplot plot object +} +\description{ +A ggplot compatible colour palette for producing NHS digital graphs. +} +\examples{ +library(gapminder) + +countries <- c("United Kingdom", "China", "Gambia") + +gapminder \%>\% + filter(country \%in\% countries) \%>\% + ggplot(aes(year, lifeExp, colour=country)) + + geom_smooth(se = FALSE) + + labs( + title = "Life expectancy", + x = "Year", + y = "Expectancy", + caption = "Source: gapminder (2020)" + ) + + theme_minimal() + + scale_colour_nhsd("blue") + + ggsave("test.png", + type = "cairo", + width = 7, + height = 4, + ) + +} diff --git a/man/scale_fill_nhsd.Rd b/man/scale_fill_nhsd.Rd new file mode 100644 index 0000000..73a7308 --- /dev/null +++ b/man/scale_fill_nhsd.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/graph_colours.R +\name{scale_fill_nhsd} +\alias{scale_fill_nhsd} +\title{NHSD fill scale} +\usage{ +scale_fill_nhsd(palette = "main", discrete = TRUE, reverse = FALSE, ...) +} +\arguments{ +\item{discrete}{Whether this scale is discrete or continuous (default: FALSE)} + +\item{reverse}{Whether to reverse the scale (default: FALSE)} + +\item{pallette}{Which colour palette to use, can be "main", "blue", "grey" (default: "main")} +} +\value{ +A colour pallette +} +\description{ +A ggplot compatible colour palette for producing NHS digital graphs. +} +\examples{ +ggplot(faithfuld, aes(waiting, eruptions)) + + geom_raster(aes(fill = density)) + + theme_minimal() + + scale_fill_nhsd(discrete=FALSE, reverse=TRUE) + +} From d36814273ede6d640373650e149e74a4bec6f444 Mon Sep 17 00:00:00 2001 From: Mike Roberts <42875462+mrob95@users.noreply.github.com> Date: Tue, 28 Jan 2020 09:54:59 +0000 Subject: [PATCH 3/4] Modify examples to make travis happy --- .travis.yml | 3 ++- R/graph_colours.R | 29 ++++++++--------------------- man/scale_colour_nhsd.Rd | 27 ++++++--------------------- man/scale_fill_nhsd.Rd | 6 ++++-- 4 files changed, 20 insertions(+), 45 deletions(-) diff --git a/.travis.yml b/.travis.yml index fda44de..938b341 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ -language: R +language: R +r_packages: ggplot2 cache: packages sudo: false warnings_are_errors: false diff --git a/R/graph_colours.R b/R/graph_colours.R index e32a670..2c78826 100644 --- a/R/graph_colours.R +++ b/R/graph_colours.R @@ -71,33 +71,18 @@ nhsd_pal <- function(palette = "main", reverse = FALSE, ...) { #' #' A ggplot compatible colour palette for producing NHS digital graphs. #' -#' @param pallette Which colour palette to use, can be "main", "blue", "grey" (default: "main") +#' @param palette Which colour palette to use, can be "main", "blue", "grey" (default: "main") #' @param discrete Whether this scale is discrete or continuous (default: TRUE) #' @param reverse Whether to reverse the scale (default: FALSE) #' @return A colour pallette, to be added to a ggplot plot object #' #' @examples -#' library(gapminder) +#' library(ggplot2) #' -#' countries <- c("United Kingdom", "China", "Gambia") -#' -#' gapminder %>% -#' filter(country %in% countries) %>% -#' ggplot(aes(year, lifeExp, colour=country)) + -#' geom_smooth(se = FALSE) + -#' labs( -#' title = "Life expectancy", -#' x = "Year", -#' y = "Expectancy", -#' caption = "Source: gapminder (2020)" -#' ) + +#' ggplot(iris, aes(Sepal.Length, Petal.Length, colour=Species)) + +#' geom_point() + #' theme_minimal() + -#' scale_colour_nhsd("blue") + -#' ggsave("test.png", -#' type = "cairo", -#' width = 7, -#' height = 4, -#' ) +#' scale_colour_nhsd() #' #' @aliases scale_color_nhsd #' @export @@ -118,12 +103,14 @@ scale_color_nhsd <- scale_colour_nhsd #' #' A ggplot compatible colour palette for producing NHS digital graphs. #' -#' @param pallette Which colour palette to use, can be "main", "blue", "grey" (default: "main") +#' @param palette Which colour palette to use, can be "main", "blue", "grey" (default: "main") #' @param discrete Whether this scale is discrete or continuous (default: FALSE) #' @param reverse Whether to reverse the scale (default: FALSE) #' @return A colour pallette #' #' @examples +#' library(ggplot2) +#' #' ggplot(faithfuld, aes(waiting, eruptions)) + #' geom_raster(aes(fill = density)) + #' theme_minimal() + diff --git a/man/scale_colour_nhsd.Rd b/man/scale_colour_nhsd.Rd index 814ad76..f2ed06b 100644 --- a/man/scale_colour_nhsd.Rd +++ b/man/scale_colour_nhsd.Rd @@ -8,11 +8,11 @@ scale_colour_nhsd(palette = "main", discrete = TRUE, reverse = FALSE, ...) } \arguments{ +\item{palette}{Which colour palette to use, can be "main", "blue", "grey" (default: "main")} + \item{discrete}{Whether this scale is discrete or continuous (default: TRUE)} \item{reverse}{Whether to reverse the scale (default: FALSE)} - -\item{pallette}{Which colour palette to use, can be "main", "blue", "grey" (default: "main")} } \value{ A colour pallette, to be added to a ggplot plot object @@ -21,26 +21,11 @@ A colour pallette, to be added to a ggplot plot object A ggplot compatible colour palette for producing NHS digital graphs. } \examples{ -library(gapminder) - -countries <- c("United Kingdom", "China", "Gambia") +library(ggplot2) -gapminder \%>\% - filter(country \%in\% countries) \%>\% - ggplot(aes(year, lifeExp, colour=country)) + - geom_smooth(se = FALSE) + - labs( - title = "Life expectancy", - x = "Year", - y = "Expectancy", - caption = "Source: gapminder (2020)" - ) + +ggplot(iris, aes(Sepal.Length, Petal.Length, colour=Species)) + + geom_point() + theme_minimal() + - scale_colour_nhsd("blue") + - ggsave("test.png", - type = "cairo", - width = 7, - height = 4, - ) + scale_colour_nhsd() } diff --git a/man/scale_fill_nhsd.Rd b/man/scale_fill_nhsd.Rd index 73a7308..df60556 100644 --- a/man/scale_fill_nhsd.Rd +++ b/man/scale_fill_nhsd.Rd @@ -7,11 +7,11 @@ scale_fill_nhsd(palette = "main", discrete = TRUE, reverse = FALSE, ...) } \arguments{ +\item{palette}{Which colour palette to use, can be "main", "blue", "grey" (default: "main")} + \item{discrete}{Whether this scale is discrete or continuous (default: FALSE)} \item{reverse}{Whether to reverse the scale (default: FALSE)} - -\item{pallette}{Which colour palette to use, can be "main", "blue", "grey" (default: "main")} } \value{ A colour pallette @@ -20,6 +20,8 @@ A colour pallette A ggplot compatible colour palette for producing NHS digital graphs. } \examples{ +library(ggplot2) + ggplot(faithfuld, aes(waiting, eruptions)) + geom_raster(aes(fill = density)) + theme_minimal() + From 2e7330436c39b2caa319e6364abcf43205596f6f Mon Sep 17 00:00:00 2001 From: Mike Roberts <42875462+mrob95@users.noreply.github.com> Date: Tue, 28 Jan 2020 10:59:07 +0000 Subject: [PATCH 4/4] Fix example imports --- DESCRIPTION | 1 + NAMESPACE | 1 + R/graph_colours.R | 4 ++-- man/scale_colour_nhsd.Rd | 1 - man/scale_fill_nhsd.Rd | 1 - 5 files changed, 4 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a45a869..8b098de 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,6 +18,7 @@ Description: Provides implementation of various methods of data analysis that wo License: BSD_3_clause + file LICENSE LazyData: false Imports: + ggplot2, MASS Suggests: testthat diff --git a/NAMESPACE b/NAMESPACE index 15e3bf5..ca02353 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,3 +6,4 @@ export(nhsd_core_colours) export(scale_color_nhsd) export(scale_colour_nhsd) export(scale_fill_nhsd) +import(ggplot2) diff --git a/R/graph_colours.R b/R/graph_colours.R index 2c78826..3107d33 100644 --- a/R/graph_colours.R +++ b/R/graph_colours.R @@ -76,9 +76,9 @@ nhsd_pal <- function(palette = "main", reverse = FALSE, ...) { #' @param reverse Whether to reverse the scale (default: FALSE) #' @return A colour pallette, to be added to a ggplot plot object #' +#' @import ggplot2 #' @examples #' library(ggplot2) -#' #' ggplot(iris, aes(Sepal.Length, Petal.Length, colour=Species)) + #' geom_point() + #' theme_minimal() + @@ -108,9 +108,9 @@ scale_color_nhsd <- scale_colour_nhsd #' @param reverse Whether to reverse the scale (default: FALSE) #' @return A colour pallette #' +#' @import ggplot2 #' @examples #' library(ggplot2) -#' #' ggplot(faithfuld, aes(waiting, eruptions)) + #' geom_raster(aes(fill = density)) + #' theme_minimal() + diff --git a/man/scale_colour_nhsd.Rd b/man/scale_colour_nhsd.Rd index f2ed06b..0ba0532 100644 --- a/man/scale_colour_nhsd.Rd +++ b/man/scale_colour_nhsd.Rd @@ -22,7 +22,6 @@ A ggplot compatible colour palette for producing NHS digital graphs. } \examples{ library(ggplot2) - ggplot(iris, aes(Sepal.Length, Petal.Length, colour=Species)) + geom_point() + theme_minimal() + diff --git a/man/scale_fill_nhsd.Rd b/man/scale_fill_nhsd.Rd index df60556..14f0e5a 100644 --- a/man/scale_fill_nhsd.Rd +++ b/man/scale_fill_nhsd.Rd @@ -21,7 +21,6 @@ A ggplot compatible colour palette for producing NHS digital graphs. } \examples{ library(ggplot2) - ggplot(faithfuld, aes(waiting, eruptions)) + geom_raster(aes(fill = density)) + theme_minimal() +