add rotatelab to bar_plot_fractions

This commit is contained in:
Leon Burgard
2021-08-09 11:00:14 +02:00
parent 1db9e3f59a
commit 545e5c9bab
4 changed files with 36 additions and 9 deletions
+21 -2
View File
@@ -66,6 +66,7 @@ party_order <- factor(c("Fraktionslos", "AfD&Fraktionslos",
#' @param flipped if TRUE draw bars horizontally, else vertically. Default is TRUE #' @param flipped if TRUE draw bars horizontally, else vertically. Default is TRUE
#' @param position default is 'dodge' #' @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 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. #' 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. #' Then color the plot depending on the fill value.
@@ -84,7 +85,8 @@ bar_plot_fractions <- function(tb,
filllab = "Fraction", filllab = "Fraction",
flipped = TRUE, flipped = TRUE,
position = "dodge", position = "dodge",
reorder = FALSE) { reorder = FALSE,
rotatelab = FALSE) {
# capture expressions in arguments # capture expressions in arguments
fill <- enexpr(fill) fill <- enexpr(fill)
y_variable <- enexpr(y_variable) y_variable <- enexpr(y_variable)
@@ -103,8 +105,23 @@ bar_plot_fractions <- function(tb,
else maps <- aes(x = factor(!!x_variable, levels = party_order), else maps <- aes(x = factor(!!x_variable, levels = party_order),
y = !!y_variable, y = !!y_variable,
fill = factor(!!fill, levels = party_order)) fill = factor(!!fill, levels = party_order))
if(rotatelab){
# make a bar plot # 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") + scale_fill_manual(values = party_colors, na.value = "#555555") +
xlab(xlab) + xlab(xlab) +
ylab(ylab) + ylab(ylab) +
@@ -112,6 +129,8 @@ bar_plot_fractions <- function(tb,
ggtitle(title) + ggtitle(title) +
geom_bar(stat = "identity", position = position) -> geom_bar(stat = "identity", position = position) ->
plt plt
}
# if flipped == TRUE, draw bars horizontally (default TRUE) # if flipped == TRUE, draw bars horizontally (default TRUE)
if (flipped) plt + coord_flip() else plt if (flipped) plt + coord_flip() else plt
} }
+5 -2
View File
@@ -15,7 +15,8 @@ bar_plot_fractions(
filllab = "Fraction", filllab = "Fraction",
flipped = TRUE, flipped = TRUE,
position = "dodge", position = "dodge",
reorder = FALSE reorder = FALSE,
rotatelab = FALSE
) )
} }
\arguments{ \arguments{
@@ -39,7 +40,9 @@ bar_plot_fractions(
\item{position}{default is 'dodge'} \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. 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. Then color the plot depending on the fill value.
+6 -3
View File
@@ -73,7 +73,8 @@ find_word(res, "Kohleausstieg") %>%
arrange(desc(n)) %>% arrange(desc(n)) %>%
bar_plot_fractions(title = "Parties using the word 'Kohleausstieg' the most (absolutely)", bar_plot_fractions(title = "Parties using the word 'Kohleausstieg' the most (absolutely)",
ylab = "Number of uses of 'Kohleausstieg'", ylab = "Number of uses of 'Kohleausstieg'",
flipped = F) flipped = F,
rotatelab = TRUE)
``` ```
### Who gives the most speeches? ### Who gives the most speeches?
@@ -127,7 +128,8 @@ pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>%
xlab = "Applauded fraction", xlab = "Applauded fraction",
ylab = "Rounds of applauses", ylab = "Rounds of applauses",
filllab = "Applauding fraction", 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", xlab = "Commented fraction",
ylab = "Number of comments", ylab = "Number of comments",
filllab = "Commenting fraction", filllab = "Commenting fraction",
flipped = FALSE) flipped = FALSE,
rotatelab = TRUE)
``` ```
### When are which topics discussed the most? ### When are which topics discussed the most?
+4 -2
View File
@@ -79,7 +79,8 @@ pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>%
xlab = "Applauded fraction", xlab = "Applauded fraction",
ylab = "Rounds of applauses", ylab = "Rounds of applauses",
filllab = "Applauding fraction", 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", xlab = "Commented fraction",
ylab = "Number of comments", ylab = "Number of comments",
filllab = "Commenting fraction", filllab = "Commenting fraction",
flipped = FALSE) flipped = FALSE,
rotatelab = TRUE)
``` ```