The Netherlands… a tiny, cozy and rainy country. A person coming to that beautiful place will immediately realize a frequent problem: a place to live, or also known as permanent accommodation. To be honest, this problem appears to most of the people who come to the Netherlands. After all, finding a house is hard, and finding a bit better house among the others is harder. At the end of the day, who does not want a ‘gezellig’ place?
Today, we are going to make a regional level housing map of the Netherlands in the form of choropleth. Despite the disadvantages of choropleth maps such as giving a false impression on the regional borders where boundaries are abruptly changed, they are nice to present visual communication establishing with shaded regions with regard to a defined key such as gradient color revealing different value ranges1.
Making a regional level map is not a hard task with R, especially with the given cleaned data. As you know that today many platforms offer open data for the public use - as there is an abundance of data, but we cannot say the same for the analysis of data. Though, it’s a good initiative to open data to all, when so many analysts can interpret it and try to turn them something meaningful.
In this example, the dataset offered from cbs.nl is used that requires a little bit data cleaning. However there is no parsing or heavy data cleaning done. We extract the house owning and house rental data. Metadata depicts that “huurwoningen” is the rental house whose property owner does not live in. “Eigenwoningen” is the homes whose owner is also the resident or one of the residents.
To create the map, you need the following packages for R :
I personally prefer to load packages in this way, which looks more cluttered than running the library function in multiple lines.
tidyverse is the tidy approach which absolutely changed the destiny of data analysis in R. We require all its connected packages.
sp package provides the tools we need to acquire and control the spatial data.
RColorBrewer offers a nice looking color palette, particularly for maps.
Import and massage the data
Data can be found here. As said, data preparation gets the 80% of work. Though, our job is not going to be so long! Let’s import the data first:
Now coding the province names from the data, which are actually obtained from the metadata file attached to the same folder of main data document. By the way, the code in this blog post depicts the data from rental column, the value can be changed for analysing different values - if needed.
Now we choose and assign variables from the data frame. It is vital to extract the rows related with the provinces. A simple for loop will do this work.
After detecting some symbol situated in the values of rows (from
levels(huisdata$value)), we come back to metadata file to see what should be done.
- Symbol Declaration (Verklaring van symbolen):
- Nothing (blank): the number can not occur on logical grounds (het cijfer kan op logische gronden niet voorkomen).
- . : The grade is unknown, insufficiently reliable or secret (het cijfer is onbekend, onvoldoende betrouwbaar of geheim).
- In this table, all numbers are rounded to hundreds (in deze tabel zijn alle getallen afgerond op honderdtallen).
So before that, we transform the ‘.’ (dot) character into numerical value as zero.
Shape the map
Second, we look our map form. GADM (Global Administrative Areas) offers spatial data of administrative areas and boundaries of the whole world. There are three levels available, which we use Level 1 that presents the regional areas.
.rds file downloaded from GADM with
readRDS function, and transform it into data frames with
fortify to use in plotting later on.
As we have data frame, we cannot learn the levels of the id column by using
levels function. We use
unique function extracting unique elements. And, there are 14 regions in total, and only 12 provinces are available. 2 provinces which are “Zeeuwse meren” and “IJsselmeer” are out of our context. Well, there are no built housing there yet, as far as I know!
Computing the summary of value column with regards to the regions mentioned in the id column. Then, a little attention should be pointed out here. For merging, the data frames should have the same number of rows, and one common name column between these two data frames.
Find the centroids of provinces, and acquire names and id according to provinces.
Here, we don’t delete the unwanted rows by numeric order of data frame, as it may be changed in the future and will cost us trouble. We choose
ID_1 property for removing action.
ggplot2 is very convenient to build the graph. Adding continious value to the
fill is feasible as we used gradient color scales instead of discrete (then, we would need to
cut it). Following that, the
sourcesign variable is used for the caption as that the italic font can be emphasized with
expression function assigned to an independent variable, and called from the
Contrary to what is believed, achieving a nice looking visualization without rambling the aim of the message is not an easy task. It may be needed to think more, especially the data, for not creating a dull and redundant map. At least, the tutorial has reached its purpose.
Remarks and Reference
Check the source code in my GitHub repository.
: Choropleth maps. (2017, January 27). Barcelona Field Studies Centre. Retrieved from http://geographyfieldwork.com/DataPresentationMappingTechniques.htm