diff --git a/R/colors_discrete.R b/R/colors_discrete.R index bed8276e..42010384 100644 --- a/R/colors_discrete.R +++ b/R/colors_discrete.R @@ -1,6 +1,8 @@ #' CMAP discrete color palettes #' -#' A selection of discrete color palettes from the CMAP color palette. +#' A selection of discrete color palettes from the CMAP color palette. These +#' include mixed color palettes and discrete versions of the gradients defined +#' in \code{link{cmap_fill_continuous}}. #' #' @examples #' # Get names of available discrete palettes. @@ -11,40 +13,44 @@ #' purrr::walk2(cmap_palettes, names(cmap_palettes), viz_palette) #' #' @export -cmap_palettes <- list( +cmap_palettes <- c( - prosperity = c("#662f00", "#e5d072", "#44008c", "#c8e572", "#c9a7ef"), + # Add CMAP gradients to the palettes list (note that we don't add the + # palettes to the gradients list since those are not sequential). + cmap_gradients, - community = c("#cc5f00", "#006b8c", "#e5a872", "#d2efa7", "#662f00"), + # Mixed color palettes + list(prosperity = c("#662f00", "#e5d072", "#44008c", "#c8e572", "#c9a7ef"), - environment = c("#00665c", "#b7e572", "#3f0030", "#36d8ca", "#006b8c"), + community = c("#cc5f00", "#006b8c", "#e5a872", "#d2efa7", "#662f00"), - governance = c("#006b8c", "#efa7a7", "#8c4100", "#00303f", "#cca600", "#a7efe8"), + environment = c("#00665c", "#b7e572", "#3f0030", "#36d8ca", "#006b8c"), - mobility = c("#8c0000", "#e5bd72", "#a7efe8", "#6d8692", "#0084ac", "#efa7a7"), + governance = c("#006b8c", "#efa7a7", "#8c4100", "#00303f", "#cca600", "#a7efe8"), - legislation = c("#00becc", "#cc5f00", "#3f0e00", "#cca600", "#003f8c", "#67ac00"), + mobility = c("#8c0000", "#e5bd72", "#a7efe8", "#6d8692", "#0084ac", "#efa7a7"), - friday = c("#00093f", "#ac8c00", "#475c66", "#e5d072", "#b5c1c8", "#006b8c"), + legislation = c("#00becc", "#cc5f00", "#3f0e00", "#cca600", "#003f8c", "#67ac00"), - race = c(white = "#75a5d8", - black = "#84c87e", - hispanic = "#d8ba39", - asian = "#e77272", - other = "#607b88") + friday = c("#00093f", "#ac8c00", "#475c66", "#e5d072", "#b5c1c8", "#006b8c"), -) + race = c(white = "#75a5d8", + black = "#84c87e", + hispanic = "#d8ba39", + asian = "#e77272", + other = "#607b88") + ) +) #' Print palette for reference #' -#' @param pal character, vector of (hexadecimal) colours representing a palette +#' @param pal character, vector of (hexadecimal) colors representing a palette #' @param ttl character, title to be displayed (the name of the palette) -#' @param num numeric, the number of colours to display +#' @param num numeric, the number of colors to display #' -#' @describeIn cmap_palettes Display CMAP diverging palettes. Borrowed with -#' respect from the \href{https://github.com/ropenscilabs/ochRe}{ochRe -#' package} +#' @describeIn cmap_palettes Display CMAP palettes. Borrowed with respect from +#' the \href{https://github.com/ropenscilabs/ochRe}{ochRe package} #' #' @export viz_palette <- function(pal, ttl = deparse(substitute(pal)), num = length(pal)) { @@ -53,15 +59,16 @@ viz_palette <- function(pal, ttl = deparse(substitute(pal)), num = length(pal)) } pal_func <- grDevices::colorRampPalette(pal) graphics::image(seq_len(num), 1, as.matrix(seq_len(num)), col = pal_func(num), - main = paste0(ttl, " (", length(pal), " colours in palette, ", + main = paste0(ttl, " (", length(pal), " colors in palette, ", num, " displayed)"), xlab = "", ylab = "", xaxt = "n", yaxt = "n", bty = "n") } - #' Discrete palette prep function #' -#' @param palette Choose from 'cmap_palettes' list +#' @param palette Choose from 'cmap_palettes' list, or use one of the gradients +#' defined in the 'cmap_gradients' list (gradients will be automatically +#' converted into discrete bins) #' @param reverse Logical; reverse color order? #' @param ... Additional parameters passed on to the scale type #' @@ -74,13 +81,14 @@ cmap_pal_discrete <- function(palette = "prosperity", reverse = FALSE) { return(grDevices::colorRampPalette(pal)) } - #' Apply discrete CMAP palettes to ggplot2 aesthetics #' #' Pick the function depending on the aesthetic of your ggplot object (fill or #' color). See \code{link{cmap_palettes}} for a listing of available gradients. #' -#' @param palette Choose from 'cmap_palettes' list +#' @param palette Choose from 'cmap_palettes' list, or use one of the gradients +#' defined in the 'cmap_gradients' list (gradients will be automatically +#' converted into discrete bins) #' @param reverse Logical; reverse color order? #' @param ... Additional parameters passed on to the scale type #' diff --git a/man/cmap_fill_discrete.Rd b/man/cmap_fill_discrete.Rd index d238a32b..faa8333e 100644 --- a/man/cmap_fill_discrete.Rd +++ b/man/cmap_fill_discrete.Rd @@ -13,7 +13,9 @@ cmap_color_discrete(palette = "prosperity", reverse = FALSE, ...) cmap_colour_discrete(palette = "prosperity", reverse = FALSE, ...) } \arguments{ -\item{palette}{Choose from 'cmap_palettes' list} +\item{palette}{Choose from 'cmap_palettes' list, or use one of the gradients +defined in the 'cmap_gradients' list (gradients will be automatically +converted into discrete bins)} \item{reverse}{Logical; reverse color order?} diff --git a/man/cmap_palettes.Rd b/man/cmap_palettes.Rd index 29014960..080a14a6 100644 --- a/man/cmap_palettes.Rd +++ b/man/cmap_palettes.Rd @@ -6,7 +6,7 @@ \alias{viz_palette} \title{CMAP discrete color palettes} \format{ -An object of class \code{list} of length 8. +An object of class \code{list} of length 31. } \usage{ cmap_palettes @@ -14,20 +14,21 @@ cmap_palettes viz_palette(pal, ttl = deparse(substitute(pal)), num = length(pal)) } \arguments{ -\item{pal}{character, vector of (hexadecimal) colours representing a palette} +\item{pal}{character, vector of (hexadecimal) colors representing a palette} \item{ttl}{character, title to be displayed (the name of the palette)} -\item{num}{numeric, the number of colours to display} +\item{num}{numeric, the number of colors to display} } \description{ -A selection of discrete color palettes from the CMAP color palette. +A selection of discrete color palettes from the CMAP color palette. These +include mixed color palettes and discrete versions of the gradients defined +in \code{link{cmap_fill_continuous}}. } \section{Functions}{ \itemize{ -\item \code{viz_palette}: Display CMAP diverging palettes. Borrowed with -respect from the \href{https://github.com/ropenscilabs/ochRe}{ochRe -package} +\item \code{viz_palette}: Display CMAP palettes. Borrowed with respect from +the \href{https://github.com/ropenscilabs/ochRe}{ochRe package} }} \examples{ diff --git a/vignettes/colors.Rmd b/vignettes/colors.Rmd index 945b924e..be5e7fbe 100644 --- a/vignettes/colors.Rmd +++ b/vignettes/colors.Rmd @@ -24,24 +24,6 @@ library(cmapplot) Palettes based on the CMAP color palette can be applied directly to ggplot2 graphics. The package contains both discrete and continuous color palettes. Each type of palette can be applied to either the color or fill attributes of a ggplot. - -## Discrete palettes - -Add discrete palettes by adding either the `cmap_fill_discrete()` or `cmap_color_discrete()` function to your plot construction. Note that discrete palettes will automatically interpolate additional colors if the dataset has more colors than the palette. This can be helpful but is not ideal for finished graphics. See `?cmap_palettes` for displays of all discrete palettes. - -```{r color_discrete, message = FALSE} -df <- dplyr::filter(grp_over_time, category == "Goods-Producing") - -ggplot(data = df) + - geom_line(mapping = aes(x = year, y = realgrp, color = cluster), - size = 1.25) + - scale_x_continuous(breaks = seq(from = 2007, to = 2017, by = 2)) + - cmap_color_discrete(palette = "community", reverse = TRUE) + - theme_cmap() + - ggtitle("Real GRP of goods-producing clusters over time") -``` - - ## Continuous palettes Add continuous palettes by adding either the `cmap_fill_continuous()` or `cmap_color_continuous()` function to your plot construction. For example: @@ -66,6 +48,23 @@ ggplot(data = df) + If you're using a divergent palette, you can specify the midpoint where the divergence begins (default is zero). See `?cmap_gradients` for displays of all continuous palettes. +## Discrete palettes + +Add discrete palettes by adding either the `cmap_fill_discrete()` or `cmap_color_discrete()` function to your plot construction. Note that discrete palettes will automatically interpolate additional colors if the dataset has more colors than the palette. This can be helpful but is not ideal for finished graphics. See `?cmap_palettes` for displays of all discrete palettes. These include a set of complementary colors (e.g., the `"prosperity"` palette), but also discrete versions of the gradients defined above (e.g., a binned blue palette implemented by calling `"seq_blues"`). + +```{r color_discrete, message = FALSE} +df <- dplyr::filter(grp_over_time, category == "Goods-Producing") + +ggplot(data = df) + + geom_line(mapping = aes(x = year, y = realgrp, color = cluster), + size = 1.25) + + scale_x_continuous(breaks = seq(from = 2007, to = 2017, by = 2)) + + cmap_color_discrete(palette = "community", reverse = TRUE) + + theme_cmap() + + ggtitle("Real GRP of goods-producing clusters over time") +``` + + ## Race/ethnicity palette If you have a graph with categories based on race and ethnicity, there are functions to apply pre-determined colors for each demographic group: `cmap_fill_race()` and `cmap_color_race()`. In the arguments, specify the case-sensitive name of each group as it appears in your data. The function can be used even if your dataset does not contain every race/ethnicity category — simply omit the parameters for the missing categories.