knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4, fig.path = "man/figures/README-", out.width = "100%" )
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
First, install datenguideR
from GitHub:
devtools::install_github("CorrelAid/datenguideR")
Load package:
library(datenguideR)
Get IDs of all available NUTS-1 regions:
datenguideR::dg_regions %>% dplyr::filter(level == "nuts1") %>% knitr::kable()
Get all available meta data on statistics, substatistics, and parameters:
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:
dg_search("vote")
Note: Descriptions of variables are also available in English now! Translated via the googleLanguageR
package.
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)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
: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.
dg_call(region_id = "11", year = c(2001, 2003, 2007), stat_name = "BETR08", substat_name = "TIERA8")
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)dg_call(nuts_nr = 1, year = 2017, stat_name = "AI0506")
stat_name
: BETR08 (Landwirtschaftliche Betriebe mit Tierhaltung)substat_name
: TIERA8 (Landwirtschaftliche Betriebe mit Viehhaltung)dg_call(nuts_nr = 1, year = c(2001, 2003, 2007), stat_name = "BETR08", substat_name = "TIERA8")
stat_name
: BAU018 Total non-residential buildingssubstat_name
: BAUAHZ Main Type of Heatingdg_call(nuts_nr = 1, stat_name = "BAU018", substat_name = "BAUAHZ", year = 2016)
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
.
dg_map(nuts_nr = 1, year = 2017, stat_name = "AI0506")
The output is a ggplot object and can be manipulated further.
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
.
turnout_dat <- dg_map(nuts_nr = 1, year = 2017, stat_name = "AI0506", return_data = T) turnout_dat
Check out some use cases. For example here:
datenguideR builds on the amazing work of Datenguide and their GraphQL API. We especially thank Simon Jockers, Simon Wörpel, and Christian Rijke 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.
This package was created with devtools, usethis, and roxygen2. Continuous integration was done with Travis CI.
datenguideR is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.