An R package to analyze the parliamentary records of the 19th legislative period of the Bundestag, the German parliament.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.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. #'Assign speaker-name to speaker-ID
  8. #'
  9. #' Assign speaker-name to speaker-ID.
  10. #'
  11. #' @param tb tibble
  12. #' @param res list of tibbles
  13. #' @param fraction_only If TRUE select only from "fraction"
  14. #'
  15. #' @export
  16. join_speaker <- function(tb, res, fraction_only = F) {
  17. joined <- left_join(tb, res$speaker, by=c("speaker" = "id"))
  18. if (fraction_only) select(joined, "fraction")
  19. else joined
  20. }
  21. #'Assignment of the official colors to the parties
  22. #'
  23. #' @export
  24. party_colors <- c(
  25. AfD="#1A9FDD",
  26. FDP="#FEEB34",
  27. "CDU/CSU"="#000000",
  28. SPD="#DF0B25",
  29. "BÜNDNIS 90 / DIE GRÜNEN"="#4A932B",
  30. "DIE LINKE"="#BC3475",
  31. "AfD&Fraktionslos"="#AAAAFF",
  32. Fraktionslos="#AAAAAA"
  33. )
  34. party_order <- factor(c("Fraktionslos", "AfD&Fraktionslos",
  35. "DIE LINKE", "BÜNDNIS 90 / DIE GRÜNEN", "SPD", "CDU/CSU",
  36. "FDP", "AfD", NA_character_))
  37. #'Bar chart of the fractions
  38. #'
  39. #' @param tb The tibble to be analyzed.
  40. #' @param x_variable Default is fraction.
  41. #' @param y_variable Default is n.
  42. #' @param fill Default is fraction.
  43. #' @param title Title of the chart.
  44. #' @param xlab Description of x-Lab. Default is Fraction.
  45. #' @param ylab Description of y-Lab. Default is n.
  46. #' @param filllab Default is Fraction.
  47. #' @param flipped Default is TRUE. If TRUE draw bars horizontally, else vertically.
  48. #' @param position Default is dodge.
  49. #' @param reorder Either reorder fraction factor by variable value or reorder fraction factor by party seat order in parliament (default).
  50. #'
  51. #' @export
  52. bar_plot_fractions <- function(tb,
  53. x_variable = NULL, # default is fraction
  54. y_variable = NULL, # default is n
  55. fill = NULL, # default is fraction
  56. title = NULL,
  57. xlab = "Fraction",
  58. ylab = "n",
  59. filllab = "Fraction",
  60. flipped = TRUE,
  61. position = "dodge",
  62. reorder = FALSE) {
  63. # capture expressions in arguments
  64. fill <- enexpr(fill)
  65. y_variable <- enexpr(y_variable)
  66. x_variable <- enexpr(x_variable)
  67. # set default values
  68. if (is.null(fill)) fill <- expr(fraction)
  69. if (is.null(y_variable)) y_variable <- expr(n)
  70. if (is.null(x_variable)) x_variable <- expr(fraction)
  71. # either reorder fraction factor by variable value
  72. if (reorder) maps <- aes(x = reorder(!!x_variable, -!!y_variable),
  73. y = !!y_variable,
  74. fill = reorder(!!fill, -!!y_variable))
  75. # or reorder fraction factor by party seat order in parliament (default)
  76. else maps <- aes(x = factor(!!x_variable, levels = party_order),
  77. y = !!y_variable,
  78. fill = factor(!!fill, levels = party_order))
  79. # make a bar plot
  80. ggplot(tb, maps) +
  81. scale_fill_manual(values = party_colors, na.value = "#555555") +
  82. xlab(xlab) +
  83. ylab(ylab) +
  84. labs(fill = filllab) +
  85. ggtitle(title) +
  86. geom_bar(stat = "identity", position = position) ->
  87. plt
  88. # if flipped == TRUE, draw bars horizontally (default TRUE)
  89. if (flipped) plt + coord_flip() else plt
  90. }
  91. #'Word usage summarised by date
  92. #'
  93. #' Counts how many talks do match a given pattern and summarises by date.
  94. #'
  95. #' @param res List of Tibbles to be analysed.
  96. #' @param patterns Words to look up.
  97. #' @param name ?
  98. #' @param tidy Default is FALSE.
  99. #'
  100. #' @export
  101. word_usage_by_date <- function(res, patterns, name, tidy=F) {
  102. tb <- res$talks
  103. nms <- names(patterns)
  104. for (i in seq_along(patterns)) {
  105. if (!is.null(nms)) name <- nms[[i]]
  106. else name <- patterns[[i]]
  107. tb <- mutate(tb, {{name}} := str_count(content, patterns[[i]]))
  108. }
  109. left_join(tb, res$speeches, by=c("speech_id" = "id")) %>%
  110. group_by(date) %>%
  111. summarize(across(where(is.numeric), sum)) %>%
  112. arrange(date) -> tb
  113. if (!tidy) pivot_longer(tb, where(is.numeric) , names_to = "pattern", values_to="count")
  114. else tb
  115. }