Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
111 lines (78 sloc) 5.92 KB
---
title: Even more images as x-axis labels
author: Jonathan Carroll
date: 2018-10-16 23:18:32
slug: even-more-images-as-x-axis-labels
categories: [R]
tags: [rstats, ggplot2, tikzDevice]
type: ''
subtitle: ''
image: ''
editor_options:
chunk_output_type: console
---
```{r, setup, include = FALSE}
library(ggplot2)
library(tinytex)
library(tikzDevice)
knitr::opts_chunk$set(
class.output = "bg-success",
class.message = "bg-info text-info",
class.warning = "bg-warning text-warning",
class.error = "bg-danger text-danger"
)
```
This is the last update to this strange saga... I hope.
![Image labels… Photo: http://www.premierpaper.com/shop/custom-labels/](/post/2018-10-16-even-more-images-as-x-axis-labels_files/labels.jpg)
<!--more-->
This is the last update to this strange saga... I hope.
![Image labels… Photo: http://www.premierpaper.com/shop/custom-labels/](/post/2018-10-16-even-more-images-as-x-axis-labels_files/labels.jpg)
Easily two of the most popular posts on my blog are [this one](https://jonocarroll.com.au/2016-06-02-images-as-x-axis-labels.html") and [this one](https://jonocarroll.com.au/2016-06-03-images-as-x-axis-labels-updated.html") describing a couple of ways in which I managed to hack together using an image as a category label in a ggplot.
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.
<div style="width:100%;height:0;padding-bottom:54%;position:relative;"><iframe src="https://giphy.com/embed/bqalUGFYfyHzW" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></div><p><a href="https://giphy.com/gifs/good-hang-breastfeeding-bqalUGFYfyHzW">via GIPHY</a></p>
One of these posts was recently shared again by the amazing [#rstats amplifier Mara Averick](https://twitter.com/dataandme") (if you're not following her on Twitter, you're missing out) and [@baptiste_auguie](https://twitter.com/baptiste_auguie") (the saviour of the previous implementation) mentioned that he had written a 'hack' to get chemical symbols as a categorical axis label using `tikzDevice`. That package leverages $\LaTeX$ (of which I am _very_ familiar, having written my PhD thesis entirely in $\LaTeX$ many moons ago) to treat all of the text in an image as potential $\LaTeX$ commands and produce a working source code which generates the required plot.
The [example code](https://groups.google.com/forum/#!topic/ggplot2/OPhpWtVcwtY) is straightforward enough
```r
options(tikzLatexPackages = c(getOption('tikzLatexPackages'),
"\\usepackage{acide-amine}\n"))
d = data.frame(x=1:10, y=1:10, f=factor(sample(letters[1:2], 10, repl=TRUE)))
p <- qplot(x,y,data=d) + theme_bw() +
theme(plot.margin = unit(c(1, 1, 5, 1), "lines"),
axis.text.x = element_text(size = 12 *
0.8, lineheight = 0.9, vjust = 10)) +
scale_x_continuous(breaks = c(2, 8), labels=c("\\phe{15}", "\\leu{15}"))
tikz("annotation.tex", standAlone=T, width=4, height=4)
print(p)
```
and produces this
![](/post/2018-10-16-even-more-images-as-x-axis-labels_files/annotation.png)
This got me curious, though -- if it can process _arbitrary_ $\LaTeX$, could it process a `\\includegraphics` call?
```{r echo = FALSE}
blogdown::shortcode('tweet', '1050535371241476096')
```
Yes, as it turns out.
<div style="width:100%;height:0;padding-bottom:75%;position:relative;"><iframe src="https://giphy.com/embed/XreQmk7ETCak0" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></div><p><a href="https://giphy.com/gifs/retro-thumbs-up-XreQmk7ETCak0">via GIPHY</a></p>
A quick test showed that it was indeed possible, which only leaves re-implementing the previous posts' images using this method.
[I've done so](https://gist.github.com/jonocarroll/08a1ccff36be628430d768e5b9426e54), and the code isn't particularly shorter than the other method.
```{r, echo = FALSE, results = 'asis'}
cat("{{% gist jonocarroll 08a1ccff36be628430d768e5b9426e54 %}}")
```
Producing nearly the same end result.
![tikzDevice result](/post/2018-10-16-even-more-images-as-x-axis-labels_files/annotation.png)
There are a few differences compared to the previous version(s):
- I had a request for rotating the additional text, which I actually [also updated recently[(https://gist.github.com/jonocarroll/2f9490f1f5e7c82ef8b791a4b91fc9ca#file-images_as_xaxis_labels_updated-r), and it seemed to fit better, so I rotated the labels within the $\LaTeX$ command.
- Since all of the text has been rendered via $\LaTeX$, the fonts are a bit different.
- The rankings have since changed, so I've added an 11th to keep Australia in the list.
The $\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 $\LaTeX$). Lastly, the result of running the `tikz` command is that a `.tex` ($\LaTeX$ source code) file is produced. This isn't quite the plot image file we want. It _does_ however have the commands to generate one. The last steps in the above gist are to process this `.tex` file with $\LaTeX$. Here I used the `tools::texi2dvi` function, but one _could_ also use a `system` command to their $\LaTeX$ installation.
That still only produced a PDF. The last step was to use the `magick` package to convert this into an image.
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 `tikzDevice` works and got to revisit my $\LaTeX$ knowledge.
<br />
<details>
<summary>
<tt>devtools::session_info()</tt>
</summary>
```{r sessionInfo, echo = FALSE}
devtools::session_info()
```
</details>
<br />
You can’t perform that action at this time.