| Title: | Image-Inspired Color Palettes for Biomedical Visualization |
|---|---|
| Description: | Provides a curated collection of image-inspired color palettes for biomedical visualization. The palettes are organized as qualitative, sequential, or diverging scales and include documented source context and intended use. The package provides functions to retrieve, inspect, preview, and apply these palettes in base R and 'ggplot2' graphics, together with utilities for working with palette definitions. |
| Authors: | Yibin Zhou [aut, cre] (ORCID: <https://orcid.org/0009-0009-4600-8175>) |
| Maintainer: | Yibin Zhou <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.2 |
| Built: | 2026-10-11 15:20:25 UTC |
| Source: | https://github.com/evanbio/biopalette |
The biopalette package provides image-inspired color palettes for biomedical visualization. Each palette is sourced from a real image — a film still, a scientific figure, or an artwork — and translated into a reproducible color system.
Main features:
Retrieve palettes by name, type, and size
Preview and browse palettes interactively
Apply palettes as discrete or continuous ggplot2 scales
Create and manage custom palettes
Convert between HEX and RGB color formats
Maintainer: Yibin Zhou [email protected] (ORCID)
Useful links:
Report bugs at https://github.com/evanbio/biopalette/issues
Save a named color palette as a JSON file in a collection directory. The
palette is usable immediately: point any reading function at the same
palettes_dir. The JSON is written to a same-directory temporary file,
validated, and then committed; a failed overwrite leaves the previous
palette intact.
create_palette( name, type = c("sequential", "diverging", "qualitative"), colors, palettes_dir, overwrite = FALSE )create_palette( name, type = c("sequential", "diverging", "qualitative"), colors, palettes_dir, overwrite = FALSE )
name |
Character. Palette name (e.g., "blues"). |
type |
Character. One of "sequential", "diverging", or "qualitative". |
colors |
Character vector of HEX color values (e.g., "#E64B35" or "#E64B35B2"). |
palettes_dir |
Character. Directory to write the palette into. Required: there is deliberately no default, so a palette can never be written into the collection that ships with the package. |
overwrite |
Logical. If TRUE, overwrite existing palette file. Default: FALSE. |
Invisibly returns a list with path and info.
temp_dir <- file.path(tempdir(), "palettes") create_palette("blues", "sequential", c("#deebf7", "#9ecae1", "#3182bd"), palettes_dir = temp_dir) create_palette("qual_vivid", "qualitative", c("#E64B35", "#4DBBD5", "#00A087"), palettes_dir = temp_dir) # Overwrite an existing palette explicitly create_palette("blues", "sequential", c("#c6dbef", "#6baed6", "#2171b5"), palettes_dir = temp_dir, overwrite = TRUE) unlink(temp_dir, recursive = TRUE)temp_dir <- file.path(tempdir(), "palettes") create_palette("blues", "sequential", c("#deebf7", "#9ecae1", "#3182bd"), palettes_dir = temp_dir) create_palette("qual_vivid", "qualitative", c("#E64B35", "#4DBBD5", "#00A087"), palettes_dir = temp_dir) # Overwrite an existing palette explicitly create_palette("blues", "sequential", c("#c6dbef", "#6baed6", "#2171b5"), palettes_dir = temp_dir, overwrite = TRUE) unlink(temp_dir, recursive = TRUE)
Retrieve a named palette by name and type, returning a vector of HEX colors. Automatically checks for type mismatch and provides smart suggestions.
get_palette(name, type = NULL, n = NULL, reverse = FALSE, palettes_dir = NULL)get_palette(name, type = NULL, n = NULL, reverse = FALSE, palettes_dir = NULL)
name |
Character. Name of the palette (e.g. "qual_vivid"). |
type |
Character. One of "sequential", "diverging", "qualitative". If NULL, type is auto-detected. |
n |
Integer. Number of colors to return. If NULL, returns all colors.
See What |
reverse |
Logical. Reverse the palette before |
palettes_dir |
Character. Directory holding a palette collection
( |
Character vector of HEX color codes.
n meansn is resolved according to what the palette's type says the colors are,
because "give me 3 colors" means two different things:
qualitativeThe colors are unordered categories, so n takes the
first n of them. Asking for more than the palette holds is an error —
there is no way to invent a category that the palette does not contain.
sequential, diverging
The colors are stops along a ramp, so n
returns n steps spanning the whole ramp, interpolating as needed.
Interpolation takes place in Lab colour space, matching the package's
ggplot2 gradient scales.
Any n works, above or below the number of stops. Taking the first n
stops instead would silently hand back one end of the ramp — the light
half of a sequential scale, or one arm of a diverging one.
n equal to the number of stops returns the palette untouched. When
reverse = TRUE the palette is flipped first, so n selects from the
reversed palette.
get_palette("gene_red", type = "qualitative") # Qualitative: the first n categories get_palette("walter_white2", type = "qualitative", n = 2) # Sequential: n steps across the whole ramp, not the first n stops get_palette("mitonuclear_blue") get_palette("mitonuclear_blue", n = 3) # Ramps can also be stretched beyond the stops they were drawn from get_palette("walter_white", type = "diverging", n = 9) # Flip a ramp end to end get_palette("mitonuclear_blue", reverse = TRUE)get_palette("gene_red", type = "qualitative") # Qualitative: the first n categories get_palette("walter_white2", type = "qualitative", n = 2) # Sequential: n steps across the whole ramp, not the first n stops get_palette("mitonuclear_blue") get_palette("mitonuclear_blue", n = 3) # Ramps can also be stretched beyond the stops they were drawn from get_palette("walter_white", type = "diverging", n = 9) # Flip a ramp end to end get_palette("mitonuclear_blue", reverse = TRUE)
Convert a character vector of HEX color codes to a data.frame with columns
hex, r, g, b, and alpha.
hex2rgb(hex)hex2rgb(hex)
hex |
Character vector of HEX color codes (e.g. |
A data.frame with hex (character) and integer r, g, b, and
alpha columns. Channels lie in [0, 255]. alpha is NA_integer_ for
a 6-digit input and the explicit alpha channel for an 8-digit input.
hex2rgb("#FF8000") hex2rgb(c("#FF8000", "#00FF00"))hex2rgb("#FF8000") hex2rgb(c("#FF8000", "#00FF00"))
Return a data.frame of all available palette metadata, optionally filtered by type.
list_palettes(type = NULL, sort = TRUE, palettes_dir = NULL)list_palettes(type = NULL, sort = TRUE, palettes_dir = NULL)
type |
Palette type(s) to filter: |
sort |
Whether to sort by type, n_color, name. Default: TRUE. |
palettes_dir |
Character. Directory holding a palette collection
( |
A data.frame with columns: name, type, n_color, colors.
list_palettes() list_palettes(type = "qualitative") list_palettes(type = c("sequential", "diverging"))list_palettes() list_palettes(type = "qualitative") list_palettes(type = c("sequential", "diverging"))
Display palettes in a paged gallery format, returning a named list of ggplot objects.
palette_gallery( type = NULL, max_palettes = 30, max_row = 12, verbose = TRUE, palettes_dir = NULL )palette_gallery( type = NULL, max_palettes = 30, max_row = 12, verbose = TRUE, palettes_dir = NULL )
type |
Palette types to include: "sequential", "diverging", "qualitative". Default NULL returns all. |
max_palettes |
Number of palettes per page. Default: 30. |
max_row |
Max colors per row. Default: 12. |
verbose |
Whether to print progress info. Default: TRUE. |
palettes_dir |
Character. Directory holding a palette collection
( |
A named list of ggplot objects (one per page).
palette_gallery() palette_gallery(type = "qualitative") palette_gallery(type = c("sequential", "diverging"), max_palettes = 10)palette_gallery() palette_gallery(type = "qualitative") palette_gallery(type = c("sequential", "diverging"), max_palettes = 10)
Return the runtime metadata for a single named palette. This is the
one-palette counterpart to list_palettes() and uses the same lookup rules
as get_palette().
palette_info(name, type = NULL, palettes_dir = NULL)palette_info(name, type = NULL, palettes_dir = NULL)
name |
Character. Palette name. |
type |
Character. One of |
palettes_dir |
Character. Directory holding a palette collection. If
|
A one-row data.frame with columns name, type, n_color, and
colors. The colors column is a list-column containing the complete HEX
color vector.
palette_info("walter_white") palette_info("babel", type = "qualitative")palette_info("walter_white") palette_info("babel", type = "qualitative")
Visualize a palette using various plot styles.
preview_palette( name, type = NULL, n = NULL, reverse = FALSE, plot_type = c("bar", "pie", "point", "rect", "circle"), title = NULL, palettes_dir = NULL )preview_palette( name, type = NULL, n = NULL, reverse = FALSE, plot_type = c("bar", "pie", "point", "rect", "circle"), title = NULL, palettes_dir = NULL )
name |
Character. Name of the palette. |
type |
Character. One of "sequential", "diverging", "qualitative". If NULL, auto-detected. |
n |
Integer. Number of colors to use. If NULL, uses all. Default: NULL. |
reverse |
Logical. Reverse the palette before |
plot_type |
Character. One of "bar", "pie", "point", "rect", "circle". Default: "bar". |
title |
Character. Plot title. If NULL, defaults to palette name. |
palettes_dir |
Character. Directory holding a palette collection
( |
"bar", "pie", "point" and "circle" are drawn with base graphics;
"rect" is drawn with ggplot2. Either way the plot goes straight to the
active device and nothing is returned — use palette_gallery() when you
want plot objects you can modify, arrange or save.
NULL, invisibly. Called for its plotting side effect — the return
shape is the same for every plot_type.
palette_gallery(), which returns ggplot objects instead of drawing.
preview_palette("gene_red", plot_type = "bar") preview_palette("walter_white", plot_type = "pie") preview_palette("walter_white2", n = 2, plot_type = "circle")preview_palette("gene_red", plot_type = "bar") preview_palette("walter_white", plot_type = "pie") preview_palette("walter_white2", n = 2, plot_type = "circle")
Remove a palette JSON file by name, searching across types if needed.
remove_palette(name, type = NULL, palettes_dir)remove_palette(name, type = NULL, palettes_dir)
name |
Character. Palette name (without '.json' suffix). |
type |
Character. One of "sequential", "diverging", "qualitative". If NULL, searches all types. |
palettes_dir |
Character. Directory holding the palette collection. Required: there is deliberately no default, so the collection that ships with the package can never be removed from. |
Invisibly TRUE if removed successfully, FALSE otherwise.
temp_dir <- tempfile("biopalette-palettes-") create_palette( "example_palette", "qualitative", c("#E64B35", "#4DBBD5", "#00A087"), palettes_dir = temp_dir ) remove_palette("example_palette", palettes_dir = temp_dir) unlink(temp_dir, recursive = TRUE)temp_dir <- tempfile("biopalette-palettes-") create_palette( "example_palette", "qualitative", c("#E64B35", "#4DBBD5", "#00A087"), palettes_dir = temp_dir ) remove_palette("example_palette", palettes_dir = temp_dir) unlink(temp_dir, recursive = TRUE)
Convert RGB or RGBA values to HEX color codes. Accepts either a numeric
vector or a data.frame, symmetrically with hex2rgb().
rgb2hex(rgb)rgb2hex(rgb)
rgb |
A numeric vector of length 3 ( |
A character vector of uppercase 6- or 8-digit HEX color codes.
rgb2hex(c(255, 128, 0)) rgb2hex(c(255, 128, 0, 178)) rgb2hex(hex2rgb(c("#FF8000", "#00FF0080")))rgb2hex(c(255, 128, 0)) rgb2hex(c(255, 128, 0, 178)) rgb2hex(hex2rgb(c("#FF8000", "#00FF0080")))
Use a named biopalette palette for a discrete colour or fill mapping. The
scale asks get_palette() for exactly as many colours as the trained data
has levels. Qualitative palettes therefore use their first n colours,
while sequential and diverging palettes sample n colours across their
complete ramps.
scale_color_biopalette( palette, ..., type = NULL, reverse = FALSE, palettes_dir = NULL ) scale_colour_biopalette( palette, ..., type = NULL, reverse = FALSE, palettes_dir = NULL ) scale_fill_biopalette( palette, ..., type = NULL, reverse = FALSE, palettes_dir = NULL )scale_color_biopalette( palette, ..., type = NULL, reverse = FALSE, palettes_dir = NULL ) scale_colour_biopalette( palette, ..., type = NULL, reverse = FALSE, palettes_dir = NULL ) scale_fill_biopalette( palette, ..., type = NULL, reverse = FALSE, palettes_dir = NULL )
palette |
Character. Name of the palette. |
... |
Passed to |
type |
Character. One of |
reverse |
Logical. Reverse the palette. Default: |
palettes_dir |
Character. Directory holding a palette collection. If
|
All three palette types are supported. Qualitative palettes suit unordered categories; sequential and diverging palettes can be useful for ordered categories.
A ggplot2 discrete scale.
library(ggplot2) ggplot(iris, aes(Sepal.Length, Sepal.Width, colour = Species)) + geom_point() + scale_color_biopalette("three_body") ggplot(iris, aes(Species, Sepal.Length, fill = Species)) + geom_boxplot() + scale_fill_biopalette("three_body", guide = "none")library(ggplot2) ggplot(iris, aes(Sepal.Length, Sepal.Width, colour = Species)) + geom_point() + scale_color_biopalette("three_body") ggplot(iris, aes(Species, Sepal.Length, fill = Species)) + geom_boxplot() + scale_fill_biopalette("three_body", guide = "none")
Build a continuous colour or fill gradient from a sequential or diverging
biopalette palette. The complete palette supplies the gradient stops to
ggplot2::scale_colour_gradientn() or ggplot2::scale_fill_gradientn().
Gradients are interpolated in Lab colour space, the same space used when
get_palette() resamples sequential and diverging palettes.
scale_color_biopalette_gradient( palette, ..., type = NULL, reverse = FALSE, values = NULL, midpoint = NULL, transform = "identity", palettes_dir = NULL ) scale_colour_biopalette_gradient( palette, ..., type = NULL, reverse = FALSE, values = NULL, midpoint = NULL, transform = "identity", palettes_dir = NULL ) scale_fill_biopalette_gradient( palette, ..., type = NULL, reverse = FALSE, values = NULL, midpoint = NULL, transform = "identity", palettes_dir = NULL )scale_color_biopalette_gradient( palette, ..., type = NULL, reverse = FALSE, values = NULL, midpoint = NULL, transform = "identity", palettes_dir = NULL ) scale_colour_biopalette_gradient( palette, ..., type = NULL, reverse = FALSE, values = NULL, midpoint = NULL, transform = "identity", palettes_dir = NULL ) scale_fill_biopalette_gradient( palette, ..., type = NULL, reverse = FALSE, values = NULL, midpoint = NULL, transform = "identity", palettes_dir = NULL )
palette |
Character. Name of the palette. |
... |
Passed to |
type |
Character. |
reverse |
Logical. Reverse the palette. When |
values |
Optional numeric positions for the palette colours, as accepted
by |
midpoint |
Optional finite numeric value to place at the visual centre
of a diverging palette. Values are rescaled with equal units on either side
of this point, so the shorter side of an asymmetric data range does not
reach the palette's extreme colour. |
transform |
A transformation specification accepted by
|
palettes_dir |
Character. Directory holding a palette collection. If
|
Qualitative palettes are rejected because interpolating unordered category
colours does not produce a meaningful continuous scale. Use
scale_color_biopalette() or scale_fill_biopalette() for those palettes.
A ggplot2 continuous scale.
library(ggplot2) ggplot(mtcars, aes(wt, mpg, colour = hp)) + geom_point(size = 3) + scale_color_biopalette_gradient("mitonuclear_blue") ggplot(mtcars, aes(factor(cyl), factor(gear), fill = mpg - mean(mpg))) + geom_tile() + scale_fill_biopalette_gradient("walter_white", midpoint = 0)library(ggplot2) ggplot(mtcars, aes(wt, mpg, colour = hp)) + geom_point(size = 3) + scale_color_biopalette_gradient("mitonuclear_blue") ggplot(mtcars, aes(factor(cyl), factor(gear), fill = mpg - mean(mpg))) + geom_tile() + scale_fill_biopalette_gradient("walter_white", midpoint = 0)