Drawing a Thematic map of Switzerland with R

While searching for ways to create a thematic map of Switzerland, I came across the impressive website of Timo Grossenbacher. In his blog he describes the construction of a Swiss map using two well-known R packages: ggplot2 and sf.

The spatial structure of the map is based on geo-data of all Swiss municipalities. The Swiss Federal Statistical Office (FSO) makes this geo-data, i.e. shape or polygon data of the municipal boundaries, freely available. Each municipality can be identified by an id defined by the FSO. The id is crucial to join any data with individual municipalities. Whether population data, voting results, or even data about energy consumption, the id allows any data to be mapped to the municipality level.

The topic of the following experiment is deliberately kept very simple: I want to depict into which seas the individual municipalities of Switzerland ultimately drain. Four destinations are available: the North Sea, the Mediterranean Sea, the Adriatic Sea and the Black Sea.

The Geo Data

Let’s have a look at the data. The current geo-shapes for the total of 2’172 Swiss municipalities in 2021 are available under this link. Download and extract the whole zip-file into a folder – named for instance /input – directly on the working directory of the R project (see the R script below).

If you take a closer look at the geo-file for the municipalities (again, the path and the name of this file can be seen in the following R script), you can see in it, besides the actual geo-shape data (i.e. the boundary polygons), for each of the listed municipalities, the id mentioned above as well as the corresponding municipality name.

Besides the municipality data, the download also provides geo-files for the rivers and lakes of Switzerland which we will use as well.

Somewhat special is the fact that the geo-file subtracts from the total area of each municipality those areas that are devoid of vegetation (e.g. glaciers, rocks, boulders, …) as well as those areas that are above 2’000 meters above sea level.

The omitted non-vegetation areas are left blank and are ultimately overlaid with a relief image in tif-format (see header image above). The relief image is originally from swisstopo. Unfortunately, I couldn’t discover the original link to it, so I’ve chosen an alternative URL for downloading this relief file named 02-relief-georef-clipped-resampled.tif.

The Theme Data

I manually created the assignment of the four drainage destinations to the individual Swiss municipalities. Starting point was the mentioned list of municipalities in the FSO geo-dataset. For each municipality identified by id and name I added the runoff destination manually and finally saved the list as a csv-file (named: themeData.csv). The following table shows selected examples of the theme file.

idnameoutflow
4941MärstettenNorth Sea
4946WeinfeldenNorth Sea
4951WigoltingenNorth Sea
5001Arbedo-CastioneAdriatic Sea
5002BellinzonaAdriatic Sea
5003CadenazzoAdriatic Sea
5497PompaplesMediterranean Sea
5498La SarrazMediterranean Sea
5499SenarclensMediterranean Sea
3746ZernezBlack Sea
3752SamnaunBlack Sea
3762ScuolBlack Sea

The R code

Here now is the R script. I took large parts of the script from Timo Grossenbacher’s publication, but also simplified it a lot. We start by including the needed R packages and by reading the downloaded data from the mentioned /input folder. Note the join operation at the end of the following code snippet.

After reading in the data, we define some global constants, e.g. for colors, fonts, etc..

Now we get to the actual map creation. The function theme_list() creates a list of basic theme attributes, such as grid lines, borders, background color, spacings, title and legend texts with position, font, etc..

The overlaying of the individual map layers is done as follows: First, we prepare the relief image and set it as background.

On top of that we put the municipality data with the respective boundaries and the (color) distinguishable variable outflow, which was joined to it via the municipality id and represents the different outflow destinations.

Finally, we embed the layers of lakes and rivers into the map, as well as the design of text elements.

The map is ready so far. In order to name the most important outflows, i.e. outflowing rivers of Switzerland and put them on the map, we use a simple annotation system. For example the Rhine (Rhein), which drains the largest area of Switzerland into the North Sea. Next to it the Doubs, which flows into the Rhône via Saône and finally drains into the Mediterranean. In the south and southeast of Switzerland, five border crossing rivers drain into the Adriatic Sea via the Po. And in the far east of Switzerland, the Inn flows eastwards and meets the Danube (Donau) in Passau, which finally drains into the Black Sea.

A respectable map, I think, which is just waiting to be fed with new data.

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 33 = 36