-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d38a7e
commit cadf835
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.