@drsimonj here to share how I create and reuse corporate color palettes for ggplot2.
You’ve started work as a data scientist at “drsimonj Inc” (congratulations, by the way) and PR have asked that all your Figures use the corporate colours. They send you the image below (coincidentally the Metro UI colors on color-hex.com):
You want to use these colours with ggplot2 while also making your code reusable and flexible.
 Outline and setup
We’re going to create the following:
Named vector of hex codes for the corporate colors
Function to access hex codes (in 1)
Named list of corporate color palettes (combinations of colors via 2)
Function to access palettes (in 3)
ggplot2-compatible scale functions that use the corporate palettes (via 4)
Load the ggplot2 package and set a default theme to setup:
library(ggplot2)
theme_set(theme_minimal())
 Start with color
Everything starts with colors. I like to first create a named vector of corporate colors like this:
# drsimonj corporate colors
drsimonj_colors dark grey #__ &quot;#8c8c8c&quot;
drsimonj_cols(&quot;red&quot;)
#__ red #__ &quot;#d11141&quot;
drsimonj_cols(&quot;red&quot;, &quot;blue&quot;)
#__ red blue #__ &quot;#d11141&quot; &quot;#00aedb&quot;
drsimonj_cols(&quot;blue&quot;, &quot;red&quot;)
#__ blue red #__ &quot;#00aedb&quot; &quot;#d11141&quot;
It’s already possible to use these manually in plots like this:
ggplot(mtcars, aes(hp, mpg)) +
geom_point(color = drsimonj_cols(&quot;red&quot;),
size = 4, alpha = .8)
 Combine colors into palettes
Like all great organisations, drsimonj Inc has a few main colors (let’s say blue, green, and yellow), but the full list (above) includes other official colors used for a variety of purposes. So we can now create palettes (various combinations) of these colors. Similar to how we deal with colors, first define a list like such:
drsimonj_palettes rgb(x[, 1L], x[, 2L], x[, 3L], x[, 4L], maxColorValue = 255)
#__ else rgb(x[, 1L], x[, 2L], x[, 3L], maxColorValue = 255)
#__ }
#__ #__ This returned function will interpolate the palette colors for a certain number of levels, making it possible to create shades between our original colors. To demonstrate, we can interpolate the &quot;cool&quot; palette (which only includes two colors, green and blue) to a length of 10:
drsimonj_pal(&quot;cool&quot;)(10)
#__ [1] &quot;#00AEDB&quot; &quot;#00AECC&quot; &quot;#00AEBE&quot; &quot;#00AFAF&quot; &quot;#00AFA1&quot; &quot;#00AF92&quot; &quot;#00B084&quot;
#__ [8] &quot;#00B075&quot; &quot;#00B067&quot; &quot;#00B159&quot;
These hex codes correspond to the following:
This is what we need to create custom ggplot2 scales.
 Scales for ggplot2
We’ve now got the essentials to create custom color and fill scale functions for ggplot2. There are many ways to do this. I like the approach taken in packages like ochRe. One function is created for color and another for fill, and each contains a boolean argument for the relevant aesthetic being discrete or not. Here are my versions:
#' Color scale constructor for drsimonj colors
#'
#' @param palette Character name of palette in drsimonj_palettes
#' @param discrete Boolean indicating whether color aesthetic is discrete or not
#' @param reverse Boolean indicating whether the palette should be reversed
#' @param ... Additional arguments passed to discrete_scale() or
#' scale_color_gradientn(), used respectively when discrete is TRUE or FALSE
#'
scale_color_drsimonj