knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)

nlmaps

The goal of nlmaps is to create choropleths of the Netherlands. Typically in R it is difficult to create choropleths. The functions presented here attempt to elegantly solve this problem.

The common approach is to first aggregate the data on the level of the regions in the shapefile and then merging the aggregated data with the shapefile. This is done by joining on the name. This approach is often problematic. For example, in case of municipality names in the Netherlands it is not easy to merge municipality names in the shapefile with the names in the data set. This is hard because of municipal reorganizations or differences in punctuation marks in municipality names. To solve this problem the data is not aggregated on the level of the regions in the shapefile. Contrary, the functions in this package detect directly the region containing the coordinates of the underlying object. This flexible approach makes it easy to create choropleth maps on different region levels.

The package has the following build-in choropleth maps:

Installation

You can install nlmaps from github with:

# install.packages("devtools")
devtools::install_github("MHaringa/nlmaps")

Example 1

The insurance dataset contains 30,000 postal codes with their sum insured, population and the corresponding longitude and latitude. The following code shows how to create a simple feature object on the 2-digit postcode level. The regions are shaded by the total sum insured per region.

library(nlmaps)
test <- choropleth_sf(nl_postcode3, insurance, sum(amount, na.rm = TRUE))
test

The following code shows how to create a choropleth map based on the simple feature object obtained in the previous step. There are three options to create a choropleth map. The first one is using the ggplot2 package:

choropleth_ggplot2(test)

The second approach is creating a choropleth map using the tmap package:

choropleth_tmap(test, mode = "plot")

The third option is to create an interactive map. This can also be achieved using the tmap package.

choropleth_tmap(test, mode = "view")

Example 2

It may be of interest to determine the average sum insured per inhabitant. To determine this average the total amount per region is divided by the population per region. This is shown in the following example on the municipality level:

test2 <- choropleth_sf(nl_postcode1, insurance, sum(amount, na.rm = TRUE) / sum(population_pc4, na.rm = TRUE))
choropleth_tmap(test2)


MHaringa/nlmaps documentation built on May 19, 2019, 9:40 p.m.