Create a Custom Color Palette in R

Maintain brand consisteny by using a consistent color scheme across all of your data visualizations.

December 30, 2020

I’m describing customizing my own color palette for ggplot2. This is based off of Creating corporate colour palettes for ggplot2 by drsimonj.

First, find a bunch of colors that you like. Canva is a good place to go for inspiration; or, use this tool from Adobe Color that will extract colors from a photo you like. Then, there’s always colorbrewer2.

In many cases, you can simply use scale_color_manual() and scale_fill_manual() functions directly within ggplot2. See the ggplot2 docs for examples.

Define colors

This is the color scheme used by the non-profit, splashdown.org.

splashdown_colors <- c(
  `ebony` = "#1E2225",
  `blue_grotto` = "#05263B",
  `charcoal` = "#43758A",
  `light_sea_green` = "#36B4AF",
  `pewter` = "#D3DADA",
  `carafe` = "#55423A",
  `goldenrod` = "#F1B416",
  `burnt_sienna` = "#E56A19",
  `crimson` = "#900000")

Function to extract colors as hex codes (source: drsimonj)

splashdown_cols <- function(...) {
  cols <- c(...)
  if (is.null(cols))
    return (splashdown_colors)
  splashdown_colors[cols]
}

Examples of returning specific color codes. The scales package has a convenient show_col() function for this:

library(scales)
library(dplyr)

par(mfrow=c(1,2))
splashdown_cols() %>% show_col()
splashdown_cols("light_sea_green", "burnt_sienna") %>% show_col()

Very simple plot with color selection.

ggplot(mtcars, aes(hp, mpg)) +
    geom_point(color = splashdown_cols("goldenrod"),
               size = 4, alpha = .8)

Create palettes

Combine sets of colors to create individual palettes.

splashdown_palettes <- list(
  `primary`  = splashdown_cols("ebony", "blue_grotto", "charcoal", "light_sea_green", "pewter"),
  `secondary`  = splashdown_cols("carafe", "goldenrod", "burnt_sienna", "crimson"),
  `all`   = splashdown_cols("ebony", "blue_grotto", "charcoal", "light_sea_green", "pewter", "carafe", "goldenrod", "burnt_sienna", "crimson")
)

Function to interpolate the palettes (source: drsimonj). Option to add additional arguments into colorRampPallete() like alpha.

splashdown_pal <- function(palette = "main", reverse = FALSE, ...) {
  pal <- splashdown_palettes[[palette]]
  if (reverse) pal <- rev(pal)
  colorRampPalette(pal, ...)
}

Preview the palettes, e.g. applying 10 levels to the primary color palette. Again using scales::show_col() to display the colors with hex codes.

par(mfrow=c(1,2))

splashdown_pal("primary")(12) %>% show_col()
splashdown_pal("secondary")(4) %>% show_col()

Create Scale functions for ggplot2

Everything that’s been up to this point is creating a storage places for favorite colors. Now, prepare some functions specifically for using with ggplot.

scale_color_splashdown <- function(palette = "main",
           discrete = TRUE,
           reverse = FALSE,
           ...) {
    pal <- splashdown_pal(palette = palette, reverse = reverse)
    
    if (discrete) {
      discrete_scale("colour", paste0("splashdown_", palette), palette = pal, ...)
    } else {
      scale_color_gradientn(colours = pal(256), ...)
    }
}

scale_fill_splashdown <- function(palette = "main", discrete = TRUE, reverse = FALSE, ...) {
  pal <- splashdown_pal(palette = palette, reverse = reverse)

  if (discrete) {
    discrete_scale("fill", paste0("splashdown_", palette), palette = pal, ...)
  } else {
    scale_fill_gradientn(colours = pal(256), ...)
  }
}

Example plots

ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Sepal.Length)) +
    geom_point(size = 4, alpha = .6) +
    scale_color_splashdown(discrete = FALSE, palette = "primary")

ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
    geom_point(size = 4) +
    scale_color_splashdown("secondary")

ggplot(mpg, aes(manufacturer, fill = manufacturer)) +
    geom_bar() +
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
    scale_fill_splashdown(palette = "all", guide = "none")

Finally, I want to be able to pull down my palette whereever I am, so I copied the above R code into splashdown_col_pal.r and pushed it to my GitHub repo. I can read it down using devtools as an r file or as a gist. (devtools cheatsheet)

library(devtools)
source_url("https://raw.githubusercontent.com/rjfranssen/rjfranssen/main/utils/splashdown_col_pal.r")

Acknowledgements

This post was made possible thanks to

Posted on:
December 30, 2020
Length:
3 minute read, 549 words
Categories:
utilities helpers
Tags:
r ggplot2 color
See Also:
Parameterized Reporting in R
US Zip Code Choropleth
It's Always #Sunny in Philadelphia