<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/combine-choropleth-data-with-raster-maps-using-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2016/12/swiss-map.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>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. (<a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Raclette">Raclette</a> and sailing are wonderful, but don't count for our purposes.) For this reason, the Swiss Federal Statistical Office publishes the boundaries of the &quot;productive&quot; parts of the municipalities, and as this <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Choropleth_map">choropleth</a> of average age in Swiss municpalities <a target="_blank" rel="nofollow" href="https://timogrossenbacher.ch/2016/12/beautiful-thematic-maps-with-ggplot2-only/">created by Timo Grossenbacher</a> shows, leaving out the non-productive parts leaves us with a very different-looking Switzerland.</p>
<p><a target="_blank" rel="nofollow" href="https://timogrossenbacher.ch/2016/12/beautiful-thematic-maps-with-ggplot2-only/"><img title="Swiss average age" alt="Swiss average age" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d24b47ed970c-800wi" border="0"></a></p>
<p>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.</p>
<p><a target="_blank" rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d24b4840970c-pi"><img title="Swiss-age-final" alt="Swiss-age-final" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d24b4840970c-800wi" border="0"></a></p>
<p>Timo's blog post, <a target="_blank" rel="nofollow" href="https://timogrossenbacher.ch/2016/12/beautiful-thematic-maps-with-ggplot2-only/">Beautiful thematic maps with ggplot2 (only)</a>, 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:</p>
<ul>
<li>To run scripts in a &quot;clean&quot; 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 &quot;<code>R --vanilla</code>&quot;. The <code>--vanilla</code> option prevents R from running any initialization scripts (that might load packages) or loading any objects from a saved workspace.</li>
<li>To import geographic boundaries using the <a target="_blank" rel="nofollow" href="https://www.rdocumentation.org/packages/rgdal/versions/1.2-5/topics/readOGR">readOGR</a> function, and the use of <code>coord_equal</code> to display them as a map without distortion.</li>
<li>To choose a useful color scale for continuous variables (like age) with the <a target="_blank" rel="nofollow" href="https://mran.microsoft.com/package/viridis/">viridis package</a>, and how to discretize them into buckets to improve visibility of regional differences.</li>
<li>To define a ggplot2 theme according to your presentation style guide (here, a light grey background, a specific font, and no grid lines).</li>
<li>Importing a TIFF relief map of Switzerland using the <a target="_blank" rel="nofollow" href="https://www.rdocumentation.org/packages/raster/versions/2.5-8/topics/raster">raster</a> function, and overlaying part of it onto the choropleth by the clever trick of making the non-mountainous parts transparent.</li>
</ul>
<p>For the complete tutorial, including links to the code and data, check out Timo's blog post linked below.</p>
<p>Timo Grossenbacher: <a target="_blank" rel="nofollow" href="https://timogrossenbacher.ch/2016/12/beautiful-thematic-maps-with-ggplot2-only/">Beautiful thematic maps with ggplot2 (only)</a></p>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2016/12/swiss-map.html"> Revolutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/I0U4_xWMxKk"></a></div>