| Title: | A Unified Framework and Interface for Color Palettes and Ramps |
|---|---|
| Description: | The 'colortheme' package provides a unified, environment-based object framework for managing, retrieving, and customizing color palettes and color ramps in R. It acts as a flexible dispatcher that supports both discrete qualitative palettes and continuous sequential/diverging color ramps. Features a compact URL-like syntax for on-the-fly theme modifications (such as color reversal, type coercion, and query-based parameter tuning) and seamless integration with base R graphics and 'ggplot2'. |
| Authors: | Ryoichi Asashiba [aut, cre] (ORCID: <https://orcid.org/0009-0001-9532-7000>) |
| Maintainer: | Ryoichi Asashiba <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-11-27 23:51:06 UTC |
| Source: | https://github.com/cran/colortheme |
as_palette() and as_ramp() extract the underlying discrete palette function or continuous color ramp function from a color theme or color kernel.
as_palette(x, ...) ## S3 method for class 'colortheme' as_palette(x, ...) ## Default S3 method: as_palette(x, ...) as_ramp(x, ...) ## S3 method for class 'colortheme' as_ramp(x, ...) ## Default S3 method: as_ramp(x, ...)as_palette(x, ...) ## S3 method for class 'colortheme' as_palette(x, ...) ## Default S3 method: as_palette(x, ...) as_ramp(x, ...) ## S3 method for class 'colortheme' as_ramp(x, ...) ## Default S3 method: as_ramp(x, ...)
x |
a |
... |
additional arguments passed to |
as_palette() returns a palette function taking an integer n and returning n colors.
as_ramp() returns a ramp function taking a numeric vector x in [0, 1] and returning corresponding colors.
pal <- as_palette("Okabe-Ito") pal(8L) ramp <- as_ramp("Viridis") ramp(c(0, 0.5, 1.0))pal <- as_palette("Okabe-Ito") pal(8L) ramp <- as_ramp("Viridis") ramp(c(0, 0.5, 1.0))
The colortheme() function is the main interface for working with "colortheme" objects.
It acts as a generic dispatcher that can retrieve a pre-defined theme by name, create a new theme from a vector of colors or a color-generating function, and modify an existing "colortheme" object.
colortheme(object, ...) ## S3 method for class 'colortheme' colortheme( object, kernel.args = NULL, options = NULL, name = NULL, source = NULL, type = NULL, reverse = FALSE, ... ) ## S3 method for class 'character' colortheme( object, kernel.args = list(), options = list(), name = NULL, source = NULL, type = NULL, reverse = FALSE, env = NULL, ... ) ## Default S3 method: colortheme( object, kernel.args = list(), options = list(), name = NULL, source = NULL, type = NULL, reverse = FALSE, ... ) ## S3 method for class 'colortheme' plot(x, n = NULL, text = x$name, ...) ## S3 method for class 'colortheme' print(x, display = TRUE, ...)colortheme(object, ...) ## S3 method for class 'colortheme' colortheme( object, kernel.args = NULL, options = NULL, name = NULL, source = NULL, type = NULL, reverse = FALSE, ... ) ## S3 method for class 'character' colortheme( object, kernel.args = list(), options = list(), name = NULL, source = NULL, type = NULL, reverse = FALSE, env = NULL, ... ) ## Default S3 method: colortheme( object, kernel.args = list(), options = list(), name = NULL, source = NULL, type = NULL, reverse = FALSE, ... ) ## S3 method for class 'colortheme' plot(x, n = NULL, text = x$name, ...) ## S3 method for class 'colortheme' print(x, display = TRUE, ...)
object |
a character string to retrieve a pre-defined theme, a color kernel (vector of colors or color-generating function) to create a new theme, or a |
... |
optional named arguments used to modify the color theme (overriding corresponding entries in |
kernel.args |
a list of arguments passed to the color kernel. |
options |
a list of option values controlling the color theme's behavior. |
name |
a character string for the color theme name. |
source |
a character string for the source name of the color theme. |
type |
a character string specifying the type of the color theme: |
reverse |
logical. If |
env |
an environment where color themes are registered. |
x |
a |
n |
optional integer specifying the number of colors to display in |
text |
optional character string for the title displayed in |
display |
logical. If |
A "colortheme" object is a specialized environment that provides two primary color-generating functions: $palette() and $ramp().
$palette(n) takes an integer n and returns a character vector of n discrete colors. It is primarily intended for qualitative themes, where distinct colors represent categorical data.
$ramp(x) takes a numeric vector x with values in the [0, 1] interval and returns a character vector of corresponding colors. It maps numeric values onto a continuous color gradient, making it suitable for sequential and diverging themes.
colortheme() dispatches based on the class of object:
If object is a character string (e.g., "Viridis", "grDevices/RdBu_r@q?alpha=.5"), it is parsed according to the theme name syntax and loaded from the registry.
If object is a color kernel (a character vector of colors, a palette function, or a ramp function), a new color theme is constructed.
If object is already a "colortheme" object, a modified copy is returned with updated settings.
colortheme() returns a "colortheme" object (an environment with class c("colortheme", "environment")) containing $palette() and $ramp() functions along with theme metadata.
When retrieving a registered theme by string, you can use a compact URL-like syntax to specify the source and apply on-the-fly modifications:
"[(source)/](name)[_r][@(type)][?(query)]"
source: (optional) the source package or collection (e.g., "grDevices").
name: the name of the theme (e.g., "RdBu").
"_r": (optional) suffix to reverse the color order.
type: (optional) desired theme type, matched against "sequential", "diverging", or "qualitative" (abbreviations like "s", "d", "q" or "seq", "div", "qual" are supported).
query: (optional) key-value pairs separated by ; or & (e.g., "?alpha=0.5;na.color='gray50'") to override theme metadata, options, or kernel.args.
colorthemes, register_colortheme, as_palette, map_to_colors, scale_color_theme
# Retrieve a pre-defined theme ct <- colortheme("Mako") print(ct) ct$palette(5L) ct$ramp(seq.int(0, 1, length.out = 5)) # Use special syntax to get a reversed, qualitative theme with alpha ct <- colortheme("grDevices/Zissou 1_r@qual?alpha=0.75") ct$palette(5L) # Create a new theme from a vector of colors ct <- colortheme(c("#003f5c", "#7a5195", "#ef5675", "#ffa600")) ct$palette(5L) # Modify an existing theme ct2 <- colortheme(ct, type = "qualitative", reverse = TRUE) ct2$palette(5L)# Retrieve a pre-defined theme ct <- colortheme("Mako") print(ct) ct$palette(5L) ct$ramp(seq.int(0, 1, length.out = 5)) # Use special syntax to get a reversed, qualitative theme with alpha ct <- colortheme("grDevices/Zissou 1_r@qual?alpha=0.75") ct$palette(5L) # Create a new theme from a vector of colors ct <- colortheme(c("#003f5c", "#7a5195", "#ef5675", "#ffa600")) ct$palette(5L) # Modify an existing theme ct2 <- colortheme(ct, type = "qualitative", reverse = TRUE) ct2$palette(5L)
colorthemes() returns a data frame listing available color themes in the registry.
register_colortheme() registers a custom color theme into the registry.
colorthemes(name = NULL, source = NULL, type = NULL, env = NULL) register_colortheme( kernel, kernel.args = list(), options = list(), name = "newtheme", source = "custom", type = NULL, env = NULL )colorthemes(name = NULL, source = NULL, type = NULL, env = NULL) register_colortheme( kernel, kernel.args = list(), options = list(), name = "newtheme", source = "custom", type = NULL, env = NULL )
name |
for |
source |
for |
type |
a character string or vector specifying the theme type(s): |
env |
an environment where color themes are registered. Defaults to the package registry ( |
kernel |
a color vector, a palette function, a ramp function, a |
kernel.args |
a list of arguments passed to the color kernel. |
options |
a list of option values controlling the color theme's behavior. |
colorthemes() extracts metadata from the registry environment and structures it into a data frame, allowing users to discover themes by name, source, and type (with case-insensitive prefix matching).
register_colortheme() takes a color vector, a color-generating function, or an existing "colortheme" object and registers it under a specified name and source (defaulting to "custom/newtheme").
To keep the registry lightweight, the kernel argument supports lazy loading: pass a character vector or list of length 2 where the first element is an R expression returning a kernel (e.g., "rainbow") and the second is the namespace (e.g., "grDevices").
colorthemes(): A data frame with columns "name", "source", and "type".
register_colortheme(): Invisibly returns the metadata of the previous theme that was overwritten (or NULL if none existed).
# List all available themes head(colorthemes()) # Case-insensitive prefix matching colorthemes(name = "ma") # Register a custom theme register_colortheme( kernel = c("#264653", "#2a9d8f", "#e9c46a", "#f4a261", "#e76f51"), name = "sand", source = "custom", type = "sequential" ) colorthemes(name = "sand") # Remove the custom theme by passing NULL register_colortheme(NULL, name = "sand", source = "custom")# List all available themes head(colorthemes()) # Case-insensitive prefix matching colorthemes(name = "ma") # Register a custom theme register_colortheme( kernel = c("#264653", "#2a9d8f", "#e9c46a", "#f4a261", "#e76f51"), name = "sand", source = "custom", type = "sequential" ) colorthemes(name = "sand") # Remove the custom theme by passing NULL register_colortheme(NULL, name = "sand", source = "custom")
map_to_colors() maps a vector of data values to a vector of hexadecimal color codes based on a specified color theme.
It automatically handles both discrete and continuous variables, appropriately scaling values and handling missing data.
map_to_colors(x, theme = NULL, discrete = NULL, middle = 0, na.value = NULL)map_to_colors(x, theme = NULL, discrete = NULL, middle = 0, na.value = NULL)
x |
a vector of data values (discrete or continuous) to be mapped to colors. |
theme |
a color theme name (e.g., |
discrete |
logical. If |
middle |
a numeric value specifying the midpoint for diverging color themes. Default is |
na.value |
a character string specifying the color for |
A character vector of hexadecimal color codes.
# Continuous mapping (Sequential) plot(cars, col = map_to_colors(cars$speed, "Viridis"), pch = 19) # Discrete mapping (Qualitative) plot(iris$Sepal.Length, iris$Sepal.Width, col = map_to_colors(iris$Species, "Set 2"), pch = 19)# Continuous mapping (Sequential) plot(cars, col = map_to_colors(cars$speed, "Viridis"), pch = 19) # Discrete mapping (Qualitative) plot(iris$Sepal.Length, iris$Sepal.Width, col = map_to_colors(iris$Species, "Set 2"), pch = 19)
scale_color_theme() and its family of functions provide a unified interface to apply color themes to the colour and fill aesthetics of ggplot2 objects.
scale_color_theme( theme = NULL, ..., discrete = NULL, middle = 0, aesthetics = "colour" ) scale_colour_theme( theme = NULL, ..., discrete = NULL, middle = 0, aesthetics = "colour" ) scale_fill_theme( theme = NULL, ..., discrete = NULL, middle = 0, aesthetics = "fill" )scale_color_theme( theme = NULL, ..., discrete = NULL, middle = 0, aesthetics = "colour" ) scale_colour_theme( theme = NULL, ..., discrete = NULL, middle = 0, aesthetics = "colour" ) scale_fill_theme( theme = NULL, ..., discrete = NULL, middle = 0, aesthetics = "fill" )
theme |
a color theme name (e.g., |
... |
optional arguments passed to |
discrete |
logical. If |
middle |
a numeric value specifying the midpoint for diverging color themes. Default is |
aesthetics |
the aesthetic(s) to be scaled ( |
This function automatically selects the appropriate ggplot2 scale based on the theme's type:
If the theme is "qualitative", a discrete scale is used by default (discrete = TRUE).
If the theme is "sequential" or "diverging", a continuous scale is used by default. Diverging themes use scales::rescale_mid() to center the gradient around middle.
A ggplot2 scale object ("ScaleContinuous" or "ScaleDiscrete").
d <- ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length, Sepal.Width, colour = Species)) + ggplot2::geom_point() d + scale_color_theme("Set 1") d + scale_color_theme("SunsetDark", discrete = TRUE)d <- ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length, Sepal.Width, colour = Species)) + ggplot2::geom_point() d + scale_color_theme("Set 1") d + scale_color_theme("SunsetDark", discrete = TRUE)