|
|
|
@@ -78,17 +78,17 @@ party_order <- factor(c("Fraktionslos", "AfD&Fraktionslos", |
|
|
|
#' |
|
|
|
#' @export |
|
|
|
bar_plot_fractions <- function(tb, |
|
|
|
x_variable = NULL, # default is fraction |
|
|
|
y_variable = NULL, # default is n |
|
|
|
fill = NULL, # default is fraction |
|
|
|
title = NULL, |
|
|
|
xlab = "Fraction", |
|
|
|
ylab = "n", |
|
|
|
filllab = "Fraction", |
|
|
|
flipped = TRUE, |
|
|
|
position = "dodge", |
|
|
|
reorder = FALSE, |
|
|
|
rotatelab = FALSE) { |
|
|
|
x_variable = NULL, # default is fraction |
|
|
|
y_variable = NULL, # default is n |
|
|
|
fill = NULL, # default is fraction |
|
|
|
title = NULL, |
|
|
|
xlab = "Fraction", |
|
|
|
ylab = "n", |
|
|
|
filllab = "Fraction", |
|
|
|
flipped = TRUE, |
|
|
|
position = "dodge", |
|
|
|
reorder = FALSE, |
|
|
|
rotatelab = FALSE) { |
|
|
|
# capture expressions in arguments |
|
|
|
fill <- enexpr(fill) |
|
|
|
y_variable <- enexpr(y_variable) |
|
|
|
@@ -99,6 +99,29 @@ bar_plot_fractions <- function(tb, |
|
|
|
if (is.null(y_variable)) y_variable <- expr(n) |
|
|
|
if (is.null(x_variable)) x_variable <- expr(fraction) |
|
|
|
|
|
|
|
# check if variables exist |
|
|
|
if (!rlang::expr_text(x_variable) %in% names(tb)) |
|
|
|
stop(paste0(rlang::expr_text(x_variable), |
|
|
|
" is not a column of tb. Did you set x_variable accordingly?"), |
|
|
|
.call = NULL) |
|
|
|
if (!rlang::expr_text(y_variable) %in% names(tb)) |
|
|
|
stop(paste0(rlang::expr_text(y_variable), |
|
|
|
" is not a column of tb. Did you set y_variable accordingly?"), |
|
|
|
.call = NULL) |
|
|
|
if (!rlang::expr_text(fill) %in% names(tb)) |
|
|
|
stop(paste0(rlang::expr_text(fill), |
|
|
|
" is not a column of tb. Did you set fill accordingly?"), |
|
|
|
.call = NULL) |
|
|
|
|
|
|
|
# check argument types |
|
|
|
stopifnot("title has to be of type character or NULL" = is.character(title) || is.null(title)) |
|
|
|
stopifnot("xlab has to be of type character" = is.character(xlab)) |
|
|
|
stopifnot("ylab has to be of type character" = is.character(ylab)) |
|
|
|
stopifnot("filllab has to be of type character" = is.character(filllab)) |
|
|
|
stopifnot("flipped has to be of type logical" = is.logical(flipped)) |
|
|
|
stopifnot("rotatelab has to be of type logical" = is.logical(rotatelab)) |
|
|
|
stopifnot("reorder has to be of type logical" = is.logical(reorder)) |
|
|
|
|
|
|
|
# either reorder fraction factor by variable value |
|
|
|
if (reorder) maps <- aes(x = reorder(!!x_variable, -!!y_variable), |
|
|
|
y = !!y_variable, |
|
|
|
@@ -108,22 +131,8 @@ bar_plot_fractions <- function(tb, |
|
|
|
y = !!y_variable, |
|
|
|
fill = factor(!!fill, levels = party_order)) |
|
|
|
|
|
|
|
if(rotatelab){ |
|
|
|
# make a bar plot |
|
|
|
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) + |
|
|
|
ggplot(tb, maps) + |
|
|
|
scale_fill_manual(values = party_colors, na.value = "#555555") + |
|
|
|
xlab(xlab) + |
|
|
|
ylab(ylab) + |
|
|
|
@@ -131,7 +140,10 @@ bar_plot_fractions <- function(tb, |
|
|
|
ggtitle(title) + |
|
|
|
geom_bar(stat = "identity", position = position) -> |
|
|
|
plt |
|
|
|
} |
|
|
|
|
|
|
|
# if rotatelab == TRUE, rotate x labels by 90 degrees |
|
|
|
if (rotatelab) |
|
|
|
plt + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) -> plt |
|
|
|
|
|
|
|
# if flipped == TRUE, draw bars horizontally (default TRUE) |
|
|
|
if (flipped) plt + coord_flip() else plt |
|
|
|
|