Skip to contents

This function generates a list of colors based on the specified number and palette type. It supports both categorical and continuous color palettes.

Usage

getColors(n, palette = 1, categoric = TRUE, exclude = NULL)

Arguments

n

An integer specifying the number of colors required.

palette

An integer specifying the color palette to use. For categorical palettes, valid values are 1 to 7, and for continuous palettes, valid values are 1 to 14.

categoric

A logical value indicating whether to use categorical palettes (TRUE) or continuous palettes (FALSE). Default is TRUE.

exclude

A character vector of colors to exclude from the generated list. Default is NULL.

Value

A vector of colors.

Details

The function uses different sets of color palettes for categorical and continuous data. For categorical data, the function includes palettes such as kelly, cols25, polychrome, glasbey, alphabet2, and alphabet. For continuous data, the function includes palettes like viridis, parula, magma, coolwarm, warmcool, inferno, plasma, and several kovesi linear palettes.

If the number of requested colors exceeds the available colors in the chosen palette, additional colors are sampled from a combined set of all available colors, ensuring the uniqueness of the generated colors.

The function uses `setdiff` to exclude specified colors and ensures that no colors are repeated by setting a seed for reproducibility.

Examples

# Generate 5 categorical colors using the first palette
getColors(5, palette = 1, categoric = TRUE)
#> [1] "#F3C300" "#875692" "#F38400" "#A1CAF1" "#222222"

# Generate 10 continuous colors using the viridis palette
getColors(10, palette = 1, categoric = FALSE)
#>  [1] "#440154" "#462777" "#3D4988" "#30678D" "#25818E" "#1F9D87" "#36B677"
#>  [8] "#6DCC57" "#B3DC2B" "#FDE725"