<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/make-your-own-color-palettes-with-paletti/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://edwinth.github.io/blog/paletti/"> That’s so Random</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Last week I blogged about the <a rel="nofollow" href="https://edwinth.github.io/blog/dutch-masters/" target="_blank">dutchmasters color palettes package</a>, which was inspired<br>
by the wonderful <a rel="nofollow" href="https://github.com/ropenscilabs/ochRe" target="_blank">ochRe package</a>. As<br>
mentioned I shamelessly copied the package. I replaced the list with character<br>
vectors containing hex colors and did a find and replace to make it<br>
<code>dutchmasters</code> instead of <code>ochRe</code>. This was pretty ugly. I realized that when<br>
we would refactor the <code>ochRe</code> functions, thus creating functions that create the functions, there would no longer be a need to copy-paste and find-and-replace. So that is what I did. I refactored and expanded <code>ochRe</code>’s chore into <code>paletti</code>. (Name chosen because I liked the ring of it). You grab it from Github, with <code>devtools::install_github(&quot;edwinth/paletti&quot;)</code>.</p>
<h2>Single palettes</h2>
<p><code>paletti</code> takes both single palettes (character vectors with hex codes) and<br>
lists with palettes, like the <code>ochRe</code> and <code>dutchmasters</code> lists. Lets start with<br>
a single palette, this might be useful when you want your coporate identity<br>
colors translated into R. Here I just pick some colors found on the <a rel="nofollow" href="http://blog.visme.co/website-color-schemes/" target="_blank">interweb</a></p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>paletti</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>mycols</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>
</span><span>red</span><span> </span><span>=</span><span> </span><span>&quot;#E27D60&quot;</span><span>,</span><span>
</span><span>blue</span><span> </span><span>=</span><span> </span><span>&quot;#085DCB&quot;</span><span>,</span><span>
</span><span>orange</span><span> </span><span>=</span><span> </span><span>&quot;#E8A87C&quot;</span><span>,</span><span>
</span><span>purple</span><span> </span><span>=</span><span> </span><span>&quot;#C38D9E&quot;</span><span>,</span><span>
</span><span>green</span><span> </span><span>=</span><span> </span><span>&quot;#41B3A3&quot;</span><span>
</span><span>)</span><span>
</span><span>viz_palette</span><span>(</span><span>mycols</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="plot of chunk unnamed-chunk-1" src="https://i1.wp.com/edwinth.github.io/figure/source/2017-12-21-paletti/unnamed-chunk-1-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Now, ochRe provided us with two functions, one two create a <code>ggplot</code> scale for<br>
colours and to create one for fills. These functions can be created in the<br>
following fashion</p>
<div>
<div>
<pre><code><span>mycols_fill</span><span> </span><span>&lt;-</span><span> </span><span>get_scale_fill</span><span>(</span><span>get_pal</span><span>(</span><span>mycols</span><span>))</span><span>
</span><span>mycols_color</span><span> </span><span>&lt;-</span><span> </span><span>get_scale_color</span><span>(</span><span>get_pal</span><span>(</span><span>mycols</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<p>Both now can be used in <code>ggplot</code></p>
<div>
<div>
<pre><code><span>mtcars</span><span>$</span><span>cyl</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>mtcars</span><span>$</span><span>cyl</span><span>)</span><span>
</span><span>col_plot</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>aes</span><span>(</span><span>mpg</span><span>,</span><span> </span><span>drat</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>cyl</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span> </span><span>col_plot</span><span> </span><span>+</span><span> </span><span>mycols_color</span><span>()</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="plot of chunk unnamed-chunk-3" src="https://i1.wp.com/edwinth.github.io/figure/source/2017-12-21-paletti/unnamed-chunk-3-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<div>
<pre><code><span>fill_plot</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>aes</span><span>(</span><span>cyl</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>cyl</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_bar</span><span>()</span><span>
</span><span>fill_plot</span><span> </span><span>+</span><span> </span><span>mycols_fill</span><span>()</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="plot of chunk unnamed-chunk-3" src="https://i2.wp.com/edwinth.github.io/figure/source/2017-12-21-paletti/unnamed-chunk-3-2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Now, I said I expanded the <code>ochRe</code> code a bit. The function <code>get_hex</code> will<br>
produce a function that will return a function in which you can directly return<br>
the hex code by typing its unquoted name. Handy if you want an exact color from<br>
your palette. Prerequisit is that your palette is a named character vector.</p>
<div>
<div>
<pre><code><span>mycols_hex</span><span> </span><span>&lt;-</span><span> </span><span>get_hex</span><span>(</span><span>mycols</span><span>)</span><span>
</span><span>fill_plot</span><span> </span><span>+</span><span> </span><span>scale_fill_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>mycols_hex</span><span>(</span><span>blue</span><span>,</span><span> </span><span>purple</span><span>,</span><span> </span><span>red</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i0.wp.com/edwinth.github.io/figure/source/2017-12-21-paletti/unnamed-chunk-4-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Multiple palettes in a list</h2>
<p>Both <code>ochRe</code> and <code>dutchmasters</code> offer multiple palettes in a list. The only<br>
difference from a single palette is that in the returned function you have to<br>
specify the name of the palette youw want to use. If you don’t, it defaults to<br>
the first palette in the list.</p>
<div>
<div>
<pre><code><span>ochRe_col</span><span> </span><span>&lt;-</span><span> </span><span>get_scale_color</span><span>(</span><span>get_pal</span><span>(</span><span>ochre_palettes</span><span>))</span><span>
</span><span>dutchmasters_fill</span><span> </span><span>&lt;-</span><span> </span><span>get_scale_fill</span><span>(</span><span>get_pal</span><span>(</span><span>dutchmasters</span><span>))</span><span>
</span><span>col_plot</span><span> </span><span>+</span><span> </span><span>ochRe_col</span><span>(</span><span>&quot;lorikeet&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i0.wp.com/edwinth.github.io/figure/source/2017-12-21-paletti/unnamed-chunk-5-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<div>
<pre><code><span>fill_plot</span><span> </span><span>+</span><span> </span><span>dutchmasters_fill</span><span>(</span><span>&quot;view_of_Delft&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i0.wp.com/edwinth.github.io/figure/source/2017-12-21-paletti/unnamed-chunk-5-2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>And the same holds for the <code>get_hex</code> function. You can feed a list with palettes<br>
as well. Note that the palettes that you are going to call must have named<br>
elements.</p>
<div>
<div>
<pre><code><span>dutchmasters_hex</span><span> </span><span>&lt;-</span><span> </span><span>get_hex</span><span>(</span><span>dutchmasters</span><span>)</span><span>
</span><span>fill_plot</span><span> </span><span>+</span><span> </span><span>scale_fill_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>dutchmasters_hex</span><span>(</span><span>&quot;anatomy&quot;</span><span>,</span><span>
</span><span>black</span><span>(</span><span>cloak</span><span>),</span><span> </span><span>red</span><span>(</span><span>muscle</span><span>),</span><span> </span><span>brown</span><span>(</span><span>table</span><span>)))</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="plot of chunk unnamed-chunk-6" src="https://i1.wp.com/edwinth.github.io/figure/source/2017-12-21-paletti/unnamed-chunk-6-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>That’s it, off you go! Add your own color palette(s) and start plotting. Once<br>
again a major thanks to the ochRe team for the inspiration and the foundations<br>
on which <code>paletti</code> is built.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://edwinth.github.io/blog/paletti/"> That’s so Random</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/9qTpnHdDp5w" height="1"></a></div>