{"id":22966673,"url":"https://github.com/favstats/btw_data","last_synced_at":"2026-01-08T19:42:53.142Z","repository":{"id":108349597,"uuid":"149297648","full_name":"favstats/btw_data","owner":"favstats","description":"Visualizing German National Elections","archived":false,"fork":false,"pushed_at":"2018-09-23T17:41:24.000Z","size":10817,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-07T19:36:19.669Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/favstats.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-18T14:00:06.000Z","updated_at":"2019-03-16T00:01:13.000Z","dependencies_parsed_at":"2023-05-21T23:00:16.213Z","dependency_job_id":null,"html_url":"https://github.com/favstats/btw_data","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/favstats%2Fbtw_data","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/favstats%2Fbtw_data/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/favstats%2Fbtw_data/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/favstats%2Fbtw_data/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/favstats","download_url":"https://codeload.github.com/favstats/btw_data/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246758372,"owners_count":20828919,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-14T20:45:00.147Z","updated_at":"2026-01-08T19:42:53.135Z","avatar_url":"https://github.com/favstats.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"German Elections Data\n================\nFabio Votta\n19.09.2018\n\nData\nSources:\n\n\u003chttps://www.govdata.de/web/guest/apps/-/details/bundestagswahl-2017\u003e\n\n\u003chttps://www.bundeswahlleiter.de/bundestagswahlen/2017/strukturdaten.html\u003e\n\n## Settings, Packages and Folders\n\n``` r\noptions(scipen = 999)\nknitr::opts_chunk$set(message = F,  warning = F, error = F, \n                      fig.align = \"center\", \n                      fig.width = 10, fig.height = 6)\n# Install these packages if you don't have theme yet\n# devtools::install_github(\"favstats/tidytemplate\")\n# install.packages(\"pacman\")\n\npacman::p_load(tidyverse)\n\n# Creates folders\n# tidytemplate::data_dir()\n# tidytemplate::images_dir()\n```\n\n## Helper Functions\n\n``` r\nget_percentage \u003c- function(part, total, digits = NULL) {\n\n  if(!is.numeric(part) | !is.numeric(total)){\n    cat(\"Non-numeric variables.. converting\\n\")\n    part \u003c- as.numeric(part)\n    total \u003c- as.numeric(total)\n  }\n   val \u003c- part / total * 100\n   if(!is.null(digits)){\n   val \u003c- round(val, digits)\n   }\n  return(val) \n}\n\n\nget_labs \u003c- function (pval) \n{\n  dplyr::case_when(is.na(pval) ~ \"\", \n                   pval \u003c 0.001 ~ \"p \u003c 0.001\",\n                   pval \u003c 0.01 ~ \"p \u003c 0.01\", \n                   pval \u003c 0.05 ~ \"p \u003c 0.05\", \n                   pval \u003c 0.1 ~  \"p \u003c 0.10\", \n      TRUE ~ \"p \u003e 0.10\")\n}\n\n\nget_r2_label \u003c- function(mod, digits = 3, adj = F){\n  if(!adj){#\n    message(\"R Squared\\n\")\nr2 \u003c- broom::glance(mod) %\u003e%\n    mutate(r.squared = r.squared * 100) %\u003e% \n    .$r.squared %\u003e% \n    format(., digits = digits) %\u003e% \n    paste0(., \"%\")\nreturn(r2)\n  } else {\n    message(\"Getting Adjusted R Squared\\n\")\n    r2 \u003c- broom::glance(mod) %\u003e%\n      mutate(adj.r.squared = adj.r.squared * 100) %\u003e% \n      .$adj.r.squared %\u003e% \n      format(., digits = digits) %\u003e% \n      paste0(., \"%\")    \nreturn(r2)\n  }\n}\n```\n\n## Load Data\n\n### BTW13\n\n``` r\n## loading in district names\ndistrict_names \u003c- tidytemplate::load_it(\"data/district_names.Rdata\")\n\n\nvars13 \u003c- c(\"wahlkreis_nr\", \"kreis\", \"part_of\", \"valid\", \"CDU\", \"SPD\", \"FDP\", \"DIE LINKE\", \"GRÜNE\", \"CSU\", \"PIRATEN\", \"NPD\", \"Tierschutzpartei\", \"REP\", \"ÖDP\", \"FAMILIE\", \"Bündnis 21/RRP\", \"RENTNER\", \"BP\", \"PBC\", \"BüSo\", \"DIE VIOLETTEN\", \"MLPD\", \"Volksabstimmung\", \"PSG\", \"AfD\", \"BIG\", \"pro Deutschland\", \"DIE RECHTE\", \"DIE FRAUEN\", \"FREIE WÄHLER\", \"Nichtwähler\", \"PARTEI DER VERNUNFT\", \"Die PARTEI\", \"B\", \"BGD\", \"DKP\", \"NEIN!\", \"Übrige\")\n\n # ss %\u003e% \n\nbtw13 \u003c- readr::read_delim(\"data/btw13_kerg.csv\", \n                           \";\", escape_double = FALSE, \n                           comment = \"#\", \n                           trim_ws = TRUE)  %\u003e% \n  janitor::clean_names() %\u003e% \n  select(nr, gebiet, geh_f6_rt, str_which(pattern = \"Endg\u003cfc\u003eltig\", .)) %\u003e%\n  select(nr, gebiet, geh_f6_rt, str_which(pattern = \"Zweitstimme\", .))  %\u003e% \n  select(-x6, -x10, -x14) %\u003e% \n  set_names(vars13) %\u003e% \n  mutate(cdu_csu = ifelse(is.na(CDU), CSU, CDU)) %\u003e% \n  mutate(perc_cdu_csu = get_percentage(cdu_csu, valid)) %\u003e%\n  mutate(perc_spd = get_percentage(SPD, valid)) %\u003e%\n  mutate(perc_linke = get_percentage(`DIE LINKE`, valid)) %\u003e%\n  mutate(perc_fdp = get_percentage(FDP, valid)) %\u003e%\n  mutate(perc_grun = get_percentage(`GRÜNE`, valid)) %\u003e%\n  mutate(perc_afd = get_percentage(AfD, valid)) %\u003e%\n  mutate(NPD = ifelse(is.na(NPD), \"0\", NPD)) %\u003e% \n  mutate(perc_npd = get_percentage(NPD, valid)) %\u003e%\n  select(wahlkreis_nr, kreis, part_of, valid, perc_afd, \n         perc_npd, perc_spd, perc_linke, perc_fdp, \n         perc_grun, perc_cdu_csu) %\u003e% \n  drop_na(wahlkreis_nr) %\u003e% \n  filter(part_of != 99)\n```\n\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n\n``` r\nstrukturdaten13 \u003c- read_delim(\"data/btw13_strukturdaten.csv\", \n                              \";\", escape_double = FALSE,\n                              comment = \"#\", \n                              trim_ws = TRUE, \n                              col_types = cols(.default = \"c\")) %\u003e% \n  janitor::clean_names() %\u003e% \n  .[-1,] %\u003e% \n  mutate(unemployment = str_replace(arbeitslosenquote_ende_dezember_2012_insgesamt, \",\", \".\") %\u003e% as.numeric) %\u003e% \n  mutate(sgbii = str_replace(empf_e4_nger_innen_von_leistungen_nach_sgb_ii_am_31_12_2012_insgesamt_je_1000_einwohner, \",\", \".\") %\u003e% as.numeric) %\u003e%   \n  mutate(wanderung = str_replace(zu_bzw_abnahme_der_bev_f6_lkerung_2011_wanderungssaldo_je_1000_einwohner, \",\", \".\") %\u003e% as.numeric) %\u003e%  \n  filter(!(str_detect(wahlkreis_name, \"insgesamt|Insgesamt\"))) %\u003e% \n  select(wahlkreis_nr, wanderung, unemployment, sgbii, wahlkreis_name, land) %\u003e%\n  mutate(wahlkreis_nr = as.integer(wahlkreis_nr))\n\nbtw13_data \u003c- btw13 %\u003e% \n  left_join(strukturdaten13) %\u003e% \n  select(-kreis, -wahlkreis_name) %\u003e% \n  left_join(district_names)\n\ntidytemplate::save_it(btw13_data)\n\nbtw13_data\n```\n\n    ## # A tibble: 299 x 15\n    ##    wahlkreis_nr part_of valid perc_afd perc_npd perc_spd perc_linke\n    ##           \u003cint\u003e \u003cchr\u003e   \u003cchr\u003e    \u003cdbl\u003e    \u003cdbl\u003e    \u003cdbl\u003e      \u003cdbl\u003e\n    ##  1            1 1       1606~     4.09    0.578     32.6       5.66\n    ##  2            2 1       1300~     3.84    0.564     29.7       4.41\n    ##  3            3 1       1249~     4.40    0.831     30.0       5.03\n    ##  4            4 1       1479~     4.39    0.577     31.5       4.36\n    ##  5            5 1       1449~     3.71    0.553     34.7       6.91\n    ##  6            6 1       1255~     4.70    0.923     32.7       4.77\n    ##  7            7 1       1773~     4.78    0.744     31.2       5.02\n    ##  8            8 1       1815~     4.97    0.720     30.1       5.02\n    ##  9            9 1       1304~     4.96    0.565     31.5       4.34\n    ## 10           10 1       1814~     5.63    0.759     29.7       4.94\n    ## # ... with 289 more rows, and 8 more variables: perc_fdp \u003cdbl\u003e,\n    ## #   perc_grun \u003cdbl\u003e, perc_cdu_csu \u003cdbl\u003e, wanderung \u003cdbl\u003e,\n    ## #   unemployment \u003cdbl\u003e, sgbii \u003cdbl\u003e, land \u003cchr\u003e, kreis \u003cchr\u003e\n\n### BTW 17\n\n``` r\nvars17 \u003c- c(\"wahlkreis_nr\", \"kreis\", \"part_of\", \"valid\", \"Christlich Demokratische Union Deutschlands\", \"Sozialdemokratische Partei Deutschlands\", \"DIE LINKE\", \"BÜNDNIS 90/DIE GRÜNEN\", \"Christlich-Soziale Union in Bayern e.V.\", \"Freie Demokratische Partei\", \"Alternative für Deutschland\", \"Piratenpartei Deutschland\", \"Nationaldemokratische Partei Deutschlands\", \"FREIE WÄHLER\", \"PARTEI MENSCH UMWELT TIERSCHUTZ\", \"Ökologisch-Demokratische Partei\", \"Die PARTEI\", \"Bayernpartei\", \"Ab jetzt...Demokratie durch Volksabstimmung\", \"Partei der Vernunft\", \"Marxistisch-Leninistische Partei Deutschlands\", \"Bürgerrechtsbewegung Solidarität\", \"Sozialistische Gleichheitspartei\", \"DIE RECHTE\", \"Allianz Deutscher Demokraten\", \"Allianz für Menschenrechte\", \"bergpartei\", \"Bündnis Grundeinkommen\", \"DEMOKRATIE IN BEWEGUNG\", \"Deutsche Kommunistische Partei\", \"Deutsche Mitte\", \"Die Grauen – Für alle Generationen\", \"Die Urbane. Eine HipHop Partei\", \"Madgeburger Gartenpartei\", \"Menschliche Welt\", \"Partei der Humanisten\", \"Partei für Gesundheitsforschung\", \"V-Partei³ - Partei für Veränderung\", \"Bündnis C - Christen für Deutschland\", \"DIE EINHEIT\", \"Die Violetten\", \"Familien-Partei Deutschlands\", \"Feministische Partei DIE FRAUEN\", \"Mieterpartei\", \"Neue Liberale – Die Sozialliberalen\", \"UNABHÄNGIGE für bürgernahe Demokratie\", \"Übrige\")\n\nbtw17 \u003c- readr::read_delim(\"data/btw17_kerg.csv\", \n                           \";\", escape_double = FALSE, comment = \"#\", \n                           trim_ws = TRUE)  %\u003e% \n  janitor::clean_names() %\u003e% \n  select(nr, gebiet, gehort_zu, grep(\"Endgültig\", .)) %\u003e%\n  select(nr, gebiet, gehort_zu, grep(\"Zweitstimme\", .)) %\u003e% \n  select(-x6, -x10, -x14) %\u003e% \n  set_names(vars17) %\u003e% \n  mutate(cdu_csu = ifelse(is.na(`Christlich Demokratische Union Deutschlands`), \n                          `Christlich-Soziale Union in Bayern e.V.`, \n                          `Christlich Demokratische Union Deutschlands`)) %\u003e% \n  mutate(perc_cdu_csu = get_percentage(cdu_csu, valid)) %\u003e%\n  mutate(perc_spd = get_percentage(`Sozialdemokratische Partei Deutschlands`, valid)) %\u003e%\n  mutate(perc_linke = get_percentage(`DIE LINKE`, valid)) %\u003e%\n  mutate(perc_fdp = get_percentage(`Freie Demokratische Partei`, valid)) %\u003e%\n  mutate(perc_grun = get_percentage(`BÜNDNIS 90/DIE GRÜNEN`, valid)) %\u003e%\n  mutate(perc_afd = get_percentage(`Alternative für Deutschland`, valid)) %\u003e%\n  mutate(NPD = ifelse(is.na(`Nationaldemokratische Partei Deutschlands`), \n                      \"0\", \n                      `Nationaldemokratische Partei Deutschlands`)) %\u003e% \n  mutate(perc_npd = get_percentage(NPD, valid)) %\u003e%\n  select(wahlkreis_nr, kreis, part_of, valid, perc_afd, \n         perc_npd, perc_spd, perc_linke, perc_fdp, \n         perc_grun, perc_cdu_csu) %\u003e% \n  drop_na(wahlkreis_nr) %\u003e% \n  filter(part_of != 99)\n```\n\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n\n``` r\nbtw17\n```\n\n    ## # A tibble: 299 x 11\n    ##    wahlkreis_nr kreis part_of valid perc_afd perc_npd perc_spd perc_linke\n    ##           \u003cint\u003e \u003cchr\u003e   \u003cint\u003e \u003cchr\u003e    \u003cdbl\u003e    \u003cdbl\u003e    \u003cdbl\u003e      \u003cdbl\u003e\n    ##  1            1 Flen~       1 1704~     6.84    0.205     23.7       8.21\n    ##  2            2 Nord~       1 1380~     6.54    0.218     22.5       6.22\n    ##  3            3 Stei~       1 1308~     8.54    0.360     22.7       6.67\n    ##  4            4 Rend~       1 1562~     7.41    0.196     22.9       6.37\n    ##  5            5 Kiel        1 1520~     6.91    0.164     23.8      10.2 \n    ##  6            6 Plön~       1 1305~     8.55    0.367     23.8       6.52\n    ##  7            7 Pinn~       1 1863~     8.57    0.239     22.9       7.03\n    ##  8            8 Sege~       1 1919~     8.94    0.241     22.4       6.90\n    ##  9            9 Osth~       1 1372~     8.58    0.219     24.6       6.05\n    ## 10           10 Herz~       1 1919~     9.79    0.256     22.3       6.50\n    ## # ... with 289 more rows, and 3 more variables: perc_fdp \u003cdbl\u003e,\n    ## #   perc_grun \u003cdbl\u003e, perc_cdu_csu \u003cdbl\u003e\n\n``` r\nstrukturdaten17 \u003c- read_delim(\"data/btw17_strukturdaten.csv\", \n                              \";\", escape_double = FALSE,\n                              col_types = cols(.default = \"c\"), \n                              comment = \"#\", \n                              trim_ws = TRUE, \n                              skip = 1) %\u003e% \n  janitor::clean_names() %\u003e% \n  mutate(unemployment = str_replace(arbeitslosenquote_marz_2017_insgesamt, \",\", \".\") %\u003e% as.numeric) %\u003e% \n  mutate(wanderung = str_replace(zu_bzw_abnahme_der_bev_f6_lkerung_2015_wanderungssaldo_je_1000_einwohner, \",\", \".\") %\u003e% as.numeric) %\u003e% \n  mutate(sgbii = str_replace(empfanger_innen_von_leistungen_nach_sgb_ii_am_31_12_2016_insgesamt_je_1000_einwohner, \",\", \".\") %\u003e% as.numeric) %\u003e% \n  filter(!(str_detect(wahlkreis_name, \"insgesamt|Insgesamt\"))) %\u003e% \n  select(wahlkreis_nr, unemployment, wanderung, sgbii, wahlkreis_name, land) %\u003e%\n  mutate(wahlkreis_nr = as.integer(wahlkreis_nr))\n\nbtw17_data \u003c- btw17 %\u003e% \n  left_join(strukturdaten17) %\u003e% \n  select(-kreis, -wahlkreis_name) %\u003e% \n  left_join(district_names)\n\ntidytemplate::save_it(btw17_data)\n\nbtw17_data\n```\n\n    ## # A tibble: 299 x 15\n    ##    wahlkreis_nr part_of valid perc_afd perc_npd perc_spd perc_linke\n    ##           \u003cint\u003e   \u003cint\u003e \u003cchr\u003e    \u003cdbl\u003e    \u003cdbl\u003e    \u003cdbl\u003e      \u003cdbl\u003e\n    ##  1            1       1 1704~     6.84    0.205     23.7       8.21\n    ##  2            2       1 1380~     6.54    0.218     22.5       6.22\n    ##  3            3       1 1308~     8.54    0.360     22.7       6.67\n    ##  4            4       1 1562~     7.41    0.196     22.9       6.37\n    ##  5            5       1 1520~     6.91    0.164     23.8      10.2 \n    ##  6            6       1 1305~     8.55    0.367     23.8       6.52\n    ##  7            7       1 1863~     8.57    0.239     22.9       7.03\n    ##  8            8       1 1919~     8.94    0.241     22.4       6.90\n    ##  9            9       1 1372~     8.58    0.219     24.6       6.05\n    ## 10           10       1 1919~     9.79    0.256     22.3       6.50\n    ## # ... with 289 more rows, and 8 more variables: perc_fdp \u003cdbl\u003e,\n    ## #   perc_grun \u003cdbl\u003e, perc_cdu_csu \u003cdbl\u003e, unemployment \u003cdbl\u003e,\n    ## #   wanderung \u003cdbl\u003e, sgbii \u003cdbl\u003e, land \u003cchr\u003e, kreis \u003cchr\u003e\n\n## All Together\n\n``` r\nbtw13_data \u003c- tidytemplate::load_it(\"data/btw13_data.Rdata\")\nbtw17_data \u003c- tidytemplate::load_it(\"data/btw17_data.Rdata\")\n\nbtw_data \u003c- bind_rows(btw17_data %\u003e% \n  mutate(year = \"BTW 2017\") %\u003e% \n  mutate(part_of = as.character(part_of)),\n  btw13_data %\u003e% \n    mutate(year = \"BTW 2013\"))  %\u003e% \n  mutate(east_west = case_when(\n    str_detect(land, \"Berlin|Brand|Nieder|Sachs\") ~ \"East Germany\",\n    T ~ \"West Germany\"\n  )) \n\ntidytemplate::save_it(btw_data)\n\nbtw_data\n```\n\n    ## # A tibble: 598 x 17\n    ##    wahlkreis_nr part_of valid perc_afd perc_npd perc_spd perc_linke\n    ##           \u003cint\u003e \u003cchr\u003e   \u003cchr\u003e    \u003cdbl\u003e    \u003cdbl\u003e    \u003cdbl\u003e      \u003cdbl\u003e\n    ##  1            1 1       1704~     6.84    0.205     23.7       8.21\n    ##  2            2 1       1380~     6.54    0.218     22.5       6.22\n    ##  3            3 1       1308~     8.54    0.360     22.7       6.67\n    ##  4            4 1       1562~     7.41    0.196     22.9       6.37\n    ##  5            5 1       1520~     6.91    0.164     23.8      10.2 \n    ##  6            6 1       1305~     8.55    0.367     23.8       6.52\n    ##  7            7 1       1863~     8.57    0.239     22.9       7.03\n    ##  8            8 1       1919~     8.94    0.241     22.4       6.90\n    ##  9            9 1       1372~     8.58    0.219     24.6       6.05\n    ## 10           10 1       1919~     9.79    0.256     22.3       6.50\n    ## # ... with 588 more rows, and 10 more variables: perc_fdp \u003cdbl\u003e,\n    ## #   perc_grun \u003cdbl\u003e, perc_cdu_csu \u003cdbl\u003e, unemployment \u003cdbl\u003e,\n    ## #   wanderung \u003cdbl\u003e, sgbii \u003cdbl\u003e, land \u003cchr\u003e, kreis \u003cchr\u003e, year \u003cchr\u003e,\n    ## #   east_west \u003cchr\u003e\n\n### Unemployment\n\n#### AfD\n\n``` r\nmod13 \u003c- lm(perc_afd ~ unemployment, data = btw13_data)\nmod17 \u003c- lm(perc_afd ~ unemployment, data = btw17_data)\n\nr2_13 \u003c- get_r2_label(mod13)\nr2_17 \u003c- get_r2_label(mod17)\n\nlabel_dat \u003c- tibble(year = c(\"BTW 2013\", \"BTW 2017\"),\n     text =   c(\nbroom::tidy(mod13) %\u003e% \n  filter(term == \"unemployment\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_13)) %\u003e% \n  .$text,\n\nbroom::tidy(mod17) %\u003e% \n  filter(term == \"unemployment\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_17)) %\u003e% \n  .$text\n))\n\ngg_afd \u003c- btw_data %\u003e% \n  ggplot(aes(unemployment, perc_afd)) +\n  geom_point() +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  geom_text(data = label_dat, aes(y = 34, x = 8, label = text), show.legend = F) +\n  labs(x = \"Unemployment Rate in %\", y = \"Vote Share AfD in %\",\n       title = \"AfD Vote Share and Unemployment Rate in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\") +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"))\n\ngg_afd\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-4-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_afd, width = 10, height = 6)\n```\n\n##### East-West\n\n``` r\ngg_afd_eastwest \u003c- btw_data %\u003e% \n  ggplot(aes(unemployment, perc_afd, color = east_west)) +\n  geom_point(aes(shape = east_west), alpha = 0.8) +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  labs(x = \"Unemployment Rate in %\", y = \"Vote Share AfD in %\",\n       title = \"AfD Vote Share and Unemployment Rate in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\")  +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"),\n        legend.position = \"bottom\") +\n  ggthemes::scale_color_colorblind(\"Region\") +\n  guides(shape = F)\n\ngg_afd_eastwest\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-5-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_afd_eastwest, width = 10, height = 6)\n```\n\n#### NPD\n\n``` r\nmod13 \u003c- lm(perc_npd ~ unemployment, data = btw13_data)\nmod17 \u003c- lm(perc_npd ~ unemployment, data = btw17_data)\n\nr2_13 \u003c- get_r2_label(mod13)\nr2_17 \u003c- get_r2_label(mod17)\n\nlabel_dat \u003c- tibble(year = c(\"BTW 2013\", \"BTW 2017\"),\n     text =   c(\nbroom::tidy(mod13) %\u003e% \n  filter(term == \"unemployment\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_13)) %\u003e% \n  .$text,\n\nbroom::tidy(mod17) %\u003e% \n  filter(term == \"unemployment\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_17)) %\u003e% \n  .$text\n))\n\ngg_npd \u003c- btw_data %\u003e% \n  ggplot(aes(unemployment, perc_npd)) +\n  geom_point() +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  geom_text(data = label_dat, aes(y = 5.2, x = 8, label = text), show.legend = F) +\n  labs(x = \"Unemployment Rate in %\", y = \"Vote Share npd in %\",\n       title = \"NPD Vote Share and Unemployment Rate in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\") +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"))\n\ngg_npd\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-6-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_npd, width = 10, height = 6)\n```\n\n##### East-West\n\n``` r\ngg_npd_eastwest \u003c- btw_data %\u003e% \n  ggplot(aes(unemployment, perc_npd, color = east_west)) +\n  geom_point(aes(shape = east_west), alpha = 0.8) +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  labs(x = \"Unemployment Rate in %\", y = \"Vote Share npd in %\",\n       title = \"NPD Vote Share and Unemployment Rate in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\")  +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"),\n        legend.position = \"bottom\") +\n  ggthemes::scale_color_colorblind(\"Region\")  +\n  guides(shape = F)\n\ngg_npd_eastwest\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-7-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_npd_eastwest, width = 10, height = 6)\n```\n\n#### Linke\n\n``` r\nmod13 \u003c- lm(perc_linke ~ unemployment, data = btw13_data)\nmod17 \u003c- lm(perc_linke ~ unemployment, data = btw17_data)\n\nr2_13 \u003c- get_r2_label(mod13)\nr2_17 \u003c- get_r2_label(mod17)\n\nlabel_dat \u003c- tibble(year = c(\"BTW 2013\", \"BTW 2017\"),\n     text =   c(\nbroom::tidy(mod13) %\u003e% \n  filter(term == \"unemployment\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_13)) %\u003e% \n  .$text,\n\nbroom::tidy(mod17) %\u003e% \n  filter(term == \"unemployment\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_17)) %\u003e% \n  .$text\n))\n\ngg_linke \u003c- btw_data %\u003e% \n  ggplot(aes(unemployment, perc_linke)) +\n  geom_point() +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  geom_text(data = label_dat, aes(y = 34, x = 8, label = text), show.legend = F) +\n  labs(x = \"Unemployment Rate in %\", y = \"Vote Share 'Die Linke' in %\",\n       title = \"'Die Linke' Vote Share and Unemployment Rate in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\") +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"))\n\ngg_linke\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-8-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_linke, width = 10, height = 6)\n```\n\n##### East-West\n\n``` r\ngg_linke_eastwest \u003c- btw_data %\u003e% \n  ggplot(aes(unemployment, perc_linke, color = east_west)) +\n  geom_point(aes(shape = east_west), alpha = 0.8) +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  labs(x = \"Unemployment Rate in %\", y = \"Vote Share 'Die Linke in %\",\n       title = \"'Die Linke' Vote Share and Unemployment Rate in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\")  +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"),\n        legend.position = \"bottom\") +\n  ggthemes::scale_color_colorblind(\"Region\")  +\n  guides(shape = F)\n\ngg_linke_eastwest\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-9-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_linke_eastwest, width = 10, height = 6)\n```\n\n### Migration\n\n#### AfD\n\n``` r\nmod13 \u003c- lm(perc_afd ~ wanderung, data = btw13_data)\nmod17 \u003c- lm(perc_afd ~ wanderung, data = btw17_data)\n\nr2_13 \u003c- get_r2_label(mod13)\nr2_17 \u003c- get_r2_label(mod17)\n\nlabel_dat \u003c- tibble(year = c(\"BTW 2013\", \"BTW 2017\"),\n     text =   c(\nbroom::tidy(mod13) %\u003e% \n  filter(term == \"wanderung\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_13)) %\u003e% \n  .$text,\n\nbroom::tidy(mod17) %\u003e% \n  filter(term == \"wanderung\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_17)) %\u003e% \n  .$text\n))\n\ngg_afd_wanderung \u003c- btw_data %\u003e% \n  ggplot(aes(wanderung, perc_afd)) +\n  geom_point() +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  geom_text(data = label_dat, aes(y = 34, x = 15, label = text), show.legend = F) +\n  labs(x = \"Migration Saldo\", y = \"Vote Share AfD in %\",\n       title = \"AfD Vote Share and Net Migration in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\") +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"))\n\ngg_afd_wanderung\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-10-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_afd_wanderung, width = 10, height = 6)\n```\n\n##### East-West\n\n``` r\ngg_afd_eastwest_wanderung \u003c- btw_data %\u003e% \n  ggplot(aes(wanderung, perc_afd, color = east_west)) +\n  geom_point(aes(shape = east_west), alpha = 0.8) +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  labs(x = \"Migration Saldo\", y = \"Vote Share AfD in %\",\n       title = \"AfD Vote Share and Net Migration in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\")  +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"),\n        legend.position = \"bottom\") +\n  ggthemes::scale_color_colorblind(\"Region\") +\n  guides(shape = F)\n\ngg_afd_eastwest_wanderung\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-11-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_afd_eastwest_wanderung, width = 10, height = 6)\n```\n\n##### Just BTW 2017\n\n``` r\ngg_afd_wanderung17 \u003c- btw_data %\u003e% \n  filter(year == \"BTW 2017\") %\u003e% \n  ggplot(aes(wanderung, perc_afd, color = east_west)) +\n  geom_point(aes(shape = east_west), alpha = 0.8) +\n  geom_smooth(method = \"lm\") +\n  theme_minimal() +\n  # geom_text(data = label_dat %\u003e% \n  #             filter(year == \"BTW 2017\"), aes(y = 34, x = 15, label = text), show.legend = F) +\n  labs(x = \"Net Migration per 1000 inhabitants\", y = \"Vote Share AfD in %\",\n       title = \"AfD Vote Share and Net Migration in the 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\")   +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"),\n        legend.position = \"bottom\") +\n  ggthemes::scale_color_colorblind(\"Region\") +\n  guides(shape = F)\n\ngg_afd_wanderung17\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-12-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_afd_wanderung17, width = 10, height = 6)\n```\n\n#### NPD\n\n``` r\nmod13 \u003c- lm(perc_npd ~ wanderung, data = btw13_data)\nmod17 \u003c- lm(perc_npd ~ wanderung, data = btw17_data)\n\nr2_13 \u003c- get_r2_label(mod13)\nr2_17 \u003c- get_r2_label(mod17)\n\nlabel_dat \u003c- tibble(year = c(\"BTW 2013\", \"BTW 2017\"),\n     text =   c(\nbroom::tidy(mod13) %\u003e% \n  filter(term == \"wanderung\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_13)) %\u003e% \n  .$text,\n\nbroom::tidy(mod17) %\u003e% \n  filter(term == \"wanderung\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_17)) %\u003e% \n  .$text\n))\n\ngg_npd_wanderung \u003c- btw_data %\u003e% \n  ggplot(aes(wanderung, perc_npd)) +\n  geom_point() +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  geom_text(data = label_dat, aes(y = 5.2, x = 15, label = text), show.legend = F) +\n  labs(x = \"Migration Saldo\", y = \"Vote Share NPD in %\",\n       title = \"NPD Vote Share and Net Migration in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\") +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"))\n\ngg_npd_wanderung\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-13-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_npd_wanderung, width = 10, height = 6)\n```\n\n##### East-West\n\n``` r\ngg_npd_eastwest_wanderung \u003c- btw_data %\u003e% \n  ggplot(aes(wanderung, perc_npd, color = east_west)) +\n  geom_point(aes(shape = east_west), alpha = 0.8) +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  labs(x = \"Migration Saldo\", y = \"Vote Share NPD in %\",\n       title = \"NPD Vote Share and Net Migration in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\")  +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"),\n        legend.position = \"bottom\") +\n  ggthemes::scale_color_colorblind(\"Region\")  +\n  guides(shape = F)\n\ngg_npd_eastwest_wanderung\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-14-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_npd_eastwest_wanderung, width = 10, height = 6)\n```\n\n#### Linke\n\n``` r\nmod13 \u003c- lm(perc_linke ~ wanderung, data = btw13_data)\nmod17 \u003c- lm(perc_linke ~ wanderung, data = btw17_data)\n\nr2_13 \u003c- get_r2_label(mod13)\nr2_17 \u003c- get_r2_label(mod17)\n\nlabel_dat \u003c- tibble(year = c(\"BTW 2013\", \"BTW 2017\"),\n     text =   c(\nbroom::tidy(mod13) %\u003e% \n  filter(term == \"wanderung\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_13)) %\u003e% \n  .$text,\n\nbroom::tidy(mod17) %\u003e% \n  filter(term == \"wanderung\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_17)) %\u003e% \n  .$text\n))\n\ngg_linke_wanderung \u003c- btw_data %\u003e% \n  ggplot(aes(wanderung, perc_linke)) +\n  geom_point() +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  geom_text(data = label_dat, aes(y = 34, x = 15, label = text), show.legend = F) +\n  labs(x = \"Migration Saldo\", y = \"Vote Share 'Die Linke' in %\",\n       title = \"'Die Linke' Vote Share and Net Migration in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\") +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"))\n\ngg_linke_wanderung\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-15-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_linke_wanderung, width = 10, height = 6)\n```\n\n##### East-West\n\n``` r\ngg_linke_eastwest_wanderung \u003c- btw_data %\u003e% \n  ggplot(aes(wanderung, perc_linke, color = east_west)) +\n  geom_point(aes(shape = east_west), alpha = 0.8) +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  labs(x = \"Migration Saldo\", y = \"Vote Share 'Die Linke in %\",\n       title = \"'Die Linke' Vote Share and Net Migration in the 2013 and 2017 German Elections\",\n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\")  +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"),\n        legend.position = \"bottom\") +\n  ggthemes::scale_color_colorblind(\"Region\")  +\n  guides(shape = F)\n\ngg_linke_eastwest_wanderung\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-16-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_linke_eastwest_wanderung, width = 10, height = 6)\n```\n\n## Where did the AfD come from?\n\n``` r\nbtw_lagged_all \u003c- btw_data %\u003e% \n  filter(year == \"BTW 2017\") %\u003e%\n  select(wahlkreis_nr, perc_afd) %\u003e% \nleft_join(\n  btw_data %\u003e% \n  filter(year == \"BTW 2013\") %\u003e%\n  select(wahlkreis_nr, perc_spd, perc_cdu_csu, perc_linke, \n         perc_grun, perc_fdp, perc_npd))  %\u003e% \n  rename(`AfD 2017` = perc_afd,\n         `SPD 2013` = perc_spd,\n         `CDU/CSU 2013` = perc_cdu_csu,\n         `Die Linke 2013` = perc_linke,\n         `Die Grünen 2013` = perc_grun,\n         `FDP 2013` = perc_fdp,\n         `NPD 2013` = perc_npd) %\u003e% \nleft_join(\n  btw_data %\u003e% select(wahlkreis_nr, east_west)) %\u003e% \n  select(-wahlkreis_nr) \n\nbtw_lagged_all\n```\n\n    ## # A tibble: 598 x 8\n    ##    `AfD 2017` `SPD 2013` `CDU/CSU 2013` `Die Linke 2013` `Die Grünen 201~\n    ##         \u003cdbl\u003e      \u003cdbl\u003e          \u003cdbl\u003e            \u003cdbl\u003e            \u003cdbl\u003e\n    ##  1       6.84       32.6           38.2             5.66             9.79\n    ##  2       6.84       32.6           38.2             5.66             9.79\n    ##  3       6.54       29.7           43.4             4.41             8.11\n    ##  4       6.54       29.7           43.4             4.41             8.11\n    ##  5       8.54       30.0           41.9             5.03             7.59\n    ##  6       8.54       30.0           41.9             5.03             7.59\n    ##  7       7.41       31.5           40.8             4.36             9.26\n    ##  8       7.41       31.5           40.8             4.36             9.26\n    ##  9       6.91       34.7           30.3             6.91            14.1 \n    ## 10       6.91       34.7           30.3             6.91            14.1 \n    ## # ... with 588 more rows, and 3 more variables: `FDP 2013` \u003cdbl\u003e, `NPD\n    ## #   2013` \u003cdbl\u003e, east_west \u003cchr\u003e\n\n``` r\nafd_origin \u003c- btw_lagged_all %\u003e% \n  gather(parties, perc, -east_west, -`AfD 2017`) %\u003e% \n  ggplot(aes(perc, `AfD 2017`, \n             color = east_west, \n             shape = east_west)) +\n  geom_point(alpha = 0.6) +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~parties, scales = \"free\") +\n  ggthemes::scale_color_colorblind(\"Region\") +\n  theme_minimal()  +\n  labs(x = \"2013 Vote Share in Percent\", \n       y = \"AfD Vote Share in 2017\",\n       title = \"Where did AfD voters come from?\", \n       subtitle = \"Comparing 2013 Election results with Vote Share of the AfD in the 2017 Election on the Electoral District Level\",\n       caption = \"N = 299 Electoral Districts\\nData from Federal Returning Officer ('Bundeswahlleiter')\\n@FabioFavusMaxim; favstats.eu\") +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"),\n        legend.position = \"bottom\") +\n  guides(shape = F) #+\n  # ggpubr::stat_cor()\n\nafd_origin\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-18-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(afd_origin, width = 12, height = 8)\n```\n\n**To-Do**\n\n## mapping\n\n``` r\nbtw_data \u003c- tidytemplate::load_it(\"data/btw_data.Rdata\")\n\nlibrary(rgdal)\nbtw_raw \u003c- readOGR(\"data/Geometrie_Wahlkreise_19DBT_geo.shp\") \n```\n\n    ## OGR data source with driver: ESRI Shapefile \n    ## Source: \"C:\\Users\\Fabio\\Documents\\git_proj\\btw_data\\data\\Geometrie_Wahlkreise_19DBT_geo.shp\", layer: \"Geometrie_Wahlkreise_19DBT_geo\"\n    ## with 299 features\n    ## It has 4 fields\n    ## Integer64 fields read as strings:  WKR_NR\n\n``` r\nplot(btw_raw)\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-19-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\nbtw_map_dat \u003c- btw_raw@data%\u003e% \n  as.data.frame() %\u003e% \n  rownames_to_column(\"id\")\n\nbtw_map_wt \u003c- spTransform(btw_raw, CRS(\"+proj=longlat +datum=WGS84\"))\n\nbtw_map \u003c- fortify(btw_map_wt) %\u003e% \n  left_join(btw_map_dat, by = \"id\")\n\n\nbtw_map13 \u003c- btw_data %\u003e% \n  filter(year == \"BTW 2013\") %\u003e% \n  inner_join(btw_map %\u003e% \n  rename(wahlkreis_nr = WKR_NR) %\u003e% \n  mutate(wahlkreis_nr = as.character(wahlkreis_nr) %\u003e% as.integer))\n\nbtw_map17 \u003c- btw_data %\u003e% \n  filter(year == \"BTW 2017\") %\u003e% \n  inner_join(btw_map %\u003e% \n  rename(wahlkreis_nr = WKR_NR) %\u003e% \n  mutate(wahlkreis_nr = as.character(wahlkreis_nr) %\u003e% as.integer))\n\n\n# btw_map \u003c- bind_rows(btw_map13 %\u003e% \n#             mutate(year = \"BTW 2013\"), \n#           btw_map17 %\u003e% \n#             mutate(year = \"BTW 2017\"))\n```\n\n``` r\nbtw_map \u003c- bind_rows(btw_map13 %\u003e% \n            mutate(year = \"BTW 2013\"), \n          btw_map17 %\u003e% \n            mutate(year = \"BTW 2017\"))\n\ngg_btw_map17 \u003c- btw_map17 %\u003e% \n  ggplot(aes(fill = perc_afd)) +\n  geom_map(map = btw_map17,\n         aes(x = long, y = lat, group = group, map_id = id),\n         color = NA, size = 0.4)  + \n  theme_void() +\n  # facet_wrap(~year) +\n  coord_map() +\n  viridis::scale_fill_viridis(\"AfD Vote Share in %\",\n                               option = \"D\", discrete = F, end = .8) +\n                       #       limits = c(1, 10),\n                       # breaks = c(1:10),\n                       # labels = c(1:10)) +\n  theme(plot.title = element_text(hjust = 0.5, size = 19, face = \"bold\"),\n    legend.justification = c(1, 0),\n    legend.position = c(0.82, 0.91),\n    legend.title = element_text(size = 16, face = \"bold\"), \n    strip.text = element_text(size = 12),\n    #axis.ticks.length = unit(3, \"cm\"),\n    legend.direction = \"horizontal\",\n    plot.caption = element_text(size = 13)) +\n  guides(fill = guide_colorbar(barwidth = 12, barheight = 0.5,\n                title.position = \"top\", title.hjust = 0.5,\n                label.theme = element_text(colour = \"black\", size = 14, angle = 0))) +\n  labs(title = \"German General Election 2017 - AfD Results\", \n       caption = \"299 Electoral Districts   \\nData from Federal Returning Officer ('Bundeswahlleiter')   \\n@FabioFavusMaxim; favstats.eu   \")\n\ngg_btw_map17\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-20-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_btw_map17, width = 8, height = 12)\n```\n\n``` r\nbtw_map \u003c- bind_rows(btw_map13 %\u003e% \n            mutate(year = \"BTW 2013\"), \n          btw_map17 %\u003e% \n            mutate(year = \"BTW 2017\"))\n\ngg_btw_map \u003c- btw_map %\u003e% \n  ggplot(aes(fill = perc_afd)) +\n  geom_map(map = btw_map17,\n         aes(x = long, y = lat, group = group, map_id = id),\n         color = NA, size = 0.4)  + \n  theme_void() +\n  facet_wrap(~year) +\n  coord_map() +\n  viridis::scale_fill_viridis(\"AfD Vote Share in %\",\n                               option = \"D\", discrete = F, end = .8) +\n                       #       limits = c(1, 10),\n                       # breaks = c(1:10),\n                       # labels = c(1:10)) +\n  theme(plot.title = element_text(hjust = 0.5, size = 19, face = \"bold\"),\n    legend.justification = c(1, 0),\n    legend.position = c(.63, 0.91),\n    legend.title = element_text(size = 12, face = \"bold\"), \n    strip.text = element_text(size = 13),\n    plot.margin=unit(c(0,0,0,0),\"mm\"),\n    #axis.ticks.length = unit(3, \"cm\"),\n    legend.direction = \"horizontal\",\n    plot.caption = element_text(size = 13)) +\n  guides(fill = guide_colorbar(barwidth = 10, barheight = 0.5,\n                title.position = \"top\", title.hjust = 0.5,\n                label.theme = element_text(colour = \"black\", size = 11, angle = 0))) +\n  labs(title = \"German General Elections 2013 and 2017 - AfD Results\\n\", \n       caption = \"299 Electoral Districts   \\nData from Federal Returning Officer ('Bundeswahlleiter')   \\n@FabioFavusMaxim; favstats.eu   \")\n\ngg_btw_map\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-21-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_btw_map, width = 8, height = 8)\n```\n\n``` r\ngg_btw_map_blue \u003c- btw_map %\u003e% \n  ggplot(aes(fill = perc_afd)) +\n  geom_map(map = btw_map17,\n         aes(x = long, y = lat, group = group, map_id = id),\n         color = NA, size = 0.4)  + \n  theme_void() +\n  facet_wrap(~year) +\n  coord_map() +\n  scale_fill_continuous(\"AfD Vote Share in %\", \n                        low = \"#00b4ff\", \n                        high = \"#002f42\") +\n  theme(plot.title = element_text(hjust = 0.5, size = 19, face = \"bold\"),\n    legend.justification = c(1, 0),\n    legend.position = c(.63, 0.91),\n    legend.title = element_text(size = 12, face = \"bold\"), \n    strip.text = element_text(size = 13),\n    plot.margin=unit(c(0,0,0,0),\"mm\"),\n    #axis.ticks.length = unit(3, \"cm\"),\n    legend.direction = \"horizontal\",\n    plot.caption = element_text(size = 13)) +\n  guides(fill = guide_colorbar(barwidth = 10, barheight = 0.5,\n                title.position = \"top\", title.hjust = 0.5,\n                label.theme = element_text(colour = \"black\", size = 11, angle = 0))) +\n  labs(title = \"German General Elections 2013 and 2017 - AfD Results\\n\", \n       caption = \"299 Electoral Districts   \\nData from Federal Returning Officer ('Bundeswahlleiter')   \\n@FabioFavusMaxim; favstats.eu   \")\n\ngg_btw_map_blue\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-22-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_btw_map_blue, width = 8, height = 8)\n```\n\n``` r\nbtw_map17_unemp \u003c- btw_map17 %\u003e% \n  ggplot(aes(fill = unemployment)) +\n  geom_map(map = btw_map17,\n         aes(x = long, y = lat, group = group, map_id = id),\n         color = NA, size = 0.4)  + \n  theme_void() +\n  # facet_wrap(~year) +\n  coord_map() +\n  viridis::scale_fill_viridis(\"Unemplyoment in %\",\n                               option = \"D\", discrete = F, end = .8) +\n                       #       limits = c(1, 10),\n                       # breaks = c(1:10),\n                       # labels = c(1:10)) +\n  theme(plot.title = element_text(hjust = 0.5, size = 19, face = \"bold\"),\n    legend.justification = c(1, 0),\n    legend.position = c(0.3, 0.82),\n    legend.title = element_text(size = 10, face = \"bold\"), strip.text = element_text(size = 12),\n    #axis.ticks.length = unit(3, \"cm\"),\n    legend.direction = \"horizontal\") +\n  guides(fill = guide_colorbar(barwidth = 10, barheight = 0.5,\n                title.position = \"top\", title.hjust = 0.5,\n                label.theme = element_text(colour = \"black\", size = 5, angle = 0)))\n\nbtw_map17_unemp\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-23-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(btw_map17_unemp, width = 8, height = 12)\n```\n\n## gganimate\n\n``` r\n# library(gganimate)\n# \n# btw_data %\u003e% \n#   ggplot(aes(unemployment, perc_afd, color = year)) +\n#   geom_point() +\n#   geom_smooth(method = \"lm\") +\n#   transition_states(year, transition_length = 10, state_length = 4)\n```\n\n``` r\n# btw_map_anim \u003c- btw_map %\u003e% \n#   ggplot(aes(fill = perc_afd)) +\n#   geom_map(map = btw_map,\n#          aes(x = long, y = lat, group = group, map_id = id),\n#          color = NA, size = 0.4)  + \n#   theme_void() +\n#   coord_map() +\n#   geom_text(aes(x = 7, y = 55, \n#                 label = paste0(\"Year: \", year)), size = 7) +\n#   viridis::scale_fill_viridis(\"AfD Vote Share in %\",\n#                                option = \"D\", discrete = F, end = .8) +\n#                        #       limits = c(1, 10),\n#                        # breaks = c(1:10),\n#                        # labels = c(1:10)) +\n#   theme(plot.title = element_text(hjust = 0.5, size = 19, face = \"bold\"),\n#     legend.justification = c(1, 0),\n#     legend.position = c(0.82, 0.91),\n#     legend.title = element_text(size = 16, face = \"bold\"), \n#     strip.text = element_text(size = 12),\n#     #axis.ticks.length = unit(3, \"cm\"),\n#     legend.direction = \"horizontal\",\n#     plot.caption = element_text(size = 13)) +\n#   guides(fill = guide_colorbar(barwidth = 12, barheight = 0.5,\n#                 title.position = \"top\", title.hjust = 0.5,\n#                 label.theme = element_text(colour = \"black\", size = 14, angle = 0))) +\n#   labs(title = \"German General Elections - AfD Results\", \n#        caption = \"299 Electoral Districts   \\nData from Federal Returning Officer ('Bundeswahlleiter')   \\n@FabioFavusMaxim; favstats.eu   \") +\n#   transition_states(year, transition_length = 10, state_length = 4)\n# \n# # btw_map_anim\n# \n# # tidytemplate::ggsave_it(btw_map_anim, width = 8, height = 12)\n# \n# btw_map_anim %\u003e% animate(\n#   nframes = 100, fps = 15, width = 666.6667, height = 1000, detail = 1\n# )\n# \n# anim_save(\"images/btw_map_anim.gif\")\n```\n\n## GGally\n\n``` r\npacman::p_load(GGally)\n\nplot_lagged_all \u003c- btw_lagged_all %\u003e% \n  ggpairs(lower = list(continuous = \"smooth\"),\n   mapping = aes(color = east_west)) +\n  labs(title = \"Where did AfD voters come from?\", \n       caption = \"N = 299 Electoral Districts\\nData from bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\") +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"))\n\n\nfor(i in 1:plot_lagged_all$nrow) {\n  for(j in 1:plot_lagged_all$ncol){\n    plot_lagged_all[i,j] \u003c- plot_lagged_all[i,j] + ggthemes::scale_color_fivethirtyeight() \n  }\n}\n\nplot_lagged_all\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-26-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(plot_lagged_all, width = 15, height = 13)\n```\n\n## SGB II\n\n``` r\nbtw_data \u003c- bind_rows(btw17_data %\u003e% \n  mutate(year = \"BTW 2017\") %\u003e% \n  mutate(part_of = as.character(part_of)),\n  btw13_data %\u003e% \n    mutate(year = \"BTW 2013\")) \n\nmod13 \u003c- lm(perc_npd ~ sgbii, data = btw13_data)\nmod17 \u003c- lm(perc_npd ~ sgbii, data = btw17_data)\n\noptions(scipen = 999)\n\nr2_13 \u003c- get_r2_label(mod13)\nr2_17 \u003c- get_r2_label(mod17)\n\nlabel_dat \u003c- tibble(year = c(\"BTW 2013\", \"BTW 2017\"),\n     text =   c(\nbroom::tidy(mod13) %\u003e% \n  filter(term == \"sgbii\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_13)) %\u003e% \n  .$text,\n\nbroom::tidy(mod17) %\u003e% \n  filter(term == \"sgbii\") %\u003e% \n  mutate(text = paste0(\"b = \", round(estimate, 2), \n         \", \", get_labs(p.value),\n         \", R Squared = \", r2_17)) %\u003e% \n  .$text\n))\n\ngg_npd_sgbii \u003c- btw_data %\u003e% \n  ggplot(aes(sgbii, perc_npd)) +\n  geom_point() +\n  geom_smooth(method = \"lm\") +\n  facet_wrap(~year) +\n  theme_minimal() +\n  geom_text(data = label_dat, aes(y = 5, x = 100, label = text), show.legend = F) +\n  labs(x = \"sgbii Rate in %\", \n       y = \"Vote Share npd in %\",\n       title = \"npd Vote Share and sgbii Rate in the 2013 and 2017 German Elections\", \n       caption = \"N = 299 Electoral Districts\\nData from govdata.de and bundeswahlleiter.de\\n@FabioFavusMaxim; favstats.eu\") +\n  theme(title = element_text(face = \"bold\"), \n        strip.background = element_rect(fill = \"lightgrey\"), \n        strip.text = element_text(face = \"bold\"))\n\ngg_npd_sgbii\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-27-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(gg_npd_sgbii, width = 10, height = 6)\n```\n\n## More BTWs\n\n``` r\nbtw13_data \u003c- tidytemplate::load_it(\"data/btw13_data.Rdata\")\nbtw17_data \u003c- tidytemplate::load_it(\"data/btw17_data.Rdata\")\nland_data \u003c- tidytemplate::load_it(\"data/land_data.Rdata\") %\u003e% \n  mutate(part_of = as.character(part_of))\n\nvars09 \u003c- readr::read_delim(\"data/btw_kerg/btw09_kerg.csv\", \n                           \";\", escape_double = FALSE, \n                           comment = \"#\", \n                           trim_ws = TRUE) %\u003e% \n  names(.) %\u003e% \n  stringi::stri_enc_tonative() %\u003e% \n  str_replace(\"\\032\", \"U\") %\u003e% \n  str_replace(\"Nr\", \"wahlkreis_nr\") %\u003e% \n  str_replace(\"Gebiet\", \"kreis\") %\u003e% \n  str_replace(\"gehUrt\", \"part_of\") %\u003e% \n  str_replace(\"GUltige\", \"valid\") %\u003e% \n  .[!(str_detect(., \"X|Wahlberechtigte|WUhler|UngUltige\"))]\n\nbtw09_data \u003c- readr::read_delim(\"data/btw_kerg/btw09_kerg.csv\", \n                           \";\", escape_double = FALSE, \n                           comment = \"#\", \n                           trim_ws = TRUE)  %\u003e% \n  janitor::clean_names() %\u003e% \n  select(nr, gebiet, geh_f6_rt, str_which(pattern = \"Endg\u003cfc\u003eltig\", .)) %\u003e%\n  select(nr, gebiet, geh_f6_rt, str_which(pattern = \"Zweitstimme\", .))  %\u003e% \n  select(-x6, -x10, -x14) %\u003e% \n  set_names(vars09) %\u003e% \n  mutate(cdu_csu = ifelse(is.na(CDU), CSU, CDU)) %\u003e% \n  mutate(perc_cdu_csu = get_percentage(cdu_csu, valid)) %\u003e%\n  mutate(perc_spd = get_percentage(SPD, valid)) %\u003e%\n  mutate(perc_linke = get_percentage(`DIE LINKE`, valid)) %\u003e%\n  mutate(perc_fdp = get_percentage(FDP, valid)) %\u003e%\n  mutate(perc_grun = get_percentage(`GRUNE`, valid)) %\u003e%\n  # mutate(perc_afd = get_percentage(AfD, valid)) %\u003e%\n  mutate(NPD = ifelse(is.na(NPD), \"0\", NPD)) %\u003e% \n  mutate(perc_npd = get_percentage(NPD, valid)) %\u003e%\n  select(wahlkreis_nr, kreis, part_of, valid, #perc_afd, \n         perc_npd, perc_spd, perc_linke, perc_fdp, \n         perc_grun, perc_cdu_csu) %\u003e% \n  drop_na(wahlkreis_nr) %\u003e% \n  filter(part_of != 99) %\u003e% \n  left_join(land_data)\n```\n\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n\n``` r\ntidytemplate::save_it(btw09_data)\n\nbtw09_data\n```\n\n    ## # A tibble: 299 x 11\n    ##    wahlkreis_nr kreis part_of valid perc_npd perc_spd perc_linke perc_fdp\n    ##           \u003cint\u003e \u003cchr\u003e \u003cchr\u003e   \u003cchr\u003e    \u003cdbl\u003e    \u003cdbl\u003e      \u003cdbl\u003e    \u003cdbl\u003e\n    ##  1            1 Flen~ 1       1591~    0.779     26.3       8.47     15.2\n    ##  2            2 Nord~ 1       1314~    0.857     24.5       6.72     17.2\n    ##  3            3 \"Ste~ 1       1252~    1.21      25.0       7.83     17.5\n    ##  4            4 \"Ren~ 1       1469~    0.830     26.7       6.68     15.9\n    ##  5            5 Kiel  1       1432~    0.890     29.6       9.38     13.4\n    ##  6            6 \"Pl\\~ 1       1249~    1.05      28.5       7.15     15.8\n    ##  7            7 Pinn~ 1       1729~    1.01      26.3       7.75     16.6\n    ##  8            8 Sege~ 1       1769~    0.953     25.1       8.07     17.7\n    ##  9            9 Osth~ 1       1288~    0.925     28.2       7.25     17.6\n    ## 10           10 \"Her~ 1       1783~    1.19      25.4       7.77     17.2\n    ## # ... with 289 more rows, and 3 more variables: perc_grun \u003cdbl\u003e,\n    ## #   perc_cdu_csu \u003cdbl\u003e, land \u003cchr\u003e\n\n``` r\nvars05 \u003c- readr::read_delim(\"data/btw_kerg/btw05_kerg.csv\", \n                           \";\", escape_double = FALSE, \n                           comment = \"#\", \n                           trim_ws = TRUE) %\u003e% \n  names(.) %\u003e% \n  stringi::stri_enc_tonative() %\u003e% \n  str_replace(\"\\032\", \"U\") %\u003e% \n  str_replace(\"Nr\", \"wahlkreis_nr\") %\u003e% \n  str_replace(\"Gebiet\", \"kreis\") %\u003e% \n  str_replace(\"gehUrt\", \"part_of\") %\u003e% \n  str_replace(\"GUltige\", \"valid\") %\u003e% \n  .[!(str_detect(., \"X|Wahlberechtigte|WUhler|UngUltige\"))]\n\nbtw05_data \u003c- readr::read_delim(\"data/btw_kerg/btw05_kerg.csv\", \n                           \";\", escape_double = FALSE, \n                           comment = \"#\", \n                           trim_ws = TRUE)  %\u003e% \n  janitor::clean_names() %\u003e% \n  select(nr, gebiet, geh_f6_rt, str_which(pattern = \"Endg\u003cfc\u003eltig\", .)) %\u003e%\n  select(nr, gebiet, geh_f6_rt, str_which(pattern = \"Zweitstimme\", .))  %\u003e% \n  select(-x6, -x10, -x14) %\u003e% \n  set_names(vars05) %\u003e% \n  mutate(cdu_csu = ifelse(is.na(CDU), CSU, CDU)) %\u003e% \n  mutate(perc_cdu_csu = get_percentage(cdu_csu, valid)) %\u003e%\n  mutate(perc_spd = get_percentage(SPD, valid)) %\u003e%\n  mutate(perc_linke = get_percentage(`Die Linke.`, valid)) %\u003e%\n  mutate(perc_fdp = get_percentage(FDP, valid)) %\u003e%\n  mutate(perc_grun = get_percentage(`GRUNE`, valid)) %\u003e%\n  # mutate(perc_afd = get_percentage(AfD, valid)) %\u003e%\n  mutate(NPD = ifelse(is.na(NPD), \"0\", NPD)) %\u003e% \n  mutate(perc_npd = get_percentage(NPD, valid)) %\u003e%\n  select(wahlkreis_nr, kreis, part_of, valid, #perc_afd, \n         perc_npd, perc_spd, perc_linke, perc_fdp, \n         perc_grun, perc_cdu_csu) %\u003e% \n  drop_na(wahlkreis_nr) %\u003e% \n  filter(part_of != 99)  %\u003e% \n  left_join(land_data)\n```\n\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n\n``` r\ntidytemplate::save_it(btw05_data)\n\nbtw05_data\n```\n\n    ## # A tibble: 299 x 11\n    ##    wahlkreis_nr kreis part_of valid perc_npd perc_spd perc_linke perc_fdp\n    ##           \u003cint\u003e \u003cchr\u003e \u003cchr\u003e   \u003cchr\u003e    \u003cdbl\u003e    \u003cdbl\u003e      \u003cdbl\u003e    \u003cdbl\u003e\n    ##  1            1 Flen~ 1       1692~    0.803     39.1       4.80     9.69\n    ##  2            2 Nord~ 1       1417~    0.840     36.2       3.86    10.1 \n    ##  3            3 \"Ste~ 1       1369~    1.22      36.3       4.59    10.7 \n    ##  4            4 \"Ren~ 1       1567~    0.929     37.6       4.16     9.89\n    ##  5            5 Kiel  1       1489~    0.924     41.5       5.97     8.85\n    ##  6            6 \"Pl\\~ 1       1348~    1.13      39.5       4.46     9.60\n    ##  7            7 Pinn~ 1       1833~    0.966     37.4       4.39    10.3 \n    ##  8            8 Sege~ 1       1865~    0.915     36.6       4.38    11.3 \n    ##  9            9 Osth~ 1       1391~    0.970     38.8       4.16    10.4 \n    ## 10           10 \"Her~ 1       1878~    1.23      35.8       4.37    11.2 \n    ## # ... with 289 more rows, and 3 more variables: perc_grun \u003cdbl\u003e,\n    ## #   perc_cdu_csu \u003cdbl\u003e, land \u003cchr\u003e\n\n``` r\nvars02 \u003c- readr::read_delim(\"data/btw_kerg/btw02_kerg.csv\", \n                           \";\", escape_double = FALSE, \n                           comment = \"#\", \n                           trim_ws = TRUE) %\u003e% \n  select(Wahlkreis, X2, Land, str_which(pattern = \"Zweitstimme\", .))  %\u003e% \n  select(-`Ung\u003cfc\u003eltige_1`) %\u003e% \n  names(.) %\u003e% \n  stringi::stri_enc_toascii() %\u003e% \n  str_replace(\"_1\", \"\") %\u003e% \n  str_replace(\"\\032\", \"U\") %\u003e% \n  str_replace(\"Wahlkreis\", \"wahlkreis_nr\") %\u003e% \n  str_replace(\"X2\", \"kreis\") %\u003e% \n  str_replace(\"Land\", \"part_of\") %\u003e% \n  str_replace(\"GR\u003cdc\u003eNE\", \"GRUNE\") %\u003e% \n  str_replace(\"G\u003cfc\u003eltige\", \"valid\") \n\nbtw02_data \u003c- readr::read_delim(\"data/btw_kerg/btw02_kerg.csv\", \n                           \";\", escape_double = FALSE, \n                           comment = \"#\", \n                           trim_ws = TRUE)  %\u003e% \n  janitor::clean_names() %\u003e% \n  select(wahlkreis, x2, land, str_which(pattern = \"Zweitstimme\", .), -ung_fc_ltige_1)  %\u003e%  \n  .[-1,] %\u003e% \n  set_names(vars02) %\u003e% \n  mutate(wahlkreis_nr = as.integer(wahlkreis_nr)) %\u003e% \n  mutate(part_of = str_sub(part_of, 2) %\u003e% \n           as.integer %\u003e% as.character) %\u003e% \n  mutate(cdu_csu = ifelse(is.na(CDU), CSU, CDU)) %\u003e% \n  mutate(perc_cdu_csu = get_percentage(cdu_csu, valid)) %\u003e%\n  mutate(perc_spd = get_percentage(SPD, valid)) %\u003e%\n  mutate(perc_linke = get_percentage(PDS, valid)) %\u003e%\n  mutate(perc_fdp = get_percentage(FDP, valid)) %\u003e%\n  mutate(perc_grun = get_percentage(`GRUNE`, valid)) %\u003e%\n  # mutate(perc_afd = get_percentage(AfD, valid)) %\u003e%\n  mutate(NPD = ifelse(is.na(NPD), \"0\", NPD)) %\u003e% \n  mutate(perc_npd = get_percentage(NPD, valid)) %\u003e%\n  select(wahlkreis_nr, kreis, part_of, valid, #perc_afd, \n         perc_npd, perc_spd, perc_linke, perc_fdp, \n         perc_grun, perc_cdu_csu) %\u003e% \n  drop_na(wahlkreis_nr) %\u003e% \n  filter(part_of != 99)  %\u003e% \n  left_join(land_data)\n```\n\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n\n``` r\ntidytemplate::save_it(btw02_data)\n\nbtw02_data\n```\n\n    ## # A tibble: 299 x 11\n    ##    wahlkreis_nr kreis part_of valid perc_npd perc_spd perc_linke perc_fdp\n    ##           \u003cint\u003e \u003cchr\u003e \u003cchr\u003e   \u003cchr\u003e    \u003cdbl\u003e    \u003cdbl\u003e      \u003cdbl\u003e    \u003cdbl\u003e\n    ##  1            1 Flen~ 1       1702~    0.190     44.0      1.29      7.45\n    ##  2            2 Nord~ 1       1429~    0.223     42.4      1.11      8.20\n    ##  3            3 \"Ste~ 1       1387~    0.315     41.2      1.28      8.83\n    ##  4            4 \"Ren~ 1       1577~    0.237     42.6      1.17      8.03\n    ##  5            5 Kiel  1       1512~    0.365     44.3      2.15      8.28\n    ##  6            6 \"Pl\\~ 1       1375~    0.302     44.6      1.22      7.67\n    ##  7            7 Pinn~ 1       1857~    0.215     41.6      1.19      8.30\n    ##  8            8 Sege~ 1       1863~    0.228     41.2      1.27      8.69\n    ##  9            9 Osth~ 1       1400~    0.278     44.0      0.954     7.83\n    ## 10           10 \"Her~ 1       1887~    0.310     40.0      1.15      8.17\n    ## # ... with 289 more rows, and 3 more variables: perc_grun \u003cdbl\u003e,\n    ## #   perc_cdu_csu \u003cdbl\u003e, land \u003cchr\u003e\n\n``` r\nvars98 \u003c- readr::read_delim(\"data/btw_kerg/btw98_kerg.csv\", \n                           \";\", escape_double = FALSE, \n                           comment = \"#\", \n                           trim_ws = TRUE) %\u003e% \n  select(Wahlkreis, X2, Land, str_which(pattern = \"Zweitstimme\", .))  %\u003e% \n  select(-`Ung\u003cfc\u003eltige_1`) %\u003e% \n  names(.) %\u003e% \n  stringi::stri_enc_toascii() %\u003e% \n  str_replace(\"_1\", \"\") %\u003e% \n  str_replace(\"\\032\", \"U\") %\u003e% \n  str_replace(\"Wahlkreis\", \"wahlkreis_nr\") %\u003e% \n  str_replace(\"X2\", \"kreis\") %\u003e% \n  str_replace(\"Land\", \"part_of\") %\u003e% \n  str_replace(\"GR\u003cdc\u003eNE\", \"GRUNE\") %\u003e% \n  str_replace(\"G\u003cfc\u003eltige\", \"valid\") \n\nbtw98_data \u003c- readr::read_delim(\"data/btw_kerg/btw98_kerg.csv\", \n                           \";\", escape_double = FALSE, \n                           comment = \"#\", \n                           trim_ws = TRUE)  %\u003e% \n  janitor::clean_names() %\u003e% \n  select(wahlkreis, x2, land, str_which(pattern = \"Zweitstimme\", .), -ung_fc_ltige_1)  %\u003e%  \n  .[-1,] %\u003e% \n  set_names(vars98) %\u003e% \n  mutate(wahlkreis_nr = as.integer(wahlkreis_nr)) %\u003e% \n  mutate(part_of = str_sub(part_of, 2) %\u003e% \n           as.integer %\u003e% as.character) %\u003e% \n  mutate(cdu_csu = ifelse(is.na(CDU), CSU, CDU)) %\u003e% \n  mutate(perc_cdu_csu = get_percentage(cdu_csu, valid)) %\u003e%\n  mutate(perc_spd = get_percentage(SPD, valid)) %\u003e%\n  mutate(perc_linke = get_percentage(PDS, valid)) %\u003e%\n  mutate(perc_fdp = get_percentage(`F.D.P.`, valid)) %\u003e%\n  mutate(perc_grun = get_percentage(`GRUNE`, valid)) %\u003e%\n  # mutate(perc_afd = get_percentage(AfD, valid)) %\u003e%\n  mutate(NPD = ifelse(is.na(NPD), \"0\", NPD)) %\u003e% \n  mutate(perc_npd = get_percentage(NPD, valid)) %\u003e%\n  select(wahlkreis_nr, kreis, part_of, valid, #perc_afd, \n         perc_npd, perc_spd, perc_linke, perc_fdp, \n         perc_grun, perc_cdu_csu) %\u003e% \n  drop_na(wahlkreis_nr) %\u003e% \n  filter(part_of != 99)  %\u003e% \n  left_join(land_data)\n```\n\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n    ## Non-numeric variables.. converting\n\n``` r\ntidytemplate::save_it(btw98_data)\n\nbtw98_data\n```\n\n    ## # A tibble: 328 x 11\n    ##    wahlkreis_nr kreis part_of valid perc_npd perc_spd perc_linke perc_fdp\n    ##           \u003cint\u003e \u003cchr\u003e \u003cchr\u003e   \u003cchr\u003e    \u003cdbl\u003e    \u003cdbl\u003e      \u003cdbl\u003e    \u003cdbl\u003e\n    ##  1            1 Flen~ 1       1711~   0.0912     47.2       1.33     6.57\n    ##  2            2 Nord~ 1       1365~   0.174      43.4       1.12     7.82\n    ##  3            3 \"Ste~ 1       1330~   0.169      45.5       1.16     7.36\n    ##  4            4 \"Ren~ 1       1708~   0.146      44.5       1.28     8.18\n    ##  5            5 Kiel  1       1429~   0.149      49.2       2.41     6.87\n    ##  6            6 \"Pl\\~ 1       1315~   0.160      47.7       1.47     6.69\n    ##  7            7 Pinn~ 1       1837~   0.144      44.9       1.47     8.27\n    ##  8            8 Sege~ 1       1950~   0.132      43.6       1.49     8.50\n    ##  9            9 Osth~ 1       1392~   0.141      44.9       1.17     7.45\n    ## 10           10 \"Her~ 1       2041~   0.238      42.8       1.46     8.35\n    ## # ... with 318 more rows, and 3 more variables: perc_grun \u003cdbl\u003e,\n    ## #   perc_cdu_csu \u003cdbl\u003e, land \u003cchr\u003e\n\n``` r\n# btw98_data \u003c- tidytemplate::load_it(\"data/btw98_data.Rdata\")\nbtw02_data \u003c- tidytemplate::load_it(\"data/btw02_data.Rdata\")\nbtw05_data \u003c- tidytemplate::load_it(\"data/btw05_data.Rdata\")\nbtw09_data \u003c- tidytemplate::load_it(\"data/btw09_data.Rdata\")\nbtw13_data \u003c- tidytemplate::load_it(\"data/btw13_data.Rdata\")\nbtw17_data \u003c- tidytemplate::load_it(\"data/btw17_data.Rdata\")\n\nbtw_data \u003c- bind_rows(btw17_data %\u003e% \n  mutate(year = \"BTW 2017\") %\u003e% \n  mutate(part_of = as.character(part_of)),\n  btw13_data %\u003e% \n    mutate(year = \"BTW 2013\"),\n  btw09_data %\u003e% \n  mutate(year = \"BTW 2009\") %\u003e% \n  mutate(part_of = as.character(part_of)),\n  btw05_data %\u003e% \n  mutate(year = \"BTW 2005\") %\u003e% \n  mutate(part_of = as.character(part_of)),\n    btw02_data %\u003e% \n  mutate(year = \"BTW 2002\") %\u003e% \n  mutate(part_of = as.character(part_of)))  %\u003e% \n  mutate(east_west = case_when(\n    str_detect(land, \"Berlin|Brand|Nieder|Sachs\") ~ \"East Germany\",\n    T ~ \"West Germany\"\n  )) \n\nlibrary(gganimate)\n\nresults \u003c- btw_data %\u003e% \n  mutate(year_label = year) %\u003e% \n  mutate(year = parse_number(year)) %\u003e% \n  group_by(year_label, year) %\u003e% \n  summarize(perc_npd = mean(perc_npd))\n\nanim_npd \u003c- btw_data %\u003e% \n  mutate(year = parse_number(year)) %\u003e% \n  ggplot(aes(year, perc_npd)) +\n  geom_line(aes(group = wahlkreis_nr, color = east_west), \n            size = 0.8, alpha = 0.2) +\n  geom_line(data = results, size = 1, \n            color = \"black\", linetype = \"dashed\") +\n  # geom_vline(data = results, aes(xintercept = year), \n  #            linetype = \"dashed\", color = \"darkgrey\") +\n  geom_text(data = results, aes(x = year, y = 6.5, label = year_label), \n            angle = 90, color = \"black\", nudge_x = 0.3) +\n  ggthemes::scale_color_gdocs(\"Region\") +\n  scale_x_continuous(breaks = c(2002, 2005, 2009, 2013, 2017),\n                     labels = c(2002, 2005, 2009, 2013, 2017)) +\n  # gganimate::transition_reveal(wahlkreis_nr, year) +\n  theme_minimal() +\n  labs(x = \"\", y = \"Vote Share NPD in %\\n\",\n       title = \"NPD Vote Share in German National Eelections (2002 - 2017)\\n\", \n       caption = \"N = 299 Electoral Districts\\nData from Federal Returning Officer ('Bundeswahlleiter')\\n@FabioFavusMaxim; favstats.eu\") +\n  theme(title = element_text(face = \"bold\"), \n        legend.position = \"bottom\")\n\nanim_npd\n```\n\n\u003cimg src=\"btw_data_files/figure-gfm/unnamed-chunk-29-1.png\" style=\"display: block; margin: auto;\" /\u003e\n\n``` r\ntidytemplate::ggsave_it(anim_npd, width = 10, height = 6)\n\n# anim_npd %\u003e% animate(\n#   nframes = 100, fps = 15, width = 1000, height = 600, detail = 1\n# )\n# \n# anim_save(\"images/anim_npd.gif\")\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffavstats%2Fbtw_data","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffavstats%2Fbtw_data","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffavstats%2Fbtw_data/lists"}