Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for discrete versions of gradients #131

Merged
merged 7 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions R/colors_discrete.R
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)) {
Expand All @@ -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
#'
Expand All @@ -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
#'
Expand Down
4 changes: 3 additions & 1 deletion man/cmap_fill_discrete.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions man/cmap_palettes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 17 additions & 18 deletions vignettes/colors.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down