(This article was first published on

**is.R()**, and kindly contributed to R-bloggers)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:

- A very simple web-scrape to get URLs of images for each flag
- Downloading each .PNG file and assigning them to a list (thanks to mnel and Ben Bolker at stackoverflow)
- Using sapply() on the list of flags to calculate the average red, green, and blue values for all pixels in each.
- Using non-metric multidimensional scaling to put similarly-colored flags in similar locations in a two-dimensional space.
- 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 his blog:**is.R()**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...