diff --git a/NAMESPACE b/NAMESPACE index 2b4e328e..1ef44c36 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export(plt) export(plt_add) export(tinyplot) export(tinyplot_add) +export(tinytheme) export(tpar) export(type_abline) export(type_area) diff --git a/R/tinytheme.R b/R/tinytheme.R new file mode 100644 index 00000000..86890bb6 --- /dev/null +++ b/R/tinytheme.R @@ -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) +) diff --git a/man/tinytheme.Rd b/man/tinytheme.Rd new file mode 100644 index 00000000..557dfe2c --- /dev/null +++ b/man/tinytheme.Rd @@ -0,0 +1,48 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tinytheme.R +\name{tinytheme} +\alias{tinytheme} +\title{Set or Reset Plot Themes for \code{tinyplot}} +\usage{ +tinytheme(theme = NULL, ...) +} +\arguments{ +\item{theme}{A character string specifying the name of the theme to apply. +If \code{NULL}, the current theme is reset to default settings. Available themes include: +\itemize{ +\item \code{"simple"} +}} + +\item{...}{Named arguments to override specific theme settings. These arguments are +passed to \code{tpar()} and take precedence over the predefined settings in the selected +theme.} +} +\value{ +The function returns nothing. It is called for its side effects. +} +\description{ +The \code{tinytheme} function sets or resets the theme for plots created with +\code{tinyplot}. Themes control the appearance of plots, such as text alignment, +font styles, and axis labels. By default, a "simple" theme is available. +} +\details{ +The function uses \code{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 \code{tpar} function. + +To reset the theme to default settings (no customization), call \code{tinytheme(NULL)}. +} +\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) + +}