Images as x-axis labels (updated)

They say “if you want to find an answer on the internet, just present a wrong one as fact. Then wait.

It didn’t take long, actually. Despite my searches while trying to get images into x-axis labels it seems I overlooked a working, significantly less hacky implementation. My Google-fu had in fact let me down.

Baptiste Auguié (@tpab / @baptiste) had this working a while ago (seemingly before the ggplot2 update that broke other methods), and in a definitively less hacky way. I’ve added a new gist (if you’re reading this on R-bloggers, the gist isn’t embedded, so either follow the link or view on my site) which implements it on the same graph as earlier, and I like this significantly more.

This method gets around the element_text() validation and updates the grobs in a way that’s above my pay grade/understanding of ggplot2 internals, and is a much more consistent way to go about it. This also:

  • places the factor labels on the graph along with the picture, covering some concerns about people not knowing which maps are for which country,
  • leaves room for the caption to go back in, which I wanted,
  • automatically scales the grob better,
  • doesn’t involve creating an external grob and thus turning off clipping; using axis.text.x is exactly what I was hoping for.

Updated version using @baptiste's method; much better.

Updated version using @baptiste’s method; much better.

My version worked (sort of) but only because it used options that were bad practice (not doubting that for a moment). I’d like to see this method make it into ggplot2 properly; Baptiste had an open GitHub issue involving it a while ago but it has since been closed, presumably without the feature being incorporated.

I started the previous post by saying how awesome open-source software is (e.g. R). You know what else is awesome? The #rstats community. Thank you to every one of you.

15 Responses to “Images as x-axis labels (updated)”

  1. […] article was first published on R – Irregularly Scheduled Programming, and kindly contributed to […]

  2. […] article was first published on R – Irregularly Scheduled Programming, and kindly contributed to […]

  3. Julia Silge says:

    This is so helpful and looks so great; thanks for sharing it!

  4. Great post! Just a short comment on obtaining the flag image files: you actually don’t need to download them beforehand, as `EBImage::readImage()` is capable of opening files directly from URLs.

  5. […] Not everyone agrees. Ggplot2 tech themes, scales, and geoms. Supreme Annotations. Images as x-axis labels. […]

  6. Hi jon, your works is interesting. I have successfully implemented it in shiny, but when implementing it in rmarkdown there is an “error: pungfun invalid width and height”, can you tell me what happens? and what should I do?
    thanks.

  7. zara says:

    Hi, Thanks for the great info and codes.
    I was wondering if we can give “angle=45” for text of X Axis ? I’ve tried to use
    axis.text.x=element_text(angle=45)
    but it’s not working, since axis.text.x is already used for imgs in this part
    axis.text.x = my_axis(imgs).
    Would you please help to merge these and give angle to the label? Thank you

    • I’ve updated the gist (might take a while to reload, but click the filename to go to it directly). my_axis now takes an angle argument. Seems to do what you’re after.

      • zara says:

        Hi, Thanks for the reply, that’s working .
        Is it possible to resize the images? I’ve around 100 images, and it does not fit and the images overlap.
        I found this code,
        im<-pics[[1]]
        resize(im,round(width(im)/10),round(height(im)/10))

        but it does not work and keep showing error

        Error in resize(im, round(width(im)/10), round(height(im)/10)) :
        Expecting a four-dimensional array

        Would you please help?

Leave a Reply