Quellcode durchsuchen

improve bar plot fraktionen helper

genderequality-alternative
flavis vor 4 Jahren
Ursprung
Commit
e2caf1ff4e
2 geänderte Dateien mit 55 neuen und 17 gelöschten Zeilen
  1. +42
    -7
      R/analyze.R
  2. +13
    -10
      vignettes/funwithdata.Rmd

+ 42
- 7
R/analyze.R Datei anzeigen

@@ -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


+ 13
- 10
vignettes/funwithdata.Rmd Datei anzeigen

@@ -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?


Laden…
Abbrechen
Speichern