leafletDK is a package that makes it easy to create interactive dynamic leaflet maps based on danish administrative areas.
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.
library(leafletDK) folk1 <- read.csv2("http://api.statbank.dk/v1/data/folk1/CSV?OMR%C3%85DE=*", 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.
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”.
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) %>% addProviderTiles("Stamen.Toner")
You can get a full overview of the available maps (called tiles) on the leaflet-providers preview page