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