Rewrite of theme_reach()

This commit is contained in:
gnoblet 2022-11-10 18:09:40 +01:00
parent f45d9e44ba
commit 045e277cb6

View file

@ -1,35 +1,70 @@
#' @title Base REACH ggplot2 theme #' @title ggplot2 theme with REACH color palettes
#' #'
#' @param family The font family. Default to "Leelawadee" #' @param palette Palette name from 'pal_reach()'.
#' @param discrete Boolean indicating whether color aesthetic is discrete or not
#' @param reverse Boolean indicating whether the palette should be reversed
#' @param family The font family for all plot's texts. Default to "Leelawadee".
#' @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_family. Defaults to 14.
#' @param plot_background_pal The color for the plot background color. Default to white.
#' @param panel_background_pal The color for the panel background color. Default to white.
#' @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 ... Additional arguments passed to `ggblanket::gg_theme()`.
#' #'
#' @description Give some reach colors and fonts to a ggplot. Based on theme_bw() #'
#' @description Give some reach colors and fonts to a ggplot.
#' #'
#' @return The base REACH theme #' @return The base REACH theme
#' #'
#' @export #' @export
theme_reach <- function(family = "Leelawadee") { theme_reach <- function(
palette = "main",
discrete = TRUE,
reverse = FALSE,
family = "Leelawadee",
text_size = 10,
title_size = 14,
plot_background_pal = "#FFFFFF",
panel_background_pal = "#FFFFFF",
void = FALSE,
legend_position = "right",
legend_direction = "vertical",
...
) {
rlang::check_installed("ggplot2", reason = "Package \"ggplot2\" needed for `theme_reach_*()` to work. Please install it.") # Basic simple theme
theme_reach <- ggblanket::gg_theme(
text_family = family,
text_size = text_size,
title_size = title_size,
plot_background_pal = plot_background_pal,
panel_background_pal = panel_background_pal,
void = void
)
ggplot2::theme_bw() +
ggplot2::theme( # Default legend to right position
title = ggplot2::element_text(family = family, theme_reach <- theme_reach +
size = 12, ggplot2::theme(legend.position = legend_position)
colour = "#58585A",
hjust = 0.5, # Defaut legend to vertical direction
vjust = 0.5), theme_reach <- theme_reach +
text = ggplot2::element_text(family = family, ggplot2::theme(legend.direction = legend_direction)
colour = "#58585A"),
axis.title = ggplot2::element_text(size = 11), # Add reach color palettes by default
axis.text = ggplot2::element_text(size = 10), theme_reach <- list(
legend.text = ggplot2::element_text(size = 11), theme_reach,
strip.text = ggplot2::element_text(size = 11), scale_color(palette = palette, discrete = discrete, reverse = reverse),
legend.title = ggplot2::element_text(size = 11) scale_fill(palette = palette, discrete = discrete, reverse = reverse)
) )
return(theme_reach)
} }
#' @title Some REACH theme for ggplot #' @title Some REACH theme for ggplot
#' #'
#' @param family The font family. Default to "Leelawadee" #' @param family The font family. Default to "Leelawadee"
@ -49,7 +84,6 @@ theme_reach_borders <- function(family = "Leelawadee") {
#' @title Some reach more minimal theme for a ggplot histogram #' @title Some reach more minimal theme for a ggplot histogram
#'
#' @param family The font family. Default to "Leelawadee" #' @param family The font family. Default to "Leelawadee"
#' #'
#' @description Give some REACH colors and fonts to a ggplot. Based on theme_bw(). To be used for vertical bar charts. #' @description Give some REACH colors and fonts to a ggplot. Based on theme_bw(). To be used for vertical bar charts.