Map danish administrative areas with leaflet

[This article was first published on Renglish – 56north | Skræddersyet dataanalyse, 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.

What is this?

a center for ants!?

leafletDK is a package that makes it easy to create interactive dynamic leaflet maps based on danish administrative areas.

The package is heavily inspired by the amazing mapDK package by Sebastian Barfort, which I recommend if you want to create static high quality maps.

Getting started

First you need to install the package from github. You do this by running:

devtools::install_github("56north/leafletDK") # install devtools if needed

After install it is really easy to use leafletDK. Simply call the administrative area that you want to map (like a municipality) and give it the data you want the map to be colored by.

Below is an example where we load data from Statistics Denmark and map it using leaflet.

First we load the package and get the most recent population count for Denmark via the API from Statistics Denmark.


folk1 <- read.csv2("*",
                   stringsAsFactors = F)

Now we have a data frame with three columns. “OMRÅDE” is the area, “TID” is time/date and “INDHOLD” is the people count. We will use the “OMRÅDE” and “INDHOLD” columns to call the municipalityDK function.

municipalityDK("INDHOLD", "OMRÅDE", data = folk1)

As default leafletDK plots the map without an underlying map. You can turn this on by supplying the parameter map = T. You can also turn on the legend with legend = T.

municipalityDK("INDHOLD", "OMRÅDE", data = folk1, map = T, legend = T)

This generates a map of Denmark where the 98 municipalities are colores according to the amount of people that live in each on. It becomes immediately apparent that a lot of people are living in Copenhagen municipality… a lot of people!

We can also zoom in the map on only a few select municipalities by selecting them with the subplot parameter. Lets take a look at Copenhagen (København), Frederiksberg and Hvidovre municipalities:

municipalityDK("INDHOLD", "OMRÅDE", 
                subplot = c("københavn", "frederiksberg", "hvidovre"), 
                data = folk1)

This generates a map with only our three chosen municipalities. If you click on one of the areas, a little popup appears with the mapped values.

Getting ids

If you are in doubt of what ids are being used to generate the maps, then you can use the getIDs function to see a list. If we want to get a list of the municipalities we will do the following:


The getIDs function accepts the following areas: “constituency”, “district”, “municipal”, “parish”, “regional”, “rural” or “zip”.

Changing the underlying map

You can change the underlying map by using the addProviderTiles() from the leaflet package. You can pipe the mapped areas directly to the function like this:

municipalityDK("INDHOLD", "OMRÅDE", 
                subplot = c("københavn", "frederiksberg", "hvidovre"), 
                data = folk1) %>% 

You can get a full overview of the available maps (called tiles) on the leaflet-providers preview page

To leave a comment for the author, please follow the link and comment on their blog: Renglish – 56north | Skræddersyet dataanalyse. 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)