What is this?
leafletDK is a package that makes it easy to create interactive dynamic leaflet maps based on danish administrative areas.
You can map
- municipalities with
- regions with
- rural areas with
- zip codes with
- political constituencies with
- political districts with
Also a special thanks to Kenneth Rose for fixing encoding issues on Windows! (I wrote the original package on Linux, where shit just work).
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 <- readr::read_csv2("http://api.statbank.dk/v1/data/folk1a/CSV?OMR%C3%85DE=*")
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.
Coloring the map
If you are not happy with the standard colorpalette (Red and Yellows) then you can change it with the
pal argument. You can use any of the palette names in the overview below.
As we saw above, Copenhagen municipality has a lot of people. That means that the area "eats up" most of the color so to say. We can change this by setting the
logcol argument to
TRUE. By doing this we use the log value to color by and not the real value.
Lets try it. Lets map the data again but this time we set the colors to "Blues" and we use the log value to color by.
municipalityDK("INDHOLD", "OMRÅDE", data = folk1, pal = "Blues", logcol = T)
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) %>% addProviderTiles("Stamen.Toner")
You can get a full overview of the available maps (called tiles) on the leaflet-providers preview page