An R package to analyze the parliamentary records of the 19th legislative period of the Bundestag, the German parliament.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

94 行
3.3KB

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