<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/how-to-create-a-ggplot-theme-unicorn-edition/"></div></div><div><a data-url="https://www.r-bloggers.com/how-to-create-a-ggplot-theme-unicorn-edition/" data-counturl="https://www.r-bloggers.com/how-to-create-a-ggplot-theme-unicorn-edition/" data-count="horizontal" data-text="How to create a ggplot Theme – Unicorn Edition" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://flovv.github.io/unicorn_ggplot_theme/"> Florian Teschner</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Themes are an convenient way to give ggplot charts an individualized, sometimes stylish look. Most of the time, I rely on the <a target="_blank" rel="nofollow" href="https://github.com/jrnold/ggthemes">ggthemes package</a> and the Economist style. Last week colleagues asked me to change the look of my charts. We joked around and I agreed to create a unicorn ggplot theme. I want to use the challenge to detail a) how to create custom ggplot themes and b) look at unicorn startup data.</p>
<p>I took the <a target="_blank" rel="nofollow" href="https://www.cbinsights.com/research-unicorn-companies">Unicorn startup dataset</a>, which contains startups with a valuation above 1 bil$.<br>
For the theme, in the first step, I changed some basic parameters (coloring, no ticks <code>axis.ticks=element_blank()</code>, no legend title <code>legend.title = element_blank()</code> and the same background for legend elements as well as the legend background <code>legend.key = element_rect(fill = &quot;lightskyblue1&quot;, color = &quot;lightskyblue1&quot;)</code>). For the impatient reader; the complete code is at the end of the post.</p>
<figure>
<pre><code data-lang="r"><span>ggplot</span><span>(</span><span>dd</span><span>,</span><span> </span><span>aes</span><span>(</span><span>year</span><span>,</span><span> </span><span>N</span><span>,</span><span> </span><span>color</span><span>=</span><span>Industry</span><span>,</span><span> </span><span>fill</span><span>=</span><span>Industry</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_bar</span><span>(</span><span>stat</span><span>=</span><span>&quot;identity&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>xlab</span><span>(</span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>ylab</span><span>(</span><span>&quot;Number of Unicorns&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>theme_unicorn</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>5</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-2" src="https://i1.wp.com/flovv.github.io/figures/post15/unnamed-chunk-2-1.png?w=456" data-recalc-dims="1"></p>
<p>Data-wise, we see that the number of unicorn valuations massively increased in recent years. Most unicorns are active in the area of ecommerce and marketplaces.<br>
Theme-wise; while the chart looks colorful, there are some parts missing; font-style and appropriate coloring. In the chart above, ggplot defaulted to the standard color palette for the plotted elements.<br>
In order to fix this, I got the “unicorn” color palette from <a target="_blank" rel="nofollow" href="http://www.color-hex.com/color-palette/22646">here</a> and created two functions to overwrite the color and fill functions. Additionally, a true unicorn chart needs a different font. Even though it is pretty old, Comic Sans might be appropriate here.</p>
<figure>
<pre><code data-lang="r"><span>windowsFonts</span><span>(</span><span>F</span><span> </span><span>=</span><span> </span><span>windowsFont</span><span>(</span><span>'Comic Sans MS'</span><span>))</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>ddd</span><span>[</span><span>1</span><span>:</span><span>5</span><span>,],</span><span> </span><span>aes</span><span>(</span><span>reorder</span><span>(</span><span>Industry</span><span>,</span><span> </span><span>-</span><span>m</span><span>),</span><span> </span><span>m</span><span>,</span><span> </span><span>color</span><span>=</span><span>Industry</span><span>,</span><span> </span><span>fill</span><span>=</span><span>Industry</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>size</span><span>=</span><span>4</span><span>,</span><span> </span><span>shape</span><span>=</span><span>&quot;O&quot;</span><span>)</span><span>
</span><span>p</span><span>+</span><span> </span><span>theme_unicorn</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>8</span><span>,</span><span>font</span><span>=</span><span>&quot;F&quot;</span><span>)</span><span> </span><span>+</span><span>scale_color_discrete_unicorn</span><span>()</span><span>+</span><span>scale_fill_unicorn</span><span>()</span><span> </span><span>+</span><span> </span><span>ylab</span><span>(</span><span>&quot;Mean Valuation in Bil$&quot;</span><span>)</span><span>+</span><span> </span><span>xlab</span><span>(</span><span>&quot;Industries&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-3" src="https://i1.wp.com/flovv.github.io/figures/post15/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<p>So, what’s missing to complete the unicorn theme? A real unicorn mixed with all the startups. Let’s add an png-image of an unicorn using <code>annotation_custom(g, xmin=30, xmax=35, ymin=10, ymax=12)</code>. The annoying part of it is, that one needs to place the image somehow manually on top of the chart using the x,y parameters. I also tried to place it within a bar-chart with factors/categories along the x-axis but I have not been able to figure how to do that.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>png</span><span>)</span><span>
</span><span>library</span><span>(</span><span>grid</span><span>)</span><span>
</span><span>## adding an image
</span><span>img</span><span> </span><span>&lt;-</span><span> </span><span>readPNG</span><span>(</span><span>source</span><span> </span><span>=</span><span> </span><span>&quot;image/unicorn.png&quot;</span><span>)</span><span>
</span><span>g</span><span> </span><span>&lt;-</span><span> </span><span>rasterGrob</span><span>(</span><span>img</span><span>,</span><span> </span><span>interpolate</span><span>=</span><span>TRUE</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>d</span><span>4</span><span>,</span><span> </span><span>aes</span><span>(</span><span>N</span><span>,</span><span> </span><span>m</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>size</span><span>=</span><span>2</span><span>,</span><span> </span><span>shape</span><span>=</span><span>1</span><span>,</span><span> </span><span>color</span><span>=</span><span>&quot;purple&quot;</span><span>)</span><span>
</span><span>p</span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>theme_unicorn</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span>font</span><span>=</span><span>&quot;F&quot;</span><span>)</span><span> </span><span>+</span><span>xlab</span><span>(</span><span>&quot;&quot;</span><span>)</span><span>+</span><span> </span><span>ylab</span><span>(</span><span>&quot;Mean Valuation in Bil$&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>xlab</span><span>(</span><span>&quot;Number of Unicorns&quot;</span><span>)</span><span> </span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>annotation_custom</span><span>(</span><span>g</span><span>,</span><span> </span><span>xmin</span><span>=</span><span>30</span><span>,</span><span> </span><span>xmax</span><span>=</span><span>35</span><span>,</span><span> </span><span>ymin</span><span>=</span><span>10</span><span>,</span><span> </span><span>ymax</span><span>=</span><span>12</span><span>)</span><span> </span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>annotate</span><span>(</span><span>geom</span><span>=</span><span>&quot;text&quot;</span><span>,</span><span> </span><span>x</span><span>=</span><span>3.5</span><span>,</span><span> </span><span>y</span><span>=</span><span>12</span><span>,</span><span> </span><span>label</span><span>=</span><span>&quot;Transportation&quot;</span><span>,</span><span> </span><span>color</span><span>=</span><span>&quot;lightblue&quot;</span><span> </span><span>,</span><span> </span><span>family</span><span>=</span><span>F</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>annotate</span><span>(</span><span>geom</span><span>=</span><span>&quot;text&quot;</span><span>,</span><span> </span><span>x</span><span>=</span><span>12</span><span>,</span><span> </span><span>y</span><span>=</span><span>11.5</span><span>,</span><span> </span><span>label</span><span>=</span><span>&quot;On Demand&quot;</span><span>,</span><span> </span><span>color</span><span>=</span><span>&quot;lightblue&quot;</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span> </span><span>+</span><span> </span><span>annotate</span><span>(</span><span>geom</span><span>=</span><span>&quot;text&quot;</span><span>,</span><span> </span><span>x</span><span>=</span><span>34</span><span>,</span><span> </span><span>y</span><span>=</span><span>4</span><span>,</span><span> </span><span>label</span><span>=</span><span>&quot;Ecommerce&quot;</span><span>,</span><span> </span><span>color</span><span>=</span><span>&quot;lightblue&quot;</span><span>)</span><span>
</span><span>p</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i0.wp.com/flovv.github.io/figures/post15/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<p>While ggplot certainly has massively improved in terms of style and customizability, there are some parts which do not work 100% (or I was not able to get it working properly).<br>
Font-family setting leads to warnings/errors (even though the chart appears). Additionally, the passing of the font-family breaks down. The chart above illustrates that, the axis labels are Comic Sans, the annotated text is not. I tried to set it directly in the annotate-function, but that would not work either.</p>
<p>Another issue is color-setting. Colors have to be defined differently depending on whether one works with a continuous scale or a discrete scale. Hence, they cannot be set within the “theme”, but need to be individually set depending on the chart. While that makes somehow sense, I assume a general custom color-setting function would be highly appreciated. Another encountered issue -when setting custom colors- is the number of colors to be defined in the discrete case. If you pass too many classes (e.g. over 20 industry types as in chart 1), you need to define at least 20+ colors. For the custom <code>scale_color_manual()</code>, it would be great if it would provide a fallback to pick n colors along the colors passed.</p>
<p>I hope you enjoyed the colorful unicorn. Please reach out if you have solutions to the problems mentioned above.<br>
I hope I will find the time to create a customizable function in the form of: theme_custom(colorpalette=colors[1:5])</p>
<p></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/how-to-create-a-ggplot-theme-unicorn-edition/"></div></div><div><a data-url="https://www.r-bloggers.com/how-to-create-a-ggplot-theme-unicorn-edition/" data-counturl="https://www.r-bloggers.com/how-to-create-a-ggplot-theme-unicorn-edition/" data-count="vertical" data-text="How to create a ggplot Theme – Unicorn Edition" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://flovv.github.io/unicorn_ggplot_theme/"> Florian Teschner</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/sRdFfJjQFv8"></a></div>