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/DESCRIPTION b/DESCRIPTION index 5ffbeed..8b098de 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,8 +18,9 @@ Description: Provides implementation of various methods of data analysis that wo License: BSD_3_clause + file LICENSE LazyData: false Imports: + ggplot2, MASS Suggests: testthat NeedsCompilation: no -RoxygenNote: 6.1.1 +RoxygenNote: 7.0.2 diff --git a/NAMESPACE b/NAMESPACE index c4ed42d..ca02353 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,9 @@ # 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) +import(ggplot2) diff --git a/R/graph_colours.R b/R/graph_colours.R new file mode 100644 index 0000000..3107d33 --- /dev/null +++ b/R/graph_colours.R @@ -0,0 +1,128 @@ +#' 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 colour 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_chart_colours) + } + 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 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 +#' +#' @import ggplot2 +#' @examples +#' library(ggplot2) +#' ggplot(iris, aes(Sepal.Length, Petal.Length, colour=Species)) + +#' geom_point() + +#' theme_minimal() + +#' scale_colour_nhsd() +#' +#' @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 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 +#' +#' @import ggplot2 +#' @examples +#' library(ggplot2) +#' 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 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..0ba0532 --- /dev/null +++ b/man/scale_colour_nhsd.Rd @@ -0,0 +1,30 @@ +% 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{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)} +} +\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(ggplot2) +ggplot(iris, aes(Sepal.Length, Petal.Length, colour=Species)) + + geom_point() + + theme_minimal() + + scale_colour_nhsd() + +} diff --git a/man/scale_fill_nhsd.Rd b/man/scale_fill_nhsd.Rd new file mode 100644 index 0000000..14f0e5a --- /dev/null +++ b/man/scale_fill_nhsd.Rd @@ -0,0 +1,29 @@ +% 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{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)} +} +\value{ +A colour pallette +} +\description{ +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() + + scale_fill_nhsd(discrete=FALSE, reverse=TRUE) + +}