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