
Generate Viridis Color Palette for Ranked Parameters
Source:R/plotting-utils.R
generateViridisColorScale.RdCreates a named color mapping for a set of numeric parameters (e.g., evolutionary rates) using the viridis color palette. Parameters are sorted in ascending order and assigned evenly spaced colors by rank. Numeric magnitude and distance between parameter values are not encoded in the colors.
Value
A named list with two elements:
NamedColorsA named character vector of hex color codes, with names corresponding to the input parameter names, ordered by increasing parameter value and colored at evenly spaced positions by rank.
ParamColorMappingA named numeric vector of the sorted parameter values, maintaining the same order and names as
NamedColors.
Details
This function is useful for plotting results where parameters should be visually distinguished by their ordering (e.g., rate shifts across a phylogeny). By using the perceptually uniform viridis palette, it avoids misleading color interpretations common with rainbow scales. Colors encode sorted rank only; they do not represent the magnitude of a parameter or the distance between parameter values.
See also
viridis::viridis() for details on
the color palette.
Examples
if (requireNamespace("viridis", quietly = TRUE)) {
library(viridis)
set.seed(1)
rates <- c(A = 0.1, B = 0.5, C = 0.9)
color_scale <- generateViridisColorScale(rates)
# View the color assignments
color_scale$NamedColors
# Plot with colors
barplot(color_scale$ParamColorMapping,
col = color_scale$NamedColors,
main = "Rates with Viridis Colors")
}
#> Loading required package: viridisLite