diff --git a/R/analyze.R b/R/analyze.R index 0f4e8e7..a67f472 100644 --- a/R/analyze.R +++ b/R/analyze.R @@ -66,6 +66,7 @@ party_order <- factor(c("Fraktionslos", "AfD&Fraktionslos", #' @param flipped if TRUE draw bars horizontally, else vertically. Default is TRUE #' @param position default is 'dodge' #' @param reorder Either reorder fraction factor by variable value or reorder fraction factor by party seat order in parliament (default). +#' @param rotatelab Default is FALSE. If true turns the labels 90 degrees to the axis. #' #' plot data from tb in the following way: for each item in x_variable show the corresponding value in y_variable. #' Then color the plot depending on the fill value. @@ -84,7 +85,8 @@ bar_plot_fractions <- function(tb, filllab = "Fraction", flipped = TRUE, position = "dodge", - reorder = FALSE) { + reorder = FALSE, + rotatelab = FALSE) { # capture expressions in arguments fill <- enexpr(fill) y_variable <- enexpr(y_variable) @@ -103,8 +105,23 @@ bar_plot_fractions <- function(tb, else maps <- aes(x = factor(!!x_variable, levels = party_order), y = !!y_variable, fill = factor(!!fill, levels = party_order)) + + if(rotatelab){ # make a bar plot - ggplot(tb, maps) + + ggplot(tb, maps, rotate) + + scale_fill_manual(values = party_colors, na.value = "#555555") + + xlab(xlab) + + ylab(ylab) + + labs(fill = filllab) + + ggtitle(title) + + geom_bar(stat = "identity", position = position) + + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) -> + plt + } + + else{ + # make a bar plot + ggplot(tb, maps, rotate) + scale_fill_manual(values = party_colors, na.value = "#555555") + xlab(xlab) + ylab(ylab) + @@ -112,6 +129,8 @@ bar_plot_fractions <- function(tb, ggtitle(title) + geom_bar(stat = "identity", position = position) -> plt + } + # if flipped == TRUE, draw bars horizontally (default TRUE) if (flipped) plt + coord_flip() else plt } diff --git a/man/bar_plot_fractions.Rd b/man/bar_plot_fractions.Rd index 4bc1122..1236ac9 100644 --- a/man/bar_plot_fractions.Rd +++ b/man/bar_plot_fractions.Rd @@ -15,7 +15,8 @@ bar_plot_fractions( filllab = "Fraction", flipped = TRUE, position = "dodge", - reorder = FALSE + reorder = FALSE, + rotatelab = FALSE ) } \arguments{ @@ -39,7 +40,9 @@ bar_plot_fractions( \item{position}{default is 'dodge'} -\item{reorder}{Either reorder fraction factor by variable value or reorder fraction factor by party seat order in parliament (default). +\item{reorder}{Either reorder fraction factor by variable value or reorder fraction factor by party seat order in parliament (default).} + +\item{rotatelab}{Default is FALSE. If true turns the labels 90 degrees to the axis. plot data from tb in the following way: for each item in x_variable show the corresponding value in y_variable. Then color the plot depending on the fill value. diff --git a/vignettes/funwithdata.Rmd b/vignettes/funwithdata.Rmd index 51950df..16bd7b9 100644 --- a/vignettes/funwithdata.Rmd +++ b/vignettes/funwithdata.Rmd @@ -73,7 +73,8 @@ find_word(res, "Kohleausstieg") %>% arrange(desc(n)) %>% bar_plot_fractions(title = "Parties using the word 'Kohleausstieg' the most (absolutely)", ylab = "Number of uses of 'Kohleausstieg'", - flipped = F) + flipped = F, + rotatelab = TRUE) ``` ### Who gives the most speeches? @@ -127,7 +128,8 @@ pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>% xlab = "Applauded fraction", ylab = "Rounds of applauses", filllab = "Applauding fraction", - flipped = FALSE) + flipped = FALSE, + rotatelab = TRUE) ``` @@ -157,7 +159,8 @@ pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>% xlab = "Commented fraction", ylab = "Number of comments", filllab = "Commenting fraction", - flipped = FALSE) + flipped = FALSE, + rotatelab = TRUE) ``` ### When are which topics discussed the most? diff --git a/vignettes/interaction.Rmd b/vignettes/interaction.Rmd index 05a6c40..6c1768a 100644 --- a/vignettes/interaction.Rmd +++ b/vignettes/interaction.Rmd @@ -79,7 +79,8 @@ pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>% xlab = "Applauded fraction", ylab = "Rounds of applauses", filllab = "Applauding fraction", - flipped = FALSE) + flipped = FALSE, + rotatelab = TRUE) ``` @@ -109,5 +110,6 @@ pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>% xlab = "Commented fraction", ylab = "Number of comments", filllab = "Commenting fraction", - flipped = FALSE) + flipped = FALSE, + rotatelab = TRUE) ```