refactor bar_plot_fractions and add error handling
This commit is contained in:
@@ -19,6 +19,7 @@ Imports:
|
|||||||
lubridate,
|
lubridate,
|
||||||
pbapply,
|
pbapply,
|
||||||
purrr,
|
purrr,
|
||||||
|
rlang,
|
||||||
rvest,
|
rvest,
|
||||||
stringr,
|
stringr,
|
||||||
tibble,
|
tibble,
|
||||||
|
|||||||
+39
-27
@@ -78,17 +78,17 @@ party_order <- factor(c("Fraktionslos", "AfD&Fraktionslos",
|
|||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
bar_plot_fractions <- function(tb,
|
bar_plot_fractions <- function(tb,
|
||||||
x_variable = NULL, # default is fraction
|
x_variable = NULL, # default is fraction
|
||||||
y_variable = NULL, # default is n
|
y_variable = NULL, # default is n
|
||||||
fill = NULL, # default is fraction
|
fill = NULL, # default is fraction
|
||||||
title = NULL,
|
title = NULL,
|
||||||
xlab = "Fraction",
|
xlab = "Fraction",
|
||||||
ylab = "n",
|
ylab = "n",
|
||||||
filllab = "Fraction",
|
filllab = "Fraction",
|
||||||
flipped = TRUE,
|
flipped = TRUE,
|
||||||
position = "dodge",
|
position = "dodge",
|
||||||
reorder = FALSE,
|
reorder = FALSE,
|
||||||
rotatelab = 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)
|
||||||
@@ -99,6 +99,29 @@ bar_plot_fractions <- function(tb,
|
|||||||
if (is.null(y_variable)) y_variable <- expr(n)
|
if (is.null(y_variable)) y_variable <- expr(n)
|
||||||
if (is.null(x_variable)) x_variable <- expr(fraction)
|
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
|
# either reorder fraction factor by variable value
|
||||||
if (reorder) maps <- aes(x = reorder(!!x_variable, -!!y_variable),
|
if (reorder) maps <- aes(x = reorder(!!x_variable, -!!y_variable),
|
||||||
y = !!y_variable,
|
y = !!y_variable,
|
||||||
@@ -108,22 +131,8 @@ bar_plot_fractions <- function(tb,
|
|||||||
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, rotate) +
|
ggplot(tb, maps) +
|
||||||
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) +
|
||||||
@@ -131,7 +140,10 @@ bar_plot_fractions <- function(tb,
|
|||||||
ggtitle(title) +
|
ggtitle(title) +
|
||||||
geom_bar(stat = "identity", position = position) ->
|
geom_bar(stat = "identity", position = position) ->
|
||||||
plt
|
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 == TRUE, draw bars horizontally (default TRUE)
|
||||||
if (flipped) plt + coord_flip() else plt
|
if (flipped) plt + coord_flip() else plt
|
||||||
|
|||||||
Reference in New Issue
Block a user