Friday, June 3, 2016

Images as x-axis labels

полезные кунштюки из Р:
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.

No comments: