get_colors: Obtain the colors for a set of cluster names

View source: R/get_colors.R

get_colorsR Documentation

Obtain the colors for a set of cluster names

Description

This function returns a vector of colors based on a vector of cluster names. It can be used to automatically assign colors.

Usage

get_colors(colors = NULL, clusters)

Arguments

colors

A vector of colors. If NULL then a set of default colors will be used when clusters has less than 12 unique values, otherwise Polychrome::palette36 will be used which can generate up to 36 unique colors. If the number of unique clusters is beyond 36 then this function will fail.

clusters

A vector of cluster names.

Value

A named vector where the values are the colors to use for displaying them different clusters. For some use cases, you might have to either change the names or use unname().

Examples


## Obtain the necessary data
if (!exists("sce_layer")) sce_layer <- fetch_data("sce_layer")

## Example layer colors with the corresponding names
get_colors(libd_layer_colors, sce_layer$layer_guess)
get_colors(libd_layer_colors, sce_layer$layer_guess_reordered_short)

## Example where colors are assigned automatically
## based on a pre-defined set of colors
get_colors(clusters = sce_layer$kmeans_k7)

## Example where Polychrome::palette36.colors() gets used
get_colors(clusters = letters[seq_len(13)])

## What happens if you have a logical variable with NAs?
set.seed(20240712)
log_var <- sample(c(TRUE, FALSE, NA),
    1000,
    replace = TRUE,
    prob = c(0.3, 0.15, 0.55)
)
log_var_sorted <- sort_clusters(log_var)
## A color does get assigned to 'NA', but will be overwritten by
## 'na_color' passed to `vis_clus_p()` and related functions.
get_colors(colors = NULL, clusters = log_var_sorted)

LieberInstitute/spatialLIBD documentation built on Dec. 19, 2024, 7:12 p.m.