An R package to analyze the parliamentary records of the 19th legislative period of the Bundestag, the German parliament.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

108 řádky
3.5KB

  1. ---
  2. title: "Interaction between fractions"
  3. output: rmarkdown::html_vignette
  4. vignette: >
  5. %\VignetteIndexEntry{Interaction between fractions}
  6. %\VignetteEngine{knitr::rmarkdown}
  7. %\VignetteEncoding{UTF-8}
  8. ---
  9. ```{r, include = FALSE}
  10. knitr::opts_chunk$set(
  11. collapse = TRUE,
  12. comment = "#>"
  13. )
  14. ```
  15. ```{r setup}
  16. library(hateimparlament)
  17. library(dplyr)
  18. library(ggplot2)
  19. library(stringr)
  20. library(tidyr)
  21. ```
  22. ## Preparation of data
  23. First, you need to download all records of the current legislative period.
  24. ```r
  25. fetch_all("../inst/records/") # path to directory where records should be stored
  26. ```
  27. Second, those `.xml` files, need to be parsed into `R` `tibbles`. This is accomplished by:
  28. ```r
  29. read_all("../inst/records/") %>% repair() -> res
  30. ```
  31. We also used `repair` to fix a bunch of formatting issues in the records.
  32. For development purposes, we load the tables from csv files.
  33. ```{r}
  34. res <- read_from_csv('../inst/csv/')
  35. ```
  36. ## Analysis
  37. Now we can start analysing our parsed dataset:
  38. ### Which party gives the most applause to which parties?
  39. ```{r}
  40. res$applause %>%
  41. left_join(res$speaker, by=c("on_speaker" = "id")) %>%
  42. select(on_fraction = fraction, where(is.logical)) %>%
  43. group_by(on_fraction) %>%
  44. arrange(on_fraction) %>%
  45. summarize("AfD" = sum(`AfD`),
  46. "BÜNDNIS 90/DIE GRÜNEN" = sum(`BUENDNIS_90_DIE_GRUENEN`),
  47. "CDU/CSU" = sum(`CDU_CSU`),
  48. "DIE LINKE" = sum(`DIE_LINKE`),
  49. "FDP" = sum(`FDP`),
  50. "SPD" = sum(`SPD`)) -> tb
  51. ```
  52. For plotting our results we reorganize them a bit and produce a bar plot:
  53. ```{r, fig.width=7, fig.height=6}
  54. pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>%
  55. filter(!is.na(on_fraction)) %>%
  56. bar_plot_fractions(x_variable = on_fraction,
  57. y_variable = value,
  58. fill = by_fraction,
  59. title = "Number of rounds of applauses from fractions to fractions",
  60. xlab = "Applauded fraction",
  61. ylab = "Rounds of applauses",
  62. filllab = "Applauding fraction",
  63. flipped = FALSE,
  64. rotatelab = TRUE)
  65. ```
  66. ### Which party comments the most on which parties?
  67. ```{r}
  68. res$comments %>%
  69. left_join(res$speaker, by=c("on_speaker" = "id")) %>%
  70. select(by_fraction = fraction.x, on_fraction = fraction.y) %>%
  71. group_by(on_fraction) %>%
  72. summarize(`AfD` = sum(str_detect(by_fraction, "AfD"), na.rm=T),
  73. `BÜNDNIS 90/DIE GRÜNEN` = sum(str_detect(by_fraction, "BÜNDNIS 90/DIE GRÜNEN"), na.rm=T),
  74. `CDU/CSU` = sum(str_detect(by_fraction, "CDU/CSU"), na.rm = T),
  75. `DIE LINKE` = sum(str_detect(by_fraction, "DIE LINKE"), na.rm=T),
  76. `FDP` = sum(str_detect(by_fraction, "FDP"), na.rm=T),
  77. `SPD` = sum(str_detect(by_fraction, "SPD"), na.rm=T)) -> tb
  78. ```
  79. Analogously we plot the results:
  80. ```{r, fig.width=7, fig.height=6}
  81. pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>%
  82. filter(!is.na(on_fraction)) %>%
  83. bar_plot_fractions(x_variable = on_fraction,
  84. y_variable = value,
  85. fill = by_fraction,
  86. title = "Number of comments from fractions to fractions",
  87. xlab = "Commented fraction",
  88. ylab = "Number of comments",
  89. filllab = "Commenting fraction",
  90. flipped = FALSE,
  91. rotatelab = TRUE)
  92. ```