improve bar plot fraktionen helper

This commit is contained in:
2021-08-03 11:33:24 +02:00
parent 354d18050b
commit e2caf1ff4e
2 changed files with 55 additions and 17 deletions
+42 -7
View File
@@ -20,20 +20,55 @@ party_colors <- c(
SPD="#DF0B25", SPD="#DF0B25",
"BÜNDNIS 90 / DIE GRÜNEN"="#4A932B", "BÜNDNIS 90 / DIE GRÜNEN"="#4A932B",
"DIE LINKE"="#BC3475", "DIE LINKE"="#BC3475",
"AfD&Fraktionslos"="#1A9FDD", "AfD&Fraktionslos"="#AAAAFF",
Fraktionslos="#FEEB34" 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 #' @export
bar_plot_fraktionen <- function(tb, variable, fill, title=NULL, xlab = "Fraction", bar_plot_fraktionen <- function(tb,
ylab="n", filllab="Fraction") { x_variable = NULL, # default is fraktion
ggplot(tb, aes(x = reorder(fraktion, -{{variable}}), y = {{variable}}, fill = {{fill}})) + y_variable = NULL, # default is n
scale_fill_manual(values = party_colors) + 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) + xlab(xlab) +
ylab(ylab) + ylab(ylab) +
labs(fill = filllab) + labs(fill = filllab) +
ggtitle(title) + 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 # Counts how many talks do match a given pattern and summarises by date
+13 -10
View File
@@ -55,7 +55,8 @@ join_redner(res$reden, res) %>%
group_by(fraktion) %>% group_by(fraktion) %>%
summarize(n = n()) %>% summarize(n = n()) %>%
arrange(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: or counting the occurences of a given word:
@@ -69,9 +70,9 @@ find_word(res, "Kohleausstieg") %>%
group_by(fraktion) %>% group_by(fraktion) %>%
summarize(n = n()) %>% summarize(n = n()) %>%
arrange(desc(n)) %>% arrange(desc(n)) %>%
bar_plot_fraktionen(n, fill = fraktion, bar_plot_fraktionen(title = "Parties using the word 'Kohleausstieg' the most (absolutely)",
title = "Parties using the word 'Kohleausstieg' the most (absolutely)", ylab = "Number of uses of 'Kohleausstieg'",
ylab = "Number of uses of 'Kohleausstieg'") flipped = F)
``` ```
### Who gives the most speeches? ### 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} ```{r, fig.width=7}
pivot_longer(tb, where(is.numeric), "by_fraktion", "count") %>% pivot_longer(tb, where(is.numeric), "by_fraktion", "count") %>%
filter(!is.na(on_fraktion)) %>% filter(!is.na(on_fraktion)) %>%
rename(fraktion = on_fraktion) %>% bar_plot_fraktionen(x_variable = on_fraktion,
bar_plot_fraktionen(value, y_variable = value,
fill = by_fraktion, fill = by_fraktion,
title = "Number of rounds of applauses from fractions to fractions", title = "Number of rounds of applauses from fractions to fractions",
xlab = "Applauded fraction", xlab = "Applauded fraction",
ylab = "Rounds of applauses", 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} ```{r, fig.width=7}
pivot_longer(tb, where(is.numeric), "by_fraktion", "count") %>% pivot_longer(tb, where(is.numeric), "by_fraktion", "count") %>%
filter(!is.na(on_fraktion)) %>% filter(!is.na(on_fraktion)) %>%
rename(fraktion = on_fraktion) %>% bar_plot_fraktionen(x_variable = on_fraktion,
bar_plot_fraktionen(value, y_variable = value,
fill = by_fraktion, fill = by_fraktion,
title = "Number of comments from fractions to fractions", title = "Number of comments from fractions to fractions",
xlab = "Commented fraction", xlab = "Commented fraction",
ylab = "Number of comments", ylab = "Number of comments",
filllab = "Commenting fraction") filllab = "Commenting fraction",
flipped = FALSE)
``` ```
### When are which topics discussed the most? ### When are which topics discussed the most?