Flag space: a scatter plot of raster images

(Click for a bigger version)

I’m not sure when plotting raster images in an R scatter plot will end up being useful. It’s not something I expect to need to do in my day-to-day research, and I think in general probably adds more complexity than clarity, when used in “interesting” ways.

That said, if the objects one wants to compare are themselves images (as opposed to nations, voters, consumers, etc.), it may be necessary to plot them. Below is the most interesting example I could think of quickly: plotting national flags by color similarity. (I will admit to being inspired by the graph from this study, which I have to assume is satirical.)

There are a couple of useful things going on in this script:

  1. A very simple web-scrape to get URLs of images for each flag
  2. Downloading each .PNG file and assigning them to a list (thanks to mnel and Ben Bolker at stackoverflow)
  3. Using sapply() on the list of flags to calculate the average red, green, and blue values for all pixels in each.
  4. Using non-metric multidimensional scaling to put similarly-colored flags in similar locations in a two-dimensional space.
  5. Using rasterImage() to plot images of each flag at its 2D color-space coordinates.

By d-sparks

Tags: devtools graphics mass png rstats RCurl