<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/even-more-images-as-x-axis-labels/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://jcarroll.com.au/2018/10/16/even-more-images-as-x-axis-labels/"> R – Irregularly Scheduled Programming</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This is the last update to this strange saga… I hope.</p>
<p><div class="wp-caption"><img src="https://i0.wp.com/jcarroll.com.au/wp-content/uploads/2018/10/labels.jpg?w=680&ssl=1" data-recalc-dims="1">
<p class="wp-caption-text">Image labels… Photo: http://www.premierpaper.com/shop/custom-labels/</p>
</div>
<p><span></span></p>
<p>Easily two of the most popular posts on my blog are <a rel="nofollow" href="https://jcarroll.com.au/2016/06/02/images-as-x-axis-labels/" target="_blank">this one</a> and <a rel="nofollow" href="https://jcarroll.com.au/2016/06/03/images-as-x-axis-labels-updated/" target="_blank">this one</a> describing a couple of ways in which I managed to hack together using an image as a category label in a ggplot. </p>
<p>There are likely many people who believe one should <em>never</em> do such a thing, but given the popularity, it seems a lot of people aren’t listening to that. Good on you.</p>
<div><iframe allowfullscreen src="https://giphy.com/embed/bqalUGFYfyHzW" width="100%" height="100%"></iframe></div>
<p><a rel="nofollow" href="https://giphy.com/gifs/good-hang-breastfeeding-bqalUGFYfyHzW" target="_blank">via GIPHY</a></p>
<p>One of these posts was recently shared again by the amazing <a rel="nofollow" href="https://twitter.com/dataandme" target="_blank">#rstats amplifier Mara Averick</a> (if you’re not following her on Twitter, you’re missing out) and <a rel="nofollow" href="https://twitter.com/baptiste_auguie" target="_blank">@baptiste_auguie</a> (the saviour of the previous implementation) mentioned that he had written a ‘hack’ to get chemical symbols as a categorical axis label using <code>tikzDevice</code>. That package leverages <img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> (of which I am <i>very</i> familiar, having written my PhD thesis entirely in <img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> many moons ago) to treat all of the text in an image as potential <img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> commands and produce a working source code which generates the required plot.</p>
<p>The <a rel="nofollow" href="https://groups.google.com/forum/#!topic/ggplot2/OPhpWtVcwtY" target="_blank">example code</a> is straightforward enough</p>
<pre>
options(tikzLatexPackages = 
c(getOption('tikzLatexPackages'),&quot;\\usepackage{acide-amine}\n&quot;)) 

d = data.frame(x=1:10, y=1:10, f=factor(sample(letters[1:2], 10, repl=TRUE))) 

p &lt;- qplot(x,y,data=d) + theme_bw() + 
  opts(plot.margin = unit(c(1, 1, 5, 1), &quot;lines&quot;), 
       axis.text.x = theme_text(size = 12 * 
        0.8, lineheight = 0.9, vjust = 10)) + 
  scale_x_continuous(breaks = c(2, 8), labels=c(&quot;\\phe{15}&quot;, &quot;\\leu{15}&quot;)) 

tikz(&quot;annotation.tex&quot;,standAlone=T,width=4,height=4) 
print(p) 
dev.off() 
</pre>
<p>and produces this</p>
<p><div class="wp-caption"><img src="https://i2.wp.com/jcarroll.com.au/wp-content/uploads/2018/10/annotation.png?w=680&ssl=1" data-recalc-dims="1">
<p class="wp-caption-text">annotation.png</p>
</div>
<p>This got me curious, though — if it can process <i>arbitrary</i> <img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex">, could it process a <code>\\includegraphics</code> call?</p>
<blockquote class="twitter-tweet" data-lang="en">
<p lang="en" dir="ltr">Efficient! If it's arbitrary LaTeX, could the labels just be \includegraphics calls?</p>
<p>— Jonathan Carroll (@carroll_jono) <a rel="nofollow" href="https://twitter.com/carroll_jono/status/1050535371241476096?ref_src=twsrc%5Etfw" target="_blank">October 11, 2018</a></p>
</blockquote>
<p></p>
<p>Yes, as it turns out. </p>
<div><iframe allowfullscreen src="https://giphy.com/embed/XreQmk7ETCak0" width="100%" height="100%"></iframe></div>
<p><a rel="nofollow" href="https://giphy.com/gifs/retro-thumbs-up-XreQmk7ETCak0" target="_blank">via GIPHY</a></p>
<p>A quick test showed that it was indeed possible, which only leaves re-implementing the previous posts’ images using this method.</p>
<p><a rel="nofollow" href="https://gist.github.com/jonocarroll/08a1ccff36be628430d768e5b9426e54" target="_blank">I’ve done so</a>, and the code isn’t particularly shorter than the other method.</p>
<p>
<div>
<div><div class="feedlyNoScript"><code>
<pre>404: Not Found
</pre>
<p>

<p>Producing nearly the same end result.</p>
<p><div class="wp-caption"><img src="https://i0.wp.com/jcarroll.com.au/wp-content/uploads/2018/10/xaxis.png?w=680&ssl=1" data-recalc-dims="1">
<p class="wp-caption-text">tikzDevice result</p>
</div>
<p>There are a few differences compared to the previous version(s):</p>
<p> – I had a request for rotating the additional text, which I actually <a rel="nofollow" href="https://gist.github.com/jonocarroll/2f9490f1f5e7c82ef8b791a4b91fc9ca#file-images_as_xaxis_labels_updated-r" target="_blank">also updated recently</a>, and it seemed to fit better, so I rotated the labels within the <img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> command.<br>
– Since all of the text has been rendered via <img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex">, the fonts are a bit different.<br>
– The rankings have since changed, so I’ve added an 11th to keep Australia in the list.</p>
<p>The <img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> component of this also meant that a few changes were necessary in the other labels, such as the dollar sign in the y-axis label, and the underscores throughout (these are considered special characters in <img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex">). Lastly, the result of running the <code>tikz</code> command is that a <code>.tex</code> (<img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> source code) file is produced. This isn’t quite the plot image file we want. It <i>does</i> however have the commands to generate one. The last steps in the above gist are to process this <code>.tex</code> file with <img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex">. Here I used the <code>tools::texi2dvi</code> function, but one <i>could</i> also use a <code>system</code> command to their <img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> installation.</p>
<p>That still only produced a PDF. The last step was to use the <code>magick</code> package to convert this into an image.</p>
<p>Overall, this is a nice proof of concept, but I don’t think it’s a particularly tidy way of achieving the goal of image axis labels. It does however lay the groundwork for anyone else who decides this might be a useful route to take. Plus I learned a bit more about how <code>tikzDevice</code> works and got to revisit my <img alt="LaTeX" title="LaTeX" src="https://s0.wp.com/latex.php?latex=%5CLaTeX&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> knowledge.</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://jcarroll.com.au/2018/10/16/even-more-images-as-x-axis-labels/"> R – Irregularly Scheduled Programming</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/TZbafZlBmXo" height="1"></a></div></code></div></div></div>