bar_reach() added

Replace hbar
Follow REACH theme by default
This commit is contained in:
gnoblet 2022-11-20 18:54:18 -04:00
parent d2ef1fdec4
commit efc9e0ffef
2 changed files with 74 additions and 152 deletions

74
R/bar.R Normal file
View file

@ -0,0 +1,74 @@
#' @title Simple bar chart
#'
#' @param df A data frame.
#' @param x A numeric column.
#' @param y A character column or coercible as a character column.
#' @param group Some grouping categorical column, e.g. administrative areas or population groups.
#' @param palette Palette name from 'pal_reach()'.
#' @param percent TRUE or FALSE. Should the x-labels be displayed as percentages? Default to TRUE.
#' @param reverse Boolean indicating whether the palette should be reversed.
#' @param family The font family for all plot's texts. Default to "Leelawadee".
#' @param alpha Transparency.
#' @param width Width.
#' @param x_title The x scale title. Default to NULL.
#' @param y_title The y scale title. Default to NULL.
#' @param group_title The group legend title. Default to NULL.
#' @param position Should the chart be stacked? Default to "dodge". Can take "dodge" and "stack".
#' @param title Plot title. Default to NULL.
#' @param subtitle Plot subtitle. Default to NULL.
#' @param caption Caption title string. Default to NULL.
#' @param text_size The size of all text other than the title, subtitle and caption. Defaults to 10.
#' @param title_size The size of the title text. Defaults to 14.
#' @param legend_position Position of the legend; Default to "right". Can take "right", "left", "top", "bottom" or "none".
#' @param legend_direction Direction of the legend. Default to "vertical". Can take "vertical" or "horizontal".
#' @param void Boolean to remove all elements from the plot. Default to FALSE.
#' @param ... Other arguments to be passed to "ggblanket::gg_col"
#'
#' @description `ggblanket` as internals for deciding whether the bar chart is horizontally readable.
#'
#' @return A bar chart
#'
#' @export
bar_reach <- function(df, x, y, group = NULL, percent = TRUE, palette = "main", reverse = FALSE, family = "Leelawadee", alpha = 1, width = 0.5, x_title = NULL, y_title = NULL, group_title = NULL, position = "dodge", title = NULL, subtitle = NULL, caption = NULL, text_size = 10, title_size = 14, legend_position = "right", legend_rev = TRUE, void = FALSE, ...){
pal <- pal_reach(palette)
if(is.null(pal)) rlang::warn(
c(paste0("There is no palette '", palette, "' for initiative 'reach'. Fallback to ggblanket's default color palette."),
"i" = paste0("Use `pal_reach(show_palettes = T)` to see the list of availabale palettes.")
)
)
if (percent) x_labels <- scales::percent else x_labels <- NULL
hbar <- df |>
ggblanket::gg_col(x = {{ x }},
y = {{ y }},
col = {{ group }},
x_title = x_title,
x_labels = x_labels,
y_title = y_title,
col_title = group_title,
alpha = alpha,
width = width,
position = position,
title = title,
subtitle = subtitle,
caption = caption,
col_legend_place = legend_position,
theme = theme_reach(
palette = palette,
reverse = reverse,
family = family,
text_size = text_size,
title_size = title_size,
plot_background_pal = "#FFFFFF",
panel_background_pal = "#FFFFFF",
legend_reverse = legend_rev,
void = FALSE
),
...
)
return(hbar)
}

152
R/hbar.R
View file

