<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/a-plot-against-the-catterplots-complot/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://masalmon.github.io/2017/02/18/complot/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In these terrible times, we R people have more important subjects to debate/care about than <code>ggplot2</code> vs. base R graphics (isn’t even worth discussing anyway, <code>ggplot2</code> is clearly the best alternative). Or so I thought until I saw <a target="_blank" rel="nofollow" href="https://github.com/Gibbsdavidl/CatterPlots"><code>CatterPlots</code></a> trending on Twitter this week and even being featured on <a target="_blank" rel="nofollow" href="http://blog.revolutionanalytics.com/2017/02/catterplots-plots-with-cats.html"><strong>Revolutions</strong> blog</a>. It was cool because plots with cats are cool, but looking more closely at the syntax of <code>CatterPlots</code>, I couldn’t but realize it was probably a complot to make us all like base R graphics syntax again! So let me show you how to make a cute plot with the awesome <code>ggplot2</code> extension <a target="_blank" rel="nofollow" href="https://github.com/GuangchuangYu/emojifont"><code>emojifont</code></a>.</p>
<p><span></span></p>
<p>The plot I’m going to re-make in this post is an old one already that I designed for celebrating <a target="_blank" rel="nofollow" href="https://edoc.ub.uni-muenchen.de/19877/">my PhD thesis</a> submission in March last year. I decided to compare the gestation time of my thesis, 1246 days, to the gestation time of some common mammals.</p>
<h1>Getting data</h1>
<p>I’m a bit ashamed because I forgot to write down where I took my data from but I’m pretty sure it was from reading Wikipedia pages and taking a more or less random numbers in the gestation time range indicated for each animals. I also chose animals I could find an emoji for. As you can imagine, it was quite an agreeable data collection. Here is what I end up with:</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;dplyr&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;forcats&quot;</span><span>)</span><span>
</span><span>gestation</span><span> </span><span>&lt;-</span><span> </span><span>readr</span><span>::</span><span>read_csv2</span><span>(</span><span>&quot;data/2017-02-18-complot.csv&quot;</span><span>)</span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>gestation</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>animal</span><span> </span><span>=</span><span> </span><span>fct_rev</span><span>(</span><span>fct_inorder</span><span>(</span><span>animal</span><span>)))</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>gestation</span><span>)</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>animal</th>
<th>gestation</th>
<th>label</th>
<th>color</th>
</tr>
</thead>
<tbody>
<tr>
<td>mouse</td>
<td>19</td>
<td>mouse</td>
<td>grey</td>
</tr>
<tr>
<td>dog</td>
<td>61</td>
<td>dog</td>
<td>grey</td>
</tr>
<tr>
<td>cat</td>
<td>64</td>
<td>cat</td>
<td>grey</td>
</tr>
<tr>
<td>wolf</td>
<td>68</td>
<td>wolf</td>
<td>grey</td>
</tr>
<tr>
<td>tiger</td>
<td>105</td>
<td>tiger</td>
<td>grey</td>
</tr>
<tr>
<td>pig</td>
<td>112</td>
<td>pig</td>
<td>grey</td>
</tr>
<tr>
<td>sheep</td>
<td>144</td>
<td>sheep</td>
<td>grey</td>
</tr>
<tr>
<td>bear</td>
<td>220</td>
<td>bear</td>
<td>grey</td>
</tr>
<tr>
<td>human</td>
<td>280</td>
<td>baby</td>
<td>grey</td>
</tr>
<tr>
<td>whale</td>
<td>590</td>
<td>whale</td>
<td>grey</td>
</tr>
<tr>
<td>elephant</td>
<td>617</td>
<td>elephant</td>
<td>grey</td>
</tr>
<tr>
<td>my thesis</td>
<td>1246</td>
<td>closed_book</td>
<td>gold</td>
</tr>
</tbody>
</table>
<div>
<pre><code><span>levels</span><span>(</span><span>gestation</span><span>$</span><span>animal</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  [1] &quot;my thesis&quot; &quot;elephant&quot;  &quot;whale&quot;     &quot;human&quot;     &quot;bear&quot;     
##  [6] &quot;sheep&quot;     &quot;pig&quot;       &quot;tiger&quot;     &quot;wolf&quot;      &quot;cat&quot;      
## [11] &quot;dog&quot;       &quot;mouse&quot;
</code></pre>
</div>
<p>I used <code>forcats::fct_inorder</code> and <code>forcats::fct_rev</code> because in such a post not using <code>forcats</code> would probably have been a deadly sin (and I don’t have 7 lives), but also because I needed the levels to be ordered from longest to shortest gestation time, otherwise the plot would have looked ugly.</p>
<h1>Making the plot</h1>
<p>At the time I used <code>gganimate</code> which I still love, but in the meantime I also fell in love with <code>magick</code> in particular since reading <a target="_blank" rel="nofollow" href="https://rud.is/b/2016/07/27/u-s-drought-animations-with-the-witchs-brew-purrr-broom-magick/">Bob’s post</a>. So I decided to re-write my code with <code>magick</code>. On this blog I already published <a target="_blank" rel="nofollow" href="http://www.masalmon.eu/2017/01/27/catan/">an article featuring both <code>emojifont</code> and <code>gganimate</code></a>.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;ggplot2&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;emojifont&quot;</span><span>)</span><span>
</span><span>load.emojifont</span><span>(</span><span>'OpenSansEmoji.ttf'</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;magick&quot;</span><span>)</span><span>
</span><span>plot_one_gestation</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>gestation_time</span><span>,</span><span> </span><span>gestation</span><span>){</span><span>
</span><span>now_data</span><span> </span><span>&lt;-</span><span> </span><span>filter_</span><span>(</span><span>gestation</span><span>,</span><span> </span><span>~</span><span> </span><span>gestation</span><span> </span><span>&lt;=</span><span> </span><span>gestation_time</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>now_data</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>geom_col</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>animal</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>gestation</span><span>,</span><span>
</span><span>fill</span><span> </span><span>=</span><span> </span><span>color</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>scale_fill_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;grey&quot;</span><span> </span><span>=</span><span> </span><span>&quot;grey30&quot;</span><span>,</span><span>
</span><span>&quot;gold&quot;</span><span> </span><span>=</span><span> </span><span>&quot;darkgoldenrod1&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>geom_text</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>animal</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>gestation</span><span> </span><span>+</span><span> </span><span>45</span><span>,</span><span>
</span><span>label</span><span> </span><span>=</span><span> </span><span>emoji</span><span>(</span><span>label</span><span>)),</span><span>
</span><span>family</span><span>=</span><span>&quot;OpenSansEmoji&quot;</span><span>,</span><span> </span><span>size</span><span>=</span><span>30</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>theme</span><span>(</span><span>axis.text.y</span><span>=</span><span>element_blank</span><span>(),</span><span>
</span><span>axis.ticks</span><span>=</span><span>element_blank</span><span>(),</span><span>
</span><span>text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span>=</span><span>40</span><span>),</span><span>
</span><span>legend.position</span><span>=</span><span>&quot;none&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>ggtitle</span><span>(</span><span>gestation_time</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>scale_x_discrete</span><span>(</span><span>limits</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>gestation</span><span>$</span><span>animal</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>ylim</span><span>(</span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>max</span><span>(</span><span>gestation</span><span>$</span><span>gestation</span><span>)</span><span> </span><span>+</span><span> </span><span>50</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>coord_flip</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>xlab</span><span>(</span><span>&quot;Animal&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>ylab</span><span>(</span><span>&quot;Gestation in days&quot;</span><span>)</span><span> </span><span>outfil</span><span> </span><span>&lt;-</span><span> </span><span>paste0</span><span>(</span><span>&quot;figs/animals_&quot;</span><span>,</span><span> </span><span>gestation_time</span><span>,</span><span> </span><span>&quot;.png&quot;</span><span>)</span><span>
</span><span>ggsave</span><span>(</span><span>outfil</span><span>,</span><span> </span><span>p</span><span>,</span><span> </span><span>width</span><span>=</span><span>5</span><span>,</span><span> </span><span>height</span><span>=</span><span>5</span><span>)</span><span>
</span><span>outfil</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<p>I wanted the last image to be repeated 4 times so that everyone might have time to ponder over my accomplishment. I was celebrating <em>and</em> boasting with this graph.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;purrr&quot;</span><span>)</span><span>
</span><span>c</span><span>(</span><span>unique</span><span>(</span><span>gestation</span><span>$</span><span>gestation</span><span>),</span><span>
</span><span>rep</span><span>(</span><span>max</span><span>(</span><span>gestation</span><span>$</span><span>gestation</span><span>),</span><span> </span><span>3</span><span>))</span><span>%>%</span><span>
</span><span>map</span><span>(</span><span>plot_one_gestation</span><span>,</span><span> </span><span>gestation</span><span> </span><span>=</span><span> </span><span>gestation</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>map</span><span>(</span><span>image_read</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_join</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>image_animate</span><span>(</span><span>fps</span><span>=</span><span>1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_write</span><span>(</span><span>&quot;gestation.gif&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i2.wp.com/github.com/masalmon/masalmon.github.io/blob/master/_source/gestation.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Aren’t these animal emojis adorable? At least as adorable as <code>CatterPlots</code>cats? You can also bring emojis to <code>ggplot2</code> with <a target="_blank" rel="nofollow" href="https://github.com/dill/emoGG"><code>emoGG</code></a> (check out <a target="_blank" rel="nofollow" href="http://livefreeordichotomize.com/2017/02/09/the-prevalence-of-drunk-podcasts/">Lucy’s recent post</a>) so you don’t need to ever leave the comfort of <code>ggplot2</code> for the sake of “c[au]t[e]?ness”.</p>
<p>Disclaimer: I actually starred <code>CatterPlots</code> on Github because I’m an open-minded cat person (married to a cat allergic person!).</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="http://masalmon.github.io/2017/02/18/complot/"> Maëlle</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/0J78aKEjM_8"></a></div>