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ů.

92 řádky
3.2KB

  1. #' @export
  2. find_word <- function(res, word) {
  3. talks <- res$talks
  4. mutate(talks, occurences = sapply(str_match_all(talks$content, regex(word, ignore_case = TRUE)),
  5. nrow))
  6. }
  7. #' @export
  8. join_speaker <- function(tb, res, fraktion_only = F) {
  9. joined <- left_join(tb, res$speaker, by=c("speaker" = "id"))
  10. if (fraktion_only) select(joined, "fraktion")
  11. else joined
  12. }
  13. #' @export
  14. party_colors <- c(
  15. AfD="#1A9FDD",
  16. FDP="#FEEB34",
  17. "CDU/CSU"="#000000",
  18. SPD="#DF0B25",
  19. "BÜNDNIS 90 / DIE GRÜNEN"="#4A932B",
  20. "DIE LINKE"="#BC3475",
  21. "AfD&Fraktionslos"="#AAAAFF",
  22. Fraktionslos="#AAAAAA"
  23. )
  24. party_order <- factor(c("Fraktionslos", "AfD&Fraktionslos",
  25. "DIE LINKE", "BÜNDNIS 90 / DIE GRÜNEN", "SPD", "CDU/CSU",
  26. "FDP", "AfD", NA_character_))
  27. #' @export
  28. bar_plot_fractions <- function(tb,
  29. x_variable = NULL, # default is fraktion
  30. y_variable = NULL, # default is n
  31. fill = NULL, # default is fraktion
  32. title = NULL,
  33. xlab = "Fraction",
  34. ylab = "n",
  35. filllab = "Fraction",
  36. flipped = TRUE,
  37. position = "dodge",
  38. reorder = FALSE) {
  39. # capture expressions in arguments
  40. fill <- enexpr(fill)
  41. y_variable <- enexpr(y_variable)
  42. x_variable <- enexpr(x_variable)
  43. # set default values
  44. if (is.null(fill)) fill <- expr(fraktion)
  45. if (is.null(y_variable)) y_variable <- expr(n)
  46. if (is.null(x_variable)) x_variable <- expr(fraktion)
  47. # either reorder fraction factor by variable value
  48. if (reorder) maps <- aes(x = reorder(!!x_variable, -!!y_variable),
  49. y = !!y_variable,
  50. fill = reorder(!!fill, -!!y_variable))
  51. # or reorder fraction factor by party seat order in parliament (default)
  52. else maps <- aes(x = factor(!!x_variable, levels = party_order),
  53. y = !!y_variable,
  54. fill = factor(!!fill, levels = party_order))
  55. # make a bar plot
  56. ggplot(tb, maps) +
  57. scale_fill_manual(values = party_colors, na.value = "#555555") +
  58. xlab(xlab) +
  59. ylab(ylab) +
  60. labs(fill = filllab) +
  61. ggtitle(title) +
  62. geom_bar(stat = "identity", position = position) ->
  63. plt
  64. # if flipped == TRUE, draw bars horizontally (default TRUE)
  65. if (flipped) plt + coord_flip() else plt
  66. }
  67. # Counts how many talks do match a given pattern and summarises by date
  68. #
  69. #' @export
  70. word_usage_by_date <- function(res, patterns, name, tidy=F) {
  71. tb <- res$talks
  72. nms <- names(patterns)
  73. for (i in seq_along(patterns)) {
  74. if (!is.null(nms)) name <- nms[[i]]
  75. else name <- patterns[[i]]
  76. tb <- mutate(tb, {{name}} := str_count(content, patterns[[i]]))
  77. }
  78. left_join(tb, res$speeches, by=c("speech_id" = "id")) %>%
  79. group_by(date) %>%
  80. summarize(across(where(is.numeric), sum)) %>%
  81. arrange(date) -> tb
  82. if (!tidy) pivot_longer(tb, where(is.numeric) , names_to = "pattern", values_to="count")
  83. else tb
  84. }