Combine choropleth data with raster maps using R

By David Smith

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

Switzerland is a country with lots of mountains, and several large lakes. While the political subdivisions (called municipalities) cover the high mountains and lakes, nothing much of economic interest happens in these places. (Raclette and sailing are wonderful, but don’t count for our purposes.) For this reason, the Swiss Federal Statistical Office publishes the boundaries of the “productive” parts of the municipalities, and as this choropleth of average age in Swiss municpalities created by Timo Grossenbacher shows, leaving out the non-productive parts leaves us with a very different-looking Switzerland.

The choropleth would be more recognizable by filling in the non-productive areas with a traditional relief map, which is exactly what Timo does (along with breaking the age scale into discrete categories, for improved interpretability) in the publication-quality map below.

Timo’s blog post, Beautiful thematic maps with ggplot2 (only), details the process of building maps like this using the ggplot2 package (and just a few others) for R. There are lots of useful nuggets of advice within the tutorial, including:

  • To run scripts in a “clean” R session, to avoid conflicts with packages and objects that happen to be hanging around. Timo suggests unloading packages and removing objects, but a quicker and easier way is to simply launch a new R session with “R --vanilla“. The --vanilla option prevents R from running any initialization scripts (that might load packages) or loading any objects from a saved workspace.
  • To import geographic boundaries using the readOGR function, and the use of coord_equal to display them as a map without distortion.
  • To choose a useful color scale for continuous variables (like age) with the viridis package, and how to discretize them into buckets to improve visibility of regional differences.
  • To define a ggplot2 theme according to your presentation style guide (here, a light grey background, a specific font, and no grid lines).
  • Importing a TIFF relief map of Switzerland using the raster function, and overlaying part of it onto the choropleth by the clever trick of making the non-mountainous parts transparent.

For the complete tutorial, including links to the code and data, check out Timo’s blog post linked below.

Timo Grossenbacher: Beautiful thematic maps with ggplot2 (only)

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

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…

Source:: R News