Site icon R-bloggers

Flag space: a scatter plot of raster images

[This article was first published on is.R(), 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.

(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.
https://gist.github.com/3886739

To leave a comment for the author, please follow the link and comment on their blog: is.R().

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.