<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/images-as-x-axis-labels/"></div></div><div><a data-url="http://www.r-bloggers.com/images-as-x-axis-labels/" data-counturl="http://www.r-bloggers.com/images-as-x-axis-labels/" data-count="horizontal" data-text="Images as x-axis labels" 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://jcarroll.com.au/2016/06/02/images-as-x-axis-labels/"> R – Irregularly Scheduled Programming</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Open-source software is awesome. If I found that a piece of closed-source software was missing a feature that I wanted, well, bad luck. I probably couldn’t even tell if was actually missing or if I just didn’t know about it. When the source is available, maintained, and documented however, things get fun. We can identify, and perhaps fill gaps.</p>
<p><span></span></p>
<p>I’ve thought for a couple of projects which had bar-graphs that it would be neat to have the categories labelled by an icon or a picture. Say, the logo for a company or an illustrative example. Sure, you could fire up GIMP/Inkscape and manually insert them over the top of the text labels (each and every time you re-produce the graph… no thanks) but that’s not how I operate. </p>
<p>There are probably very few cases for which this is technically a good idea (trying to be a featured author on <a target="_blank" href="http://junkcharts.typepad.com/">JunkCharts</a> might very well be one of those reasons). Nonetheless, there are at least a couple of requests for this floating around on stackoverflow; <a target="_blank" href="http://stackoverflow.com/questions/29939447/icons-as-x-axis-labels-in-r-ggplot2">here</a> and <a target="_blank" href="http://stackoverflow.com/questions/8905101/how-can-i-use-a-graphic-imported-with-grimport-as-axis-tick-labels-in-ggplot2-u">here</a> for example. I struggled to find any satisfactory solutions that were in current working order (though perhaps my Google-fu has failed me).</p>
<p>The <a target="_blank" href="http://stackoverflow.com/questions/8905101/how-can-i-use-a-graphic-imported-with-grimport-as-axis-tick-labels-in-ggplot2-u">second link there</a> has a working example, but the big update to <code>ggplot2</code> breaks that pretty strongly; <code>opts</code> was deprecated and now <code>element_text()</code> has a gatekeeper validation routine that prevents any such messing around. The <a target="_blank" href="http://stackoverflow.com/questions/29939447/icons-as-x-axis-labels-in-r-ggplot2">first link</a> however takes a different route. I couldn’t get that one to work either, but in any case the answer is a year out of date (updates in <code>ggplot2</code> can easily have broken the <code>gTree</code> relations), not particularly flexible, and relies on saving intermittent image files for <code>PostScriptTrace</code> to read back in which I’m not a fan of (and couldn’t get to work anyway).</p>
<p>I decided that I perhaps had enough ammunition to hack something together myself (emphasis on hack), and sure enough it seems to have worked (for a limited definition of “worked” with no attached or implied guarantees whatsoever).</p>
<div class="wp-caption"><a target="_blank" href="http://i2.wp.com/jcarroll.com.au/wp-content/uploads/2016/06/GDP.png"><img alt="GDP per capita with flags for x-axis labels. This is harder to make than it seemed." class="wp-image-939" src="http://i2.wp.com/jcarroll.com.au/wp-content/uploads/2016/06/GDP.png?resize=680%2C485" data-recalc-dims="1"></a>
<p class="wp-caption-text">GDP per capita with flags for x-axis labels. This was harder to make than it seemed, but I’ve since added a little more flexibility to it.</p>
</div>
<p>The way to go about making your own is as follows;</p>
<ol>
<li> Stop and carefully re-evaluate the choices that you’ve made to bring you to this decision. Are you sure? Okay…</li>
<li> Save the images (in the correct factor order) into a list (e.g. <code>pics</code>).</li>
<li> Build your bar graph with categorical x-axis as per normal, using <code>theme()</code> to remove the labels. Save as an object (e.g. <code>g</code>).</li>
<li> Source the function <a target="_blank" href="https://gist.github.com/jonocarroll/1d1bdb00a7b3910d62bf3eec8a77b4a7">from this gist</a> (at your own risk… copy and paste if you prefer): </li>
<pre>
devtools::source_gist(&quot;1d1bdb00a7b3910d62bf3eec8a77b4a7&quot;)
</pre>

<div>
<div><div class="feedlyNoScript"><code>
<pre>Not Found</pre>
<p>

<li> Call (or pipe your <code>ggplot</code> object to) the function: </li>
<pre>
g %>% add_images_as_xlabels(pics)

## or

add_images_as_xlabels(g, pics)
</pre>
<li> Your image will be re-drawn with your pictures labelling the categories.</li>

<p>Here’s an example of the code used to generate the GDP per capita image, featuring some fairly brief (for what it does) <code>rvest</code> scraping (to reiterate; I don’t want to have to do any of this by hand, so let’s code it up!).</p>

<div>
<div><div class="feedlyNoScript"><code>
<pre>Not Found</pre>
<p>

<p>At least a few caveats surround what I did manage to get working, including but not limited to:</p>
<ul>
<li> I’m not sure how to put the x-axis title back in at the right position without padding it with a lot of linebreaks (<code>&quot;nnnnX-AXIS TITLE&quot;</code>).</li>
<li> I’m not sure how to move the <code>caption</code> line from <code>labs()</code> (assuming you’re using the development version of <code>ggplot2</code> on GitHub with @hrbrmstr’s excellent annotation additions) so it potentially gets drawn over.</li>
<li> The spacing below the graph is currently arbitrarily set to a few lines more than necessary, but it’s a compromise in having an arbitrary number of images loaded at their correct sizes.</li>
<li> Similarly, I’ve just expanded the plot range of the original graph by a seemingly okay amount which has worked for the few examples I’ve tried.</li>
<li> Using a graph like this places the onus of domain knowledge onto the reader; if you don’t know what those flags refer to then this graph is less useful than one with the countries labelled with words. Prettier though.</li>
</ul>
<p>I’ve no doubt that there must be a better way to do this, but it’s beyond my understanding of how <code>ggproto</code> works, and I can’t seem to bypass <code>element_text</code>‘s requirements with what I do know. If you would like to help develop this into something more robust then I’m most interested. Given that it’s a single function I wasn’t going to create a package just for this, but I’m willing to help incorporate it into someone’s existing package. Hit the comments or ping me on Twitter (<a target="_blank" href="https://twitter.com/carroll_jono">@carroll_jono</a>)!</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="http://www.r-bloggers.com/images-as-x-axis-labels/"></div></div><div><a data-url="http://www.r-bloggers.com/images-as-x-axis-labels/" data-counturl="http://www.r-bloggers.com/images-as-x-axis-labels/" data-count="vertical" data-text="Images as x-axis labels" 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://jcarroll.com.au/2016/06/02/images-as-x-axis-labels/"> R – Irregularly Scheduled Programming</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/gkMkqg7RIlI"></a></div></code></div></div></div></code></div></div></div></ol>