@ -1,152 +0,0 @@
#' @title Simple horizontal bar chart
#'
#' @param .tbl Some data
#' @param x Some numeric column on the x scale
#' @param y Some column on the y scale
#' @param group Some grouping categorical column, e.g. administrative areas
#' @param initiative Either "reach" or "agora" or "impact" for the color palette
#' @param palette The color palette from the initiative
#' @param width Width
#' @param x_title The x scale title. Default to empty string
#' @param y_title The y scale title. Default to empty string
#' @param group_title The group legend title. Defaut to NULL
#' @param font_family The font family. Default to "Leelawadee"
#' @param position Should the chart be stacked? Default to dodge
#' @param reverse Boolean indicating whether the color palette should be reversed
#' @param title Plot title. Default to empty string
#' @param subtitle Plot subtitle. Default to empty string
#' @param gg_theme Some ggplot2 theme
#' @param ... Other arguments to be passed to "ggblanket::gg_col"
#'
#' @return A horizontal bar chart
#'
#' @export
hbar <- function(.tbl, x, y, group = NULL, initiative = "reach", palette = "primary", width = 0.5, x_title = "", y_title = "", group_title = NULL, font_family = "Leelawadee", position = "dodge", reverse = FALSE, title = "", subtitle = "", gg_theme = NULL, ...){
if (!(initiative %in% c("reach", "agora", "impact"))) rlang::abort(c("Wrong `initiative` arg", "*" = paste0("Arg `initiative` cannot be: ", initiative), "i" = "It must be one of 'reach' or 'agora' or 'impact'"))
if (initiative == "reach") {
palette <- pal_reach(palette, reverse = reverse)
main_col <- cols_reach("main_grey")
if(is.null(palette)) rlang::warn(
c(paste0("There is no palette '", palette, "' for initiative 'reach'. Fallback to ggblanket's default color palette."),
"i" = paste0("Use `pal_reach(show_palettes = T)` to see the list of availabale palettes.")
)
)
}
if (initiative == "agora") {
palette <- pal_agora(palette, reverse = reverse)
main_col <- cols_agora("main_bordeaux")
if(is.null(palette)) rlang::warn(
c(paste0("There is no palette '", palette, "' for initiative 'agora'. Fallback to ggblanket's default color palette."),
"i" = paste0("Use `pal_agora(show_palettes = T)` to see the list of availabale palettes.")
)
)
}
if (initiative == "impact") rlang::warn("IMPACT colors are under development. Fallback to ggblanket's default.")
hbar <- .tbl |>
ggblanket::gg_col(x = {{ x }},
y = {{ y }},
col = {{ group }},
x_title = x_title,
y_title = y_title,
col_title = group_title,
alpha_fill = 1,
pal = palette,
width = width,
position = position,
stat = "identity",
title = "",
subtitle = "",
theme = gg_theme,
...
)
return(hbar)
}
#' @title Simple horizontal bar chart which scales x_labels to percentages
#'
#' @param .tbl Some data
#' @param x Some numeric column on the x scale
#' @param y Some column on the y scale
#' @param group Some grouping categorical column, e.g. administrative areas
#' @param initiative Either "reach" or "agora" or "impact" for the color palette
#' @param palette The color palette from the initiative
#' @param width Width
#' @param x_title The x scale title. Default to empty string
#' @param y_title The y scale title. Default to empty string
#' @param group_title The group legend title. Defaut to NULL
#' @param font_family The font family. Default to "Leelawadee"
#' @param position Should the chart be stacked? Default to dodge
#' @param reverse Boolean indicating whether the color palette should be reversed
#' @param title Plot title. Default to empty string
#' @param subtitle Plot subtitle. Default to empty string
#' @param gg_theme Some ggplot2 theme
#' @param ... Other arguments to be passed to "ggblanket::gg_col"
#'
#' @return A horizontal bar chart
#'
#' @export
hbar_percent <- function(.tbl, x, y, group = NULL, initiative = "reach", palette = "primary", width = 0.5, x_title = "", y_title = "", group_title = NULL, font_family = "Leelawadee", position = "dodge", reverse = FALSE, title = "", subtitle = "", gg_theme = NULL, ...){
if (!(initiative %in% c("reach", "agora", "impact"))) rlang::abort(c("Wrong `initiative` arg", "*" = paste0("Arg `initiative` cannot be: ", initiative), "i" = "It must be one of 'reach' or 'agora' or 'impact'"))
if (initiative == "reach") {
palette <- pal_reach(palette, reverse = reverse)
main_col <- cols_reach("main_grey")
if(is.null(palette)) rlang::warn(
c(paste0("There is no palette '", palette, "' for initiative 'reach'. Fallback to ggblanket's default color palette."),
"i" = paste0("Use `pal_reach(show_palettes = T)` to see the list of availabale palettes.")
)
)
}
if (initiative == "agora") {
palette <- pal_agora(palette, reverse = reverse)
main_col <- cols_agora("main_bordeaux")
if(is.null(palette)) rlang::warn(
c(paste0("There is no palette '", palette, "' for initiative 'agora'. Fallback to ggblanket's default color palette."),
"i" = paste0("Use `pal_agora(show_palettes = T)` to see the list of availabale palettes.")
)
)
}
if (initiative == "impact") rlang::warn("IMPACT colors are under development. Fallback to ggblanket's default.")
hbar <- .tbl |>
ggblanket::gg_col(x = {{ x }},
y = {{ y }},
col = {{ group }},
x_title = x_title,
y_title = y_title,
col_title = group_title,
alpha_fill = 1,
pal = palette,
width = width,
x_labels = scales::percent,
position = position,
stat = "identity",
title = "",
subtitle = "",
theme = gg_theme,
...
)
return(hbar)
}