Flag space: a scatter plot of raster images

October 16, 2012

(This article was first published on is.R(), and kindly contributed to R-bloggers)

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

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , , ,

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)