<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/creating-corporate-colour-palettes-for-ggplot2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://drsimonj.svbtle.com/creating-corporate-colour-palettes-for-ggplot2"> blogR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://twitter.com/drsimonj" target="_blank">@drsimonj</a> here to share how I create and reuse corporate color palettes for ggplot2.</p>
<p>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 <a rel="nofollow" href="http://www.color-hex.com/color-palette/700" target="_blank">Metro UI colors on color-hex.com</a>):</p>
<p><a rel="nofollow" href="https://i2.wp.com/svbtleusercontent.com/zxequdizcj5apg.png?ssl=1" target="_blank"><img alt="corporate_swatch-1.png" src="https://i1.wp.com/svbtleusercontent.com/zxequdizcj5apg_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>You want to use these colours with ggplot2 while also making your code reusable and flexible.</p>
<h2><a rel="nofollow" href="http://drsimonj.svbtle.com/creating-corporate-colour-palettes-for-ggplot2#outline-and-setup_2" target="_blank"> </a>Outline and setup</h2>
<p>We’re going to create the following:</p>
<ol>
<li> Named vector of hex codes for the corporate colors</li>
<li> Function to access hex codes (in 1)</li>
<li> Named list of corporate color palettes (combinations of colors via 2)</li>
<li> Function to access palettes (in 3)</li>
<li> ggplot2-compatible scale functions that use the corporate palettes (via 4)</li>
</ol>
<p>Load the ggplot2 package and set a default theme to setup:</p>
<pre><code>library(ggplot2)

theme_set(theme_minimal())
</code></pre>
<h2><a rel="nofollow" href="http://drsimonj.svbtle.com/creating-corporate-colour-palettes-for-ggplot2#start-with-color_2" target="_blank"> </a>Start with color</h2>
<p>Everything starts with colors. I like to first create a named vector of corporate colors like this:</p>
<pre><code># drsimonj corporate colors
drsimonj_colors &lt;- c(
  `red`        = &quot;#d11141&quot;,
  `green`      = &quot;#00b159&quot;,
  `blue`       = &quot;#00aedb&quot;,
  `orange`     = &quot;#f37735&quot;,
  `yellow`     = &quot;#ffc425&quot;,
  `light grey` = &quot;#cccccc&quot;,
  `dark grey`  = &quot;#8c8c8c&quot;)
</code></pre>
<p>Any changes to these colors, or addition of new colors, are done in this vector.</p>
<p>Tip: use back ticks to remove naming restrictions (e.g., to include spaces for `light grey` and `dark grey`).</p>
<p>Next, write a function that extracts the hex codes from this vector by name.</p>
<pre><code>#' Function to extract drsimonj colors as hex codes
#'
#' @param ... Character names of drsimonj_colors 
#'
drsimonj_cols &lt;- function(...) {
  cols &lt;- c(...)

  if (is.null(cols))
    return (drsimonj_colors)

  drsimonj_colors[cols]
}
</code></pre>
<p>This allows us to get hex colors in a robust and flexible way. For example, you can have all colors returned as they are, specify certain colors, in a particular order, add additional function arguments and checks, and so on:</p>
<pre><code>drsimonj_cols()
#>        red      green       blue     orange     yellow light grey 
#>  &quot;#d11141&quot;  &quot;#00b159&quot;  &quot;#00aedb&quot;  &quot;#f37735&quot;  &quot;#ffc425&quot;  &quot;#cccccc&quot; 
#>  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;
</code></pre>
<p>It’s already possible to use these manually in plots like this:</p>
<pre><code>ggplot(mtcars, aes(hp, mpg)) +
    geom_point(color = drsimonj_cols(&quot;red&quot;),
               size = 4, alpha = .8)
</code></pre>
<p><a rel="nofollow" href="https://i2.wp.com/svbtleusercontent.com/ork4qlkmzn9sag.png?ssl=1" target="_blank"><img alt="unnamed-chunk-6-1.png" src="https://i2.wp.com/svbtleusercontent.com/ork4qlkmzn9sag_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<h2><a rel="nofollow" href="http://drsimonj.svbtle.com/creating-corporate-colour-palettes-for-ggplot2#combine-colors-into-palettes_2" target="_blank"> </a>Combine colors into palettes</h2>
<p>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:</p>
<pre><code>drsimonj_palettes &lt;- list(
  `main`  = drsimonj_cols(&quot;blue&quot;, &quot;green&quot;, &quot;yellow&quot;),

  `cool`  = drsimonj_cols(&quot;blue&quot;, &quot;green&quot;),

  `hot`   = drsimonj_cols(&quot;yellow&quot;, &quot;orange&quot;, &quot;red&quot;),

  `mixed` = drsimonj_cols(&quot;blue&quot;, &quot;green&quot;, &quot;yellow&quot;, &quot;orange&quot;, &quot;red&quot;),

  `grey`  = drsimonj_cols(&quot;light grey&quot;, &quot;dark grey&quot;)
)
</code></pre>
<p>Changes or new color palettes are added in this list. We write a function to access and interpolate them like so:</p>
<pre><code>#' Return function to interpolate a drsimonj color palette
#'
#' @param palette Character name of palette in drsimonj_palettes
#' @param reverse Boolean indicating whether the palette should be reversed
#' @param ... Additional arguments to pass to colorRampPalette()
#'
drsimonj_pal &lt;- function(palette = &quot;main&quot;, reverse = FALSE, ...) {
  pal &lt;- drsimonj_palettes[[palette]]

  if (reverse) pal &lt;- rev(pal)

  colorRampPalette(pal, ...)
}
</code></pre>
<p>This function gets a pallete by name from the list (<code>&quot;main&quot;</code> by default), has a boolean condition determining whether to reverse the order or not, and additional arguments to pass on to <code>colorRampPallete()</code> (such as an <code>alpha</code> value). This returns another function:</p>
<pre><code>drsimonj_pal(&quot;cool&quot;)
#> function (n) 
#> {
#>     x &lt;- ramp(seq.int(0, 1, length.out = n))
#>     if (ncol(x) == 4L) 
#>         rgb(x[, 1L], x[, 2L], x[, 3L], x[, 4L], maxColorValue = 255)
#>     else rgb(x[, 1L], x[, 2L], x[, 3L], maxColorValue = 255)
#> }
#> </code></pre>