Spanish R user and solar energy lecturer Oscar Perpiñán Lamigueiro has written a detailed three-part guide to creating beautiful maps and choropleths (maps color-coded with regional data) using the R language. Motivated by the desire to recreate this graphic from the New York Times, Oscar describes how he creates similar high-quality maps using R.
In Part 1, Oscar grabbed some voting data from Spanish elections and shapefiles (detailed map coordinates) from Spain's Official Statistics department (INE) and set to work in R. He then used the maptools package and lattice graphics to create this choropleth of voting patterns in Spain:
In Part 2, he uses the raster package to import pixel-based environmental data from NASA, to create a map that combines population density, land use and land cover in a single map. (Personally, I find it very difficult to interpret such multivariate maps encoded in multiple dimensions of color, but the process is interesting.)
Finally in Part 3, Oscar uses the pxR package to read map data in the PC-Axis format, and the gridSVG package to create an interactive SVG map. If you have a modern browser, you can click the image below, and then annotations will appear as move your mouse over the regions. [Updated: as Oscar explains in the blog, the annotiations require Javascript to work, so I've changed the image below to link to Oscar's annotated version.]
With these examples, you can see how you can get close to the types of maps featured in the New York Times (which, themselves, often involve R in their production). You can find the data and R code used to create these maps at Oscar's blog linked below.
Omnia sunt Communia!: Maps with R (III)
Thanks a lot for this review.
Only a small fix: the graphical methods are based on the lattice package instead of ggplot2 as you wrote.
Cheers,
Oscar.
Posted by: Oscar Perpiñán Lamigueiro | February 24, 2012 at 13:27
Thanks for the correction, Oscar! I've updated the post above.
Posted by: David Smith | February 24, 2012 at 13:31
Show us how you would do statistical tables with R code, like an adverse event table, or any other table that involves many summary and advanced statistics, and lots of cross-categories along the top and left margin of the table.
Note, programming a statistical table is very different from programming a graph, since the two things are not the same.
Robert
http://fivetimesfaster.blogspot.com
Posted by: Robert Wilkins | February 24, 2012 at 15:49
Hi, several links have broken
i.g. Part1 (broken link)(http://procomun.wordpress.com/2012/02/18/maps_with_r_1/)
Part2 (missing link)
Part 3 (Broken link)
Posted by: Julian | February 26, 2012 at 20:31
Hi Julian, the links seems to be working again now, and I added the missing Part 2 link. Thanks!
Posted by: David Smith | February 27, 2012 at 07:52