-
Notifications
You must be signed in to change notification settings - Fork 5
/
README.Rmd
131 lines (94 loc) · 4.59 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
123
124
125
126
127
128
129
130
---
output:
md_document:
variant: markdown_github
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
This is a package for making easy maps of Denmark.
Currently, the package allows you to do three things:
1. make basic maps of Denmark
2. turn these maps into (static) choropleth maps
3. plot longitude-latitude points on a map of Denmark
I recommend you install the development version from github, using Hadley Wickham's [devtools](http://cran.r-project.org/web/packages/devtools/index.html) package:
```
if(!require("devtools")) install.packages("devtools")
devtools::install_github("sebastianbarfort/mapDK")
```
To create a basic map of Denmark at the municipality level simply run
```{r, message = FALSE, warning = FALSE}
library(mapDK)
mapDK()
```
You can control the level of the map by specifying the `detail` argument. Currenty, `mapDK` accepts the following arguments
- `municipality` - plots Denmark's 98 municipalities
- `region` - plots Denmark's 5 regions
- `rural` - plots Denmark's 11 rural areas
- `zip` - plots Denmark's 598 zip code areas
- `polling` - plots Denmark's 1385 polling places (as of 2015)
- `parish` - plots Denmark's 1931 parishes
## Choropleth Maps
`mapDK` also allows you to create static choropleth maps using either the package's build in datasets, or new datasets provided by the user.
### Plot Municipality Level Data
To create a static choropleth map at the municipality level we can use some data about burglaries in Denmark (included in the package).
One creates a choropleth map simply by specifying the values and id's (as strings) and the dataset in the call to `mapDK`
```{r, message = FALSE, warning = FALSE}
mapDK(values = "indbrud", id = "kommune", data = crime)
```
If you don't provide names for all municipalities, the function will throw a warning.
## Plot Polling Place Data
You can create cool maps of election results at the polling place level by specifying `detail = "polling"`. The package includes a dataset of Danish general election 2011 results at the polling level. Below, we plot Socialdemokratiets election results in percent at each available polling place in the map dataset
```{r, message = FALSE, warning = FALSE}
mapDK(values = "stemmer", id = "id",
data = subset(votes, navn == "socialdemokratiet"),
detail = "polling", show_missing = FALSE,
guide.label = "Stemmer \nSocialdemokratiet (pct)")
```
Say you only want to plot Socialdemokratiets votes in Copenhagen. That's easily done using the `sub.plot` option
```{r, message = FALSE, warning = FALSE}
mapDK(values = "stemmer", id = "id",
data = subset(votes, navn == "socialdemokratiet"),
detail = "polling", show_missing = FALSE,
guide.label = "Stemmer \nSocialdemokratiet (pct)",
sub.plot = "koebenhavn")
```
## Plot points on a map of Denmark
You can provide your own data set of longitude-latitude points (in WGS84) and plot them on a map of Denmark using the `pointDK` function. Below, I plot a data set of benches in Copenhagen available in the package
```{r, message = FALSE, warning = FALSE}
pointDK(benches, sub = "koebenhavn", point.colour = "red")
```
You can also plot values in your data by specifying a `values` column
```{r, message = FALSE, warning = FALSE}
benches$mydata = 1:nrow(benches) # create values
pointDK(benches, values = "mydata", detail = "polling", sub.plot = "koebenhavn", point.colour = "red",
aesthetic = "colour")
```
## The getID function
The `getID` function allows you to print the map key. Running this before plotting your dataset with `mapDK` is probably a good idea.
`getID` accepts only one argument, `detail`, and using it is as easy as (it returns keys for municipalities if nothing else is specified)
```{r, message = FALSE, warning = FALSE}
getID()[1:10]
```
Say you want the names of the parishes instead, just run `mapDK(detail = "parish")`.
## `mapDK` and `ggmap`
The functions in `mapDK` work nicely with `ggmap`. Below is an example
```{r, message = FALSE, warning = FALSE}
library("ggmap")
library("dplyr")
votes.cph.shape = mapDK::polling %>% filter(KommuneNav == "koebenhavn") %>% left_join(mapDK::votes)
cph.map = ggmap(get_map(location = c(12.57, 55.68),
source = "stamen",
maptype = "toner", crop = TRUE,
zoom = 13))
cph.map +
geom_polygon(data = subset(votes.cph.shape, navn == "socialdemokratiet"),
aes(x = long, y = lat,
group = group, fill = stemmer),
alpha = .75)
```