Combine choropleth data with raster maps using R

[This article was first published on Revolutions, 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.

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.

Swiss average age

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

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)