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

116 строки
3.6KB

  1. ---
  2. title: "interaction"
  3. output: rmarkdown::html_vignette
  4. vignette: >
  5. %\VignetteIndexEntry{interaction}
  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 and unpacked
  32. the result into more descriptive variables.
  33. For development purposes, we load the tables from csv files.
  34. ```{r}
  35. res <- read_from_csv('../inst/csv/')
  36. ```
  37. and unpack our tibbles
  38. ```{r}
  39. comments <- res$comments
  40. speeches <- res$speeches
  41. speaker <- res$speaker
  42. talks <- res$talks
  43. ```
  44. ## Analysis
  45. Now we can start analysing our parsed dataset:
  46. ### Which party gives the most applause to which parties?
  47. ```{r}
  48. res$applause %>%
  49. left_join(res$speaker, by=c("on_speaker" = "id")) %>%
  50. select(on_fraction = fraction, where(is.logical)) %>%
  51. group_by(on_fraction) %>%
  52. arrange(on_fraction) %>%
  53. summarize("AfD" = sum(`AfD`),
  54. "BÜNDNIS 90 / DIE GRÜNEN" = sum(`BUENDNIS_90_DIE_GRUENEN`),
  55. "CDU/CSU" = sum(`CDU_CSU`),
  56. "DIE LINKE" = sum(`DIE_LINKE`),
  57. "FDP" = sum(`FDP`),
  58. "SPD" = sum(`SPD`)) -> tb
  59. ```
  60. For plotting our results we reorganize them a bit and produce a bar plot:
  61. ```{r, fig.width=7}
  62. pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>%
  63. filter(!is.na(on_fraction)) %>%
  64. bar_plot_fractions(x_variable = on_fraction,
  65. y_variable = value,
  66. fill = by_fraction,
  67. title = "Number of rounds of applauses from fractions to fractions",
  68. xlab = "Applauded fraction",
  69. ylab = "Rounds of applauses",
  70. filllab = "Applauding fraction",
  71. flipped = FALSE,
  72. rotatelab = TRUE)
  73. ```
  74. ### Which party comments the most on which parties?
  75. ```{r}
  76. res$comments %>%
  77. left_join(res$speaker, by=c("on_speaker" = "id")) %>%
  78. select(by_fraction = fraction.x, on_fraction = fraction.y) %>%
  79. group_by(on_fraction) %>%
  80. summarize(`AfD` = sum(str_detect(by_fraction, "AfD"), na.rm=T),
  81. `BÜNDNIS 90 / DIE GRÜNEN` = sum(str_detect(by_fraction, "BÜNDNIS 90/DIE GRÜNEN"), na.rm=T),
  82. `CDU/CSU` = sum(str_detect(by_fraction, "CDU/CSU"), na.rm = T),
  83. `DIE LINKE` = sum(str_detect(by_fraction, "DIE LINKE"), na.rm=T),
  84. `FDP` = sum(str_detect(by_fraction, "FDP"), na.rm=T),
  85. `SPD` = sum(str_detect(by_fraction, "SPD"), na.rm=T)) -> tb
  86. ```
  87. Analogously we plot the results:
  88. ```{r, fig.width=7}
  89. pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>%
  90. filter(!is.na(on_fraction)) %>%
  91. bar_plot_fractions(x_variable = on_fraction,
  92. y_variable = value,
  93. fill = by_fraction,
  94. title = "Number of comments from fractions to fractions",
  95. xlab = "Commented fraction",
  96. ylab = "Number of comments",
  97. filllab = "Commenting fraction",
  98. flipped = FALSE,
  99. rotatelab = TRUE)
  100. ```