From e2caf1ff4e930ac7297c3a12a9abe8f12d130fd7 Mon Sep 17 00:00:00 2001 From: flavis Date: Tue, 3 Aug 2021 11:33:24 +0200 Subject: [PATCH] improve bar plot fraktionen helper --- R/analyze.R | 49 +++++++++++++++++++++++++++++++++------ vignettes/funwithdata.Rmd | 23 ++++++++++-------- 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/R/analyze.R b/R/analyze.R index 1c406d5..088d0d8 100644 --- a/R/analyze.R +++ b/R/analyze.R @@ -20,20 +20,55 @@ party_colors <- c( SPD="#DF0B25", "BÜNDNIS 90 / DIE GRÜNEN"="#4A932B", "DIE LINKE"="#BC3475", - "AfD&Fraktionslos"="#1A9FDD", - Fraktionslos="#FEEB34" + "AfD&Fraktionslos"="#AAAAFF", + Fraktionslos="#AAAAAA" ) +party_order <- factor(c("Fraktionslos", "AfD&Fraktionslos", + "DIE LINKE", "BÜNDNIS 90 / DIE GRÜNEN", "SPD", "CDU/CSU", + "FDP", "AfD", NA_character_)) + #' @export -bar_plot_fraktionen <- function(tb, variable, fill, title=NULL, xlab = "Fraction", - ylab="n", filllab="Fraction") { - ggplot(tb, aes(x = reorder(fraktion, -{{variable}}), y = {{variable}}, fill = {{fill}})) + - scale_fill_manual(values = party_colors) + +bar_plot_fraktionen <- function(tb, + x_variable = NULL, # default is fraktion + y_variable = NULL, # default is n + fill = NULL, # default is fraktion + title = NULL, + xlab = "Fraction", + ylab = "n", + filllab = "Fraction", + flipped = TRUE, + position = "dodge", + reorder = FALSE) { + # capture expressions in arguments + fill <- enexpr(fill) + y_variable <- enexpr(y_variable) + x_variable <- enexpr(x_variable) + + # set default values + if (is.null(fill)) fill <- expr(fraktion) + if (is.null(y_variable)) y_variable <- expr(n) + if (is.null(x_variable)) x_variable <- expr(fraktion) + + # either reorder fraction factor by variable value + if (reorder) maps <- aes(x = reorder(!!x_variable, -!!y_variable), + y = !!y_variable, + fill = reorder(!!fill, -!!y_variable)) + # or reorder fraction factor by party seat order in parliament (default) + else maps <- aes(x = factor(!!x_variable, levels = party_order), + y = !!y_variable, + fill = factor(!!fill, levels = party_order)) + # make a bar plot + ggplot(tb, maps) + + scale_fill_manual(values = party_colors, na.value = "#555555") + xlab(xlab) + ylab(ylab) + labs(fill = filllab) + ggtitle(title) + - geom_bar(stat = "identity") + coord_flip() + geom_bar(stat = "identity", position = position) -> + plt + # if flipped == TRUE, draw bars horizontally (default TRUE) + if (flipped) plt + coord_flip() else plt } # Counts how many talks do match a given pattern and summarises by date diff --git a/vignettes/funwithdata.Rmd b/vignettes/funwithdata.Rmd index 833f1a7..d5dded1 100644 --- a/vignettes/funwithdata.Rmd +++ b/vignettes/funwithdata.Rmd @@ -55,7 +55,8 @@ join_redner(res$reden, res) %>% group_by(fraktion) %>% summarize(n = n()) %>% arrange(n) %>% - bar_plot_fraktionen(n, fill = fraktion, title="Number of speeches given by fraction", ylab="Number of speeches") + bar_plot_fraktionen(title="Number of speeches given by fraction", + ylab="Number of speeches") ``` or counting the occurences of a given word: @@ -69,9 +70,9 @@ find_word(res, "Kohleausstieg") %>% group_by(fraktion) %>% summarize(n = n()) %>% arrange(desc(n)) %>% - bar_plot_fraktionen(n, fill = fraktion, - title = "Parties using the word 'Kohleausstieg' the most (absolutely)", - ylab = "Number of uses of 'Kohleausstieg'") + bar_plot_fraktionen(title = "Parties using the word 'Kohleausstieg' the most (absolutely)", + ylab = "Number of uses of 'Kohleausstieg'", + flipped = F) ``` ### Who gives the most speeches? @@ -118,13 +119,14 @@ For plotting our results we reorganize them a bit and produce a bar plot: ```{r, fig.width=7} pivot_longer(tb, where(is.numeric), "by_fraktion", "count") %>% filter(!is.na(on_fraktion)) %>% - rename(fraktion = on_fraktion) %>% - bar_plot_fraktionen(value, + bar_plot_fraktionen(x_variable = on_fraktion, + y_variable = value, fill = by_fraktion, title = "Number of rounds of applauses from fractions to fractions", xlab = "Applauded fraction", ylab = "Rounds of applauses", - filllab = "Applauding fraction") + filllab = "Applauding fraction", + flipped = FALSE) ``` @@ -147,13 +149,14 @@ Analogously we plot the results: ```{r, fig.width=7} pivot_longer(tb, where(is.numeric), "by_fraktion", "count") %>% filter(!is.na(on_fraktion)) %>% - rename(fraktion = on_fraktion) %>% - bar_plot_fraktionen(value, + bar_plot_fraktionen(x_variable = on_fraktion, + y_variable = value, fill = by_fraktion, title = "Number of comments from fractions to fractions", xlab = "Commented fraction", ylab = "Number of comments", - filllab = "Commenting fraction") + filllab = "Commenting fraction", + flipped = FALSE) ``` ### When are which topics discussed the most?