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

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")
#> 2023-09-05 20:31:46.131382 loading file /github/home/.cache/R/BiocFileCache/11446d56ea68_Human_DLPFC_Visium_processedData_sce_scran_sce_layer_spatialLIBD.Rdata%3Fdl%3D1

## Example layer colors with the corresponding names
get_colors(libd_layer_colors, sce_layer$layer_guess)
#>        Layer1        Layer2        Layer3        Layer4        Layer5 
#>     "#F0027F"     "#377EB8"     "#4DAF4A"     "#984EA3"     "#FFD700" 
#>        Layer6            WM            NA           WM2 
#>     "#FF7F00"     "#1A1A1A" "transparent"     "#666666" 
get_colors(libd_layer_colors, sce_layer$layer_guess_reordered_short)
#>            L1            L2            L3            L4            L5 
#>     "#F0027F"     "#377EB8"     "#4DAF4A"     "#984EA3"     "#FFD700" 
#>            L6            WM            NA           WM2 
#>     "#FF7F00"     "#1A1A1A" "transparent"     "#666666" 

## Example where colors are assigned automatically
## based on a pre-defined set of colors
get_colors(clusters = sce_layer$kmeans_k7)
#>         1         2         3         4         5         6         7         8 
#> "#b2df8a" "#e41a1c" "#377eb8" "#4daf4a" "#ff7f00"    "gold" "#a65628" "#999999" 
#>         9        10        11        12 
#>   "black"    "grey"   "white"  "purple" 

## Example where Polychrome::palette36.colors() gets used
get_colors(clusters = letters[seq_len(13)])
#> <colors>
#> #5A5156FF #E4E1E3FF #F6222EFF #FE00FAFF #16FF32FF #3283FEFF #FEAF16FF #B00068FF #1CFFCEFF #90AD1CFF #2ED9FFFF #DEA0FDFF #AA0DFEFF