From 6ff42bac73f8fd7b1c754c7cff14c42ce580314d Mon Sep 17 00:00:00 2001 From: gnoblet Date: Wed, 21 Dec 2022 12:19:47 -0500 Subject: [PATCH] Wraping of title, caption and subtitle pass to theming --- DESCRIPTION | 3 ++- R/bar.R | 11 ++++------- R/point.R | 11 ++++------- R/theme_reach.R | 6 +++++- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3ba3de9..a72e1ef 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -24,6 +24,7 @@ Imports: grDevices, glue, scales, - stringr + stringr, + ggtext Suggests: knitr, sf, tmap VignetteBuilder: knitr diff --git a/R/bar.R b/R/bar.R index 8a9216e..24ddae8 100644 --- a/R/bar.R +++ b/R/bar.R @@ -16,15 +16,12 @@ #' @param caption Plot caption. Default to NULL. #' @param add_text TRUE or FALSE. Add the value as text. #' @param add_text_suffix If percent is FALSE, should we add a suffix to the text label? -#' @param title_wrap Wrap title, number of characters. -#' @param subtitle_wrap Wrap subtitle, number of characters. -#' @param caption_wrap Wrap caption, number of characters. #' @param theme Whatever theme. Default to theme_reach(). #' #' @return A bar chart #' #' @export -bar <- function(df, x, y, group = NULL, flip = TRUE, percent = TRUE, 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 = "", title_wrap = 60, subtitle_wrap = 60, caption_wrap = 120, theme = theme_reach()){ +bar <- function(df, x, y, group = NULL, flip = TRUE, percent = TRUE, 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()){ # To do : # - automate bar width and text size, or at least give the flexibility and still center text @@ -44,9 +41,9 @@ bar <- function(df, x, y, group = NULL, flip = TRUE, percent = TRUE, position = # Add title, subtitle, caption, x_title, y_title g <- g + ggplot2::labs( - title = stringr::str_wrap(title, title_wrap), - subtitle = stringr::str_wrap(subtitle, subtitle_wrap), - caption = stringr::str_wrap(caption, caption_wrap), + title = title, + subtitle = subtitle, + caption = caption, x = x_title, y = y_title, color = group_title, diff --git a/R/point.R b/R/point.R index 16e296f..9dae4bf 100644 --- a/R/point.R +++ b/R/point.R @@ -13,15 +13,12 @@ #' @param title Plot title. Default to NULL. #' @param subtitle Plot subtitle. Default to NULL. #' @param caption Plot caption. Default to NULL. -#' @param title_wrap Wrap title, number of characters. -#' @param subtitle_wrap Wrap subtitle, number of characters. -#' @param caption_wrap Wrap caption, number of characters. #' @param theme Whatever theme. Default to theme_reach(). #' #' @return A bar chart #' #' @export -point <- function(df, x, y, group = NULL, flip = TRUE, alpha = 1, size = 1, x_title = NULL, y_title = NULL, group_title = NULL, title = NULL, subtitle = NULL, caption = NULL, title_wrap = 60, subtitle_wrap = 60, caption_wrap = 120, theme = theme_reach()){ +point <- function(df, x, y, group = NULL, flip = TRUE, alpha = 1, size = 1, x_title = NULL, y_title = NULL, group_title = NULL, title = NULL, subtitle = NULL, caption = NULL, theme = theme_reach()){ # To do : # - automate bar width and text size, or at least give the flexibility and still center text @@ -41,9 +38,9 @@ point <- function(df, x, y, group = NULL, flip = TRUE, alpha = 1, size = 1, x_ti # Add title, subtitle, caption, x_title, y_title g <- g + ggplot2::labs( - title = stringr::str_wrap(title, title_wrap), - subtitle = stringr::str_wrap(subtitle, subtitle_wrap), - caption = stringr::str_wrap(caption, caption_wrap), + title = title, + subtitle = subtitle, + caption = caption, x = x_title, y = y_title, color = group_title, diff --git a/R/theme_reach.R b/R/theme_reach.R index 30784a6..99036e9 100644 --- a/R/theme_reach.R +++ b/R/theme_reach.R @@ -96,7 +96,11 @@ theme_reach <- function( plot.caption.position = "plot", # Text sizes axis.text = ggplot2::element_text(size = axis_text_size), - axis.title = ggplot2::element_text(size = axis_title_size) + axis.title = ggplot2::element_text(size = axis_title_size), + # Wrap title + plot.title = ggtext::element_textbox_simple(), + plot.subtitle = ggtext::element_textbox_simple(), + plot.caption = ggtext::element_textbox_simple() ) # Axis lines ?