-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME.Rmd
251 lines (165 loc) · 7.41 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
# datenguideR <img src='man/figures/logo.png' align="right" height="139" />
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.width = 6,
fig.height = 4,
fig.path = "man/figures/README-",
out.width = "100%"
)
```
<!-- badges: start -->
[![Build_Status](https://travis-ci.org/CorrelAid/datenguideR.svg?branch=master)](https://travis-ci.org/CorrelAid/datenguideR)
[![Codecov test coverage](https://codecov.io/gh/CorrelAid/datenguideR/branch/master/graph/badge.svg)](https://codecov.io/gh/CorrelAid/datenguideR?branch=master)
[![Licence](https://img.shields.io/badge/licence-GPL--3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.en.html)
[![Open Source Love](https://badges.frapsoft.com/os/v2/open-source.png?v=103)](https://github.com/ellerbrock/open-source-badges/)
<!-- badges: end -->
Access and download German regional statistics from Datenguide <http://datengui.de>. `datenguideR` provides a wrapper for their GraphQL API and also includes metadata for all available statistics and regions.
**Overview**
+ [Usage](https://github.com/CorrelAid/datenguideR#usage)
+ [Examples](https://github.com/CorrelAid/datenguideR#examples)
+ [Search meta data (`dg_search`)](https://github.com/CorrelAid/datenguideR#dg_search)
+ [Main function (`dg_call`)](https://github.com/CorrelAid/datenguideR#dg_call)
+ [Multiple regions (at once)](https://github.com/CorrelAid/datenguideR#multiple-regions-with-nuts_nr-or-lau_nr)
+ [Plot data on maps (`dg_map`)](https://github.com/CorrelAid/datenguideR#dg_map)
+ [Use Cases](https://github.com/CorrelAid/datenguideR#use-cases)
+ [Credits and Acknowledgements](https://github.com/CorrelAid/datenguideR#credits-and-acknowledgements)
+ [Code of Conduct](https://github.com/CorrelAid/datenguideR#code-of-conduct)
## Usage
First, install `datenguideR` from GitHub:
```{r, eval=F}
devtools::install_github("CorrelAid/datenguideR")
```
Load package:
```{r}
library(datenguideR)
```
## Examples
Get IDs of all available NUTS-1 regions:
```{r}
datenguideR::dg_regions %>%
dplyr::filter(level == "nuts1") %>%
knitr::kable()
```
Get all available meta data on statistics, substatistics, and parameters:
```{r}
datenguideR::dg_descriptions
```
### `dg_search`
You can also use `dg_search` to look for a variable of interest. The function will match your string with any strings in the `dg_descriptions` data frame, returning only rows with those matches.
Looking for variables where the string *"vote"* appears somewhere in the documentation:
```{r}
dg_search("vote")
```
Note: Descriptions of variables are also available in English now! Translated via the [`googleLanguageR`](https://github.com/ropensci/googleLanguageR) package.
```{r}
dg_search("vote") %>%
dplyr::select(stat_name, dplyr::contains("_en"))
```
### `dg_call`
The main function of the package is `dg_call`. It gives access to all API endpoints.
Simply pick a statistic and put it into `dg_call()` (infos can be retrieved from `dg_descriptions`).
For example:
- `stat_name`: AI0506 *(Wahlbeteiligung, Bundestagswahl)*
- `region_id`: 11 (stands for Berlin)
```{r}
dg_call(region_id = "11",
year = 2017,
stat_name = "AI0506")
```
A slightly more complex call with substatistics:
- `stat_name`: BETR08 *(Landwirtschaftliche Betriebe mit Tierhaltung)*
- `substat_name`: TIERA8 *(Landwirtschaftliche Betriebe mit Viehhaltung)*
- `parameter`:
- TIERART2 *(Rinder)*
- TIERART3 *(Schweine)*
```{r}
dg_call(region_id = "11",
year = c(2001, 2003, 2007),
stat_name = "BETR08",
substat_name = "TIERA8",
parameter = c("TIERART2", "TIERART3"))
```
If you give no parameters for a substat, it will default to return results for all of them.
```{r}
dg_call(region_id = "11",
year = c(2001, 2003, 2007),
stat_name = "BETR08",
substat_name = "TIERA8")
```
### Multiple regions with `nuts_nr` or `lau_nr`
Instead of specifying a `region_id` for individual Bundesland (state) data you can also use `nuts_nr` to receive data for *NUTS-1*, *NUTS-2* and *NUTS-3*.
Just leave `region_id` blank and provide either a `nuts_nr` (or `lau_nr`) to get data for multiple regions at once.
- `stat_name`: AI0506 *(Wahlbeteiligung, Bundestagswahl)*
```{r}
dg_call(nuts_nr = 1,
year = 2017,
stat_name = "AI0506")
```
- `stat_name`: BETR08 *(Landwirtschaftliche Betriebe mit Tierhaltung)*
- `substat_name`: TIERA8 *(Landwirtschaftliche Betriebe mit Viehhaltung)*
```{r}
dg_call(nuts_nr = 1,
year = c(2001, 2003, 2007),
stat_name = "BETR08",
substat_name = "TIERA8")
```
- `stat_name`: BAU018 *Total non-residential buildings*
- `substat_name`: BAUAHZ *Main Type of Heating*
```{r}
dg_call(nuts_nr = 1,
stat_name = "BAU018",
substat_name = "BAUAHZ",
year = 2016)
```
<!-- ```{r} -->
<!-- dg_call(nuts_nr = 2, -->
<!-- stat_name = "GEBWOR", -->
<!-- substat_name = "BAUAT2") -->
<!-- ``` -->
<!-- # ```{r} -->
<!-- # library(datenguideR) -->
<!-- # debugonce(datenguideR:::add_substat_info) -->
<!-- # dg_call(lau_nr = 1, parent_chr = 10041, -->
<!-- # stat_name = "BAU018", -->
<!-- # substat_name = "BAUAHZ",) -->
<!-- # ``` -->
### `dg_map`
You can also use `dg_map` to plot retrieved data on a Germany map. This currently only supports NUTS-1 and NUTS-2. Arguments are (mostly) identical to `dg_call`.
```{r}
dg_map(nuts_nr = 1,
year = 2017,
stat_name = "AI0506")
```
The output is a ggplot object and can be manipulated further.
```{r}
dg_map(nuts_nr = 1,
year = 2017,
stat_name = "AI0506") +
ggthemes::theme_map() +
ggplot2::scale_fill_viridis_c("Voter Turnout") +
ggplot2::ggtitle("Voter Turnout in German Parliamentary Election (2017)") +
ggplot2::theme(legend.position = "right")
```
You can also return the data and use your own plotting functions with `return_data = TRUE`.
```{r}
turnout_dat <- dg_map(nuts_nr = 1,
year = 2017,
stat_name = "AI0506",
return_data = T)
turnout_dat
```
## Use Cases
Check out some use cases. For example here:
+ [Correlaid Meetup - datenguideR workshop](https://github.com/TripLLL/weRnuts3)
## Credits and Acknowledgements
datenguideR builds on the amazing work of [Datenguide](https://datengui.de/) and their [GraphQL API](https://github.com/datenguide/datenguide-api). We especially thank [Simon Jockers](https://twitter.com/sjockers), [Simon Wörpel](https://twitter.com/simonwoerpel), and [Christian Rijke](https://twitter.com/crijke) for their constructive feedback, helpful comments, and overall support while developing the package.
The data is retrieved via the Datenguide API from the German Federal Statistical Office and the statistical offices of the German states. Data being used via this package has to be credited according to the [Datenlizenz Deutschland – Namensnennung – Version 2.0](https://www.govdata.de/dl-de/by-2-0).
This package was created with [devtools](https://github.com/r-lib/devtools), [usethis](https://github.com/r-lib/usethis), and [roxygen2](https://github.com/r-lib/roxygen2). Continuous integration was done with [Travis CI](https://travis-ci.org/).
## Code of Conduct
datenguideR is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.