fix funwithdata vignette, generalize bar plot helper
This commit is contained in:
+13
-7
@@ -12,21 +12,27 @@ join_redner <- function(tb, res, fraktion_only = F) {
|
|||||||
else joined
|
else joined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#' @export
|
||||||
party_colors <- c(
|
party_colors <- c(
|
||||||
SPD="#DF0B25",
|
|
||||||
"CDU/CSU"="#000000",
|
|
||||||
AfD="#1A9FDD",
|
AfD="#1A9FDD",
|
||||||
"AfD&Fraktionslos"="#1A9FDD",
|
|
||||||
"DIE LINKE"="#BC3475",
|
|
||||||
"BÜNDNIS 90 / DIE GRÜNEN"="#4A932B",
|
|
||||||
FDP="#FEEB34",
|
FDP="#FEEB34",
|
||||||
|
"CDU/CSU"="#000000",
|
||||||
|
SPD="#DF0B25",
|
||||||
|
"BÜNDNIS 90 / DIE GRÜNEN"="#4A932B",
|
||||||
|
"DIE LINKE"="#BC3475",
|
||||||
|
"AfD&Fraktionslos"="#1A9FDD",
|
||||||
Fraktionslos="#FEEB34"
|
Fraktionslos="#FEEB34"
|
||||||
)
|
)
|
||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
bar_plot_fraktionen <- function(tb) {
|
bar_plot_fraktionen <- function(tb, variable, fill, title=NULL, xlab = "Fraction",
|
||||||
ggplot(tb, aes(x = reorder(fraktion, -n), y = n, fill = fraktion)) +
|
ylab="n", filllab="Fraction") {
|
||||||
|
ggplot(tb, aes(x = reorder(fraktion, -{{variable}}), y = {{variable}}, fill = {{fill}})) +
|
||||||
scale_fill_manual(values = party_colors) +
|
scale_fill_manual(values = party_colors) +
|
||||||
|
xlab(xlab) +
|
||||||
|
ylab(ylab) +
|
||||||
|
labs(fill = filllab) +
|
||||||
|
ggtitle(title) +
|
||||||
geom_bar(stat = "identity")
|
geom_bar(stat = "identity")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ knitr::opts_chunk$set(
|
|||||||
library(hateimparlament)
|
library(hateimparlament)
|
||||||
library(dplyr)
|
library(dplyr)
|
||||||
library(ggplot2)
|
library(ggplot2)
|
||||||
|
library(stringr)
|
||||||
|
library(tidyr)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Preparation of data
|
## Preparation of data
|
||||||
@@ -48,25 +50,28 @@ talks <- res$talks
|
|||||||
## Analysis
|
## Analysis
|
||||||
|
|
||||||
Now we can start analysing our parsed dataset, e.g. find out which party gives the most talks:
|
Now we can start analysing our parsed dataset, e.g. find out which party gives the most talks:
|
||||||
```{r, fig.width=8}
|
```{r, fig.width=7}
|
||||||
join_redner(res$reden, res) %>%
|
join_redner(res$reden, res) %>%
|
||||||
group_by(fraktion) %>%
|
group_by(fraktion) %>%
|
||||||
summarize(n = n()) %>%
|
summarize(n = n()) %>%
|
||||||
arrange(n) %>%
|
arrange(n) %>%
|
||||||
bar_plot_fraktionen()
|
bar_plot_fraktionen(n, fill = fraktion, title="Number of speeches given by fraction", ylab="Number of speeches")
|
||||||
```
|
```
|
||||||
|
|
||||||
### Count a word occurence
|
or counting the occurences of a given word:
|
||||||
|
|
||||||
```{r, fig.width=8}
|
```{r, fig.width=7}
|
||||||
find_word(res, "hitler") %>%
|
find_word(res, "Kohleausstieg") %>%
|
||||||
filter(occurences > 0) %>%
|
filter(occurences > 0) %>%
|
||||||
join_redner(res) %>%
|
join_redner(res) %>%
|
||||||
select(content, fraktion) %>%
|
select(content, fraktion) %>%
|
||||||
|
filter(!is.na(fraktion)) %>%
|
||||||
group_by(fraktion) %>%
|
group_by(fraktion) %>%
|
||||||
summarize(n = n()) %>%
|
summarize(n = n()) %>%
|
||||||
arrange(desc(n)) %>%
|
arrange(desc(n)) %>%
|
||||||
bar_plot_fraktionen()
|
bar_plot_fraktionen(n, fill = fraktion,
|
||||||
|
title = "Parties using the word 'Kohleausstieg' the most (absolutely)",
|
||||||
|
ylab = "Number of uses of 'Kohleausstieg'")
|
||||||
```
|
```
|
||||||
|
|
||||||
### Who gives the most speeches?
|
### Who gives the most speeches?
|
||||||
@@ -105,9 +110,24 @@ res$applause %>%
|
|||||||
"CDU/CSU" = sum(`CDU_CSU`),
|
"CDU/CSU" = sum(`CDU_CSU`),
|
||||||
"DIE LINKE" = sum(`DIE_LINKE`),
|
"DIE LINKE" = sum(`DIE_LINKE`),
|
||||||
"FDP" = sum(`FDP`),
|
"FDP" = sum(`FDP`),
|
||||||
"SPD" = sum(`SPD`))
|
"SPD" = sum(`SPD`)) -> tb
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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,
|
||||||
|
fill = by_fraktion,
|
||||||
|
title = "Number of rounds of applauses from fractions to fractions",
|
||||||
|
xlab = "Applauded fraction",
|
||||||
|
ylab = "Rounds of applauses",
|
||||||
|
filllab = "Applauding fraction")
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Which party comments the most on which parties?
|
### Which party comments the most on which parties?
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
@@ -120,12 +140,25 @@ res$comments %>%
|
|||||||
`CDU/CSU` = sum(str_detect(by_fraktion, "CDU/CSU"), na.rm = T),
|
`CDU/CSU` = sum(str_detect(by_fraktion, "CDU/CSU"), na.rm = T),
|
||||||
`DIE LINKE` = sum(str_detect(by_fraktion, "DIE LINKE"), na.rm=T),
|
`DIE LINKE` = sum(str_detect(by_fraktion, "DIE LINKE"), na.rm=T),
|
||||||
`FDP` = sum(str_detect(by_fraktion, "FDP"), na.rm=T),
|
`FDP` = sum(str_detect(by_fraktion, "FDP"), na.rm=T),
|
||||||
`SPD` = sum(str_detect(by_fraktion, "SPD"), na.rm=T))
|
`SPD` = sum(str_detect(by_fraktion, "SPD"), na.rm=T)) -> tb
|
||||||
|
```
|
||||||
|
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,
|
||||||
|
fill = by_fraktion,
|
||||||
|
title = "Number of comments from fractions to fractions",
|
||||||
|
xlab = "Commented fraction",
|
||||||
|
ylab = "Number of comments",
|
||||||
|
filllab = "Commenting fraction")
|
||||||
```
|
```
|
||||||
|
|
||||||
### When are which topics discussed the most?
|
### When are which topics discussed the most?
|
||||||
|
|
||||||
```{r, fig.width=8}
|
```{r, fig.width=7}
|
||||||
pandemic_pattern <- "(?i)virus|corona|covid|lockdown"
|
pandemic_pattern <- "(?i)virus|corona|covid|lockdown"
|
||||||
climate_pattern <- "(?i)klimawandel|erderwärmung|co2|treibhaus|methan|kyoto-protokoll|klimaabkommen"
|
climate_pattern <- "(?i)klimawandel|erderwärmung|co2|treibhaus|methan|kyoto-protokoll|klimaabkommen"
|
||||||
pension_pattern <- "(?i)rente|pension|altersarmut"
|
pension_pattern <- "(?i)rente|pension|altersarmut"
|
||||||
|
|||||||
Reference in New Issue
Block a user