Site icon R-bloggers

Images as x-axis labels

[This article was first published on R – Irregularly Scheduled Programming, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

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.

There are probably very few cases for which this is technically a good idea (trying to be a featured author on JunkCharts might very well be one of those reasons). Nonetheless, there are at least a couple of requests for this floating around on stackoverflow; here and here for example. I struggled to find any satisfactory solutions that were in current working order (though perhaps my Google-fu has failed me).

The second link there has a working example, but the big update to ggplot2 breaks that pretty strongly; opts was deprecated and now element_text() has a gatekeeper validation routine that prevents any such messing around. The first link 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 ggplot2 can easily have broken the gTree relations), not particularly flexible, and relies on saving intermittent image files for PostScriptTrace to read back in which I’m not a fan of (and couldn’t get to work anyway).

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).

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.

The way to go about making your own is as follows;

  1. Stop and carefully re-evaluate the choices that you’ve made to bring you to this decision. Are you sure? Okay…
  2. Save the images (in the correct factor order) into a list (e.g. pics).
  3. Build your bar graph with categorical x-axis as per normal, using theme() to remove the labels. Save as an object (e.g. g).
  4. Source the function from this gist (at your own risk… copy and paste if you prefer):
  5. devtools::source_gist("1d1bdb00a7b3910d62bf3eec8a77b4a7")
    
    Not Found
  6. Call (or pipe your ggplot object to) the function:
  7. g %>% add_images_as_xlabels(pics)
    
    ## or
    
    add_images_as_xlabels(g, pics)
    
  8. Your image will be re-drawn with your pictures labelling the categories.

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

Not Found

At least a few caveats surround what I did manage to get working, including but not limited to:

I’ve no doubt that there must be a better way to do this, but it’s beyond my understanding of how ggproto works, and I can’t seem to bypass element_text‘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 (@carroll_jono)!

To leave a comment for the author, please follow the link and comment on their blog: R – Irregularly Scheduled Programming.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.