Skip to content

Commit

Permalink
tinytheme
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Nov 15, 2024
1 parent 4d38a7e commit cadf835
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(plt)
export(plt_add)
export(tinyplot)
export(tinyplot_add)
export(tinytheme)
export(tpar)
export(type_abline)
export(type_area)
Expand Down
72 changes: 72 additions & 0 deletions R/tinytheme.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#' Set or Reset Plot Themes for `tinyplot`
#'
#' The `tinytheme` function sets or resets the theme for plots created with
#' `tinyplot`. Themes control the appearance of plots, such as text alignment,
#' font styles, and axis labels. By default, a "simple" theme is available.
#'
#' @param theme A character string specifying the name of the theme to apply.
#' If `NULL`, the current theme is reset to default settings. Available themes include:
#' - `"simple"`
#' @param ... Named arguments to override specific theme settings. These arguments are
#' passed to `tpar()` and take precedence over the predefined settings in the selected
#' theme.
#'
#' @details
#' The function uses `setHook("before.plot.new", ...)` to apply the specified
#' theme settings just before a new plot is drawn. Themes are implemented
#' as a list of graphical parameters, which are passed to the `tpar` function.
#'
#' To reset the theme to default settings (no customization), call `tinytheme(NULL)`.
#'
#' @return The function returns nothing. It is called for its side effects.
#'
#' @examples
#' # Set a theme
#' tinytheme("simple")
#' tinyplot(mpg ~ hp | factor(am), data = mtcars)
#'
#' # Customize the theme by overriding default settings
#' tinytheme("simple", fg = "blue", font.main = 2)
#' tinyplot(mpg ~ hp | factor(am), data = mtcars, main = "Hello World!")
#'
#' # Reset the theme
#' tinytheme()
#' tinyplot(mpg ~ hp | factor(am), data = mtcars)
#'
#' @export
tinytheme = function(theme = NULL, ...) {
if (is.null(theme)) {
setHook("before.plot.new", NULL, action = "replace")
return(invisible(NULL))
}

assert_choice(theme, c("simple"))
settings = switch(theme,
"simple" = theme_simple
)

dots = list(...)
for (n in names(dots)) {
settings[[n]] = dots[[n]]
}

theme_fun = function() {
do.call(tpar, settings)
}

setHook("before.plot.new", theme_fun, action = "replace")
}

theme_simple = list(
fg = "black", # Foreground color
adj = 0.5, # Horizontal alignment of text
bty = "l", # Box type around the plot
family = "serif", # Font family
font = 1, # Font style (plain)
font.axis = 1, # Font style for axis labels
font.lab = 1, # Font style for axis titles
font.main = 1, # Font style for the main title (normal)
font.sub = 3, # Font style for the subtitle (italic)
las = 1, # Orientation of axis labels (1 = horizontal)
tck = 0 # Tick mark length (0 = none)
)
48 changes: 48 additions & 0 deletions man/tinytheme.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cadf835

Please sign in to comment.