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.

57 lines
1.7KB

  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_redner <- function(tb, res, fraktion_only = F) {
  9. joined <- left_join(tb, res$redner, by=c("redner" = "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"="#1A9FDD",
  22. Fraktionslos="#FEEB34"
  23. )
  24. #' @export
  25. bar_plot_fraktionen <- function(tb, variable, fill, title=NULL, xlab = "Fraction",
  26. ylab="n", filllab="Fraction") {
  27. ggplot(tb, aes(x = reorder(fraktion, -{{variable}}), y = {{variable}}, fill = {{fill}})) +
  28. scale_fill_manual(values = party_colors) +
  29. xlab(xlab) +
  30. ylab(ylab) +
  31. labs(fill = filllab) +
  32. ggtitle(title) +
  33. geom_bar(stat = "identity") + coord_flip()
  34. }
  35. # Counts how many talks do match a given pattern and summarises by date
  36. #
  37. #' @export
  38. word_usage_by_date <- function(res, patterns, name, tidy=F) {
  39. tb <- res$talks
  40. nms <- names(patterns)
  41. for (i in seq_along(patterns)) {
  42. if (!is.null(nms)) name <- nms[[i]]
  43. else name <- patterns[[i]]
  44. tb <- mutate(tb, {{name}} := str_count(content, patterns[[i]]))
  45. }
  46. left_join(tb, res$reden, by=c("rede_id" = "id")) %>%
  47. group_by(date) %>%
  48. summarize(across(where(is.numeric), sum)) %>%
  49. arrange(date) -> tb
  50. if (!tidy) pivot_longer(tb, where(is.numeric) , names_to = "pattern", values_to="count")
  51. else tb
  52. }