Tries and errors for backup

This commit is contained in:
gnoblet 2023-07-17 08:32:56 +02:00
parent 515a94fbb5
commit 960140ad95
2 changed files with 17 additions and 10 deletions

21
R/bar.R
View file

@ -16,13 +16,15 @@
#' @param subtitle Plot subtitle. Default to NULL. #' @param subtitle Plot subtitle. Default to NULL.
#' @param caption Plot caption. Default to NULL. #' @param caption Plot caption. Default to NULL.
#' @param add_text TRUE or FALSE. Add the value as text. #' @param add_text TRUE or FALSE. Add the value as text.
#' @param add_text_threshold_display Minimum value to add the text label.
#' @param add_text_color Text color.
#' @param add_text_suffix If percent is FALSE, should we add a suffix to the text label? #' @param add_text_suffix If percent is FALSE, should we add a suffix to the text label?
#' @param theme Whatever theme. Default to theme_reach(). #' @param theme Whatever theme. Default to theme_reach().
#' #'
#' @return A bar chart #' @return A bar chart
#' #'
#' @export #' @export
bar <- function(df, x, y, group = NULL, flip = TRUE, percent = TRUE, wrap = NULL, position = "dodge", alpha = 1, x_title = NULL, y_title = NULL, group_title = NULL, title = NULL, subtitle = NULL, caption = NULL, add_text = FALSE, add_text_suffix = "", theme = theme_reach()){ bar <- function(df, x, y, group = NULL, flip = TRUE, percent = TRUE, wrap = NULL, position = "dodge", alpha = 1, x_title = NULL, y_title = NULL, group_title = NULL, title = NULL, subtitle = NULL, caption = NULL, width = 0.9, add_text = FALSE, add_text_threshold_display = 5, add_text_color = "white", add_text_suffix = "", theme = theme_reach()){
# To do : # To do :
# - automate bar width and text size, or at least give the flexibility and still center text # - automate bar width and text size, or at least give the flexibility and still center text
@ -51,8 +53,8 @@ bar <- function(df, x, y, group = NULL, flip = TRUE, percent = TRUE, wrap = NULL
fill = group_title fill = group_title
) )
width <- 0.5 width <- width
dodge_width <- 0.5 dodge_width <- width
# Should the graph use position_fill? # Should the graph use position_fill?
if (position == "stack"){ if (position == "stack"){
@ -108,27 +110,32 @@ bar <- function(df, x, y, group = NULL, flip = TRUE, percent = TRUE, wrap = NULL
# Add text labels # Add text labels
if (add_text) { if (add_text) {
df <- dplyr::mutate(df, "y_threshold" = ifelse({{ y }} >= add_text_threshold_display, {{ y }}, NA ))
if (percent) { if (percent) {
g <- g + ggplot2::geom_text( g <- g + ggplot2::geom_text(
data = df,
ggplot2::aes( ggplot2::aes(
label = scales::label_percent( label = scales::label_percent(
accuracy = 1, accuracy = 1,
decimal.mark = ",", decimal.mark = ",",
suffix = " %")({{ y }}), suffix = " %")(!!rlang::sym("y_threshold")),
group = {{ group }}), group = {{ group }}),
hjust = hjust_flip, hjust = hjust_flip,
vjust = vjust_flip, vjust = vjust_flip,
color = "white", color = add_text_color,
fontface = "bold", fontface = "bold",
position = ggplot2::position_dodge(width = dodge_width)) position = ggplot2::position_dodge(width = dodge_width))
} else { } else {
g <- g + ggplot2::geom_text( g <- g + ggplot2::geom_text(
data = df,
ggplot2::aes( ggplot2::aes(
label = paste0(round({{ y }}), add_text_suffix), label = paste0(round(!!rlang::sym("y_threshold")), add_text_suffix),
group = {{ group }}), group = {{ group }}),
hjust = hjust_flip, hjust = hjust_flip,
vjust = vjust_flip, vjust = vjust_flip,
color = "white", color = add_text_color,
fontface = "bold", fontface = "bold",
position = ggplot2::position_dodge(width = dodge_width)) position = ggplot2::position_dodge(width = dodge_width))
} }

View file

@ -11,7 +11,7 @@
#' @param arrange TRUE or FALSE. Arrange by highest percentage first. #' @param arrange TRUE or FALSE. Arrange by highest percentage first.
#' @param hole_size Hole size. Default to 3. If less than 2, back to a pie chart. #' @param hole_size Hole size. Default to 3. If less than 2, back to a pie chart.
#' @param add_text TRUE or FALSE. Add the value as text. #' @param add_text TRUE or FALSE. Add the value as text.
#' @param add_text_treshold_display Minimum value to add the text label. #' @param add_text_threshold_display Minimum value to add the text label.
#' @param add_text_color Text color. #' @param add_text_color Text color.
#' @param add_text_suffix If percent is FALSE, should we add a suffix to the text label? #' @param add_text_suffix If percent is FALSE, should we add a suffix to the text label?
#' @param theme Whatever theme. Default to theme_reach(). #' @param theme Whatever theme. Default to theme_reach().
@ -30,7 +30,7 @@ donut <- function(df,
arrange = TRUE, arrange = TRUE,
hole_size = 3, hole_size = 3,
add_text = TRUE, add_text = TRUE,
add_text_treshold_display = 5, add_text_color = "white", add_text_suffix = "", theme = theme_reach(legend_reverse = TRUE)){ add_text_threshold_display = 5, add_text_color = "white", add_text_suffix = "", theme = theme_reach(legend_reverse = TRUE)){
# Arrange by biggest prop first ? # Arrange by biggest prop first ?
if (arrange) df <- dplyr::arrange( if (arrange) df <- dplyr::arrange(
@ -60,7 +60,7 @@ donut <- function(df,
# Add text labels # Add text labels
if (add_text) { if (add_text) {
df <- dplyr::mutate(df, y_treshold = ifelse({{ y }} >= add_text_treshold_display, {{ y }}, NA )) df <- dplyr::mutate(df, y_treshold = ifelse({{ y }} >= add_text_threshold_display, {{ y }}, NA ))
g <- g + g <- g +
ggplot2::geom_text( ggplot2::geom_text(