forked from mikkelkrogsholm/leafletDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
122 lines (79 loc) · 4.74 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
## What is this?
[a center for ants!?](https://youtu.be/7ffj8SHrbk0)
leafletDK is a package that makes it easy to create interactive dynamic leaflet maps based on danish administrative areas.
You can map
- municipalities with `municipalityDK()`
- regions with `regionalDK()`
- rural areas with `ruralDK()`
- zip codes with `zipdDK()`
- political constituencies with `constituencyDK()`
- political districts with `districtsDK()`
The package is heavily inspired by the amazing [mapDK](https://github.com/sebastianbarfort/mapDK) package by [Sebastian Barfort](https://twitter.com/sbarfort), which I recommend if you want to create static high quality maps.
Also a special thanks to [Kenneth Rose](https://twitter.com/kennethrose82) for fixing encoding issues on Windows! (I wrote the original package on Linux, where shit just work).
## 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.
```
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)
```
![](nonpackage/readme_plot1.png)
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)
```
![](nonpackage/readme_plot2.png)
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)
```
![](nonpackage/readme_plot3.png)
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.
![](nonpackage/brewer_swatches.png)
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)
```
![](nonpackage/readme_plot4.png)
## 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:
```
getIDs("municipal")
```
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")
```
![](nonpackage/readme_plot4.png)
You can get a full overview of the available maps (called tiles) on the [leaflet-providers preview page](http://leaflet-extras.github.io/leaflet-providers/preview/)