An R package to analyze the parliamentary records of the 19th legislative period of the Bundestag, the German parliament.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

109 рядки
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 only fetch records if they are not already
  33. stored as csv files:
  34. ```{r}
  35. res <- read_from_csv_or_fetch('../inst/')
  36. ```
  37. ## Analysis
  38. Now we can start analysing our parsed dataset:
  39. ### Which party gives the most applause to which parties?
  40. ```{r}
  41. res$applause %>%
  42. left_join(res$speaker, by=c("on_speaker" = "id")) %>%
  43. select(on_fraction = fraction, where(is.logical)) %>%
  44. group_by(on_fraction) %>%
  45. arrange(on_fraction) %>%
  46. summarize("AfD" = sum(`AfD`),
  47. "BÜNDNIS 90/DIE GRÜNEN" = sum(`BUENDNIS_90_DIE_GRUENEN`),
  48. "CDU/CSU" = sum(`CDU_CSU`),
  49. "DIE LINKE" = sum(`DIE_LINKE`),
  50. "FDP" = sum(`FDP`),
  51. "SPD" = sum(`SPD`)) -> tb
  52. ```
  53. For plotting our results we reorganize them a bit and produce a bar plot:
  54. ```{r, fig.width=7, fig.height=6}
  55. pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>%
  56. filter(!is.na(on_fraction)) %>%
  57. bar_plot_fractions(x_variable = on_fraction,
  58. y_variable = value,
  59. fill = by_fraction,
  60. title = "Number of rounds of applauses from fractions to fractions",
  61. xlab = "Applauded fraction",
  62. ylab = "Rounds of applauses",
  63. filllab = "Applauding fraction",
  64. flipped = FALSE,
  65. rotatelab = TRUE)
  66. ```
  67. ### Which party comments the most on which parties?
  68. ```{r}
  69. res$comments %>%
  70. left_join(res$speaker, by=c("on_speaker" = "id")) %>%
  71. select(by_fraction = fraction.x, on_fraction = fraction.y) %>%
  72. group_by(on_fraction) %>%
  73. summarize(`AfD` = sum(str_detect(by_fraction, "AfD"), na.rm=T),
  74. `BÜNDNIS 90/DIE GRÜNEN` = sum(str_detect(by_fraction, "BÜNDNIS 90/DIE GRÜNEN"), na.rm=T),
  75. `CDU/CSU` = sum(str_detect(by_fraction, "CDU/CSU"), na.rm = T),
  76. `DIE LINKE` = sum(str_detect(by_fraction, "DIE LINKE"), na.rm=T),
  77. `FDP` = sum(str_detect(by_fraction, "FDP"), na.rm=T),
  78. `SPD` = sum(str_detect(by_fraction, "SPD"), na.rm=T)) -> tb
  79. ```
  80. Analogously we plot the results:
  81. ```{r, fig.width=7, fig.height=6}
  82. pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>%
  83. filter(!is.na(on_fraction)) %>%
  84. bar_plot_fractions(x_variable = on_fraction,
  85. y_variable = value,
  86. fill = by_fraction,
  87. title = "Number of comments from fractions to fractions",
  88. xlab = "Commented fraction",
  89. ylab = "Number of comments",
  90. filllab = "Commenting fraction",
  91. flipped = FALSE,
  92. rotatelab = TRUE)
  93. ```