Add (or overwrite, if already exists) a column .color with color names that correspond to levels of factor variable by. Function hyRm_palette removes this palette.

hyRm_palette(sp = NULL)

hyAdd_palette(
  sp = NULL,
  by,
  palette = c(RColorBrewer::brewer.pal(9, "Set1")),
  label = NULL,
  ...
)

hyAdd_color(
  sp = NULL,
  by,
  palette = c(RColorBrewer::brewer.pal(9, "Set1")),
  label = NULL,
  ...
)

Arguments

sp

hyperSpec object.

by

A factor variale which levels will correspond to colors in .color.

palette

A color palette (vector with colors for each level in by). If this argument is not provided, default palette is used.

label

Label for column .color. If label = NULL, default label is added.

Value

HyperSpec object with added/replaced column .color. Lables of variable .color indicate unique colors used (illustration in section "Examples").

Author

Vilmantas Gegzna

Examples


data(Spectra2)
Spectra1 <- hyAdd_color(Spectra2, "class")

colnames(Spectra2)
#> [1] "gr"    "class" "spc"  
   #> [1] "gr"    "class" "spc"

colnames(Spectra1)
#> [1] "gr"     "class"  "spc"    ".color"
   #> [1] "gr"     "class"  "spc"    ".color"

# Default labels
labels(Spectra1,".color")
#> [1] "Colors for \"class\""
   #>  [1] "Colors for \"class\""

hyGet_palette(Spectra1)
#> [1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3"
   #>  "#377EB8" "#4DAF4A" "#984EA3" "#FF7F00"

# ATTENTION -------------------------------------------------

# Preserve labels:
Spectra1[1,".color"] <- "red"
hyGet_palette(Spectra1)
#> Warning: Vector `.color` contains more unique colors than listed in the palette. 
#> Newly added colors are: red.
#> [1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3"

labels(Spectra1,".color")
#> [1] "Colors for \"class\""

# Overwrites labels:
Spectra1$.color[1] <- "red"

hyGet_palette(Spectra1)
#> Warning: Vector `.color` contains more unique colors than listed in the palette. 
#> Newly added colors are: red.
#> [1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3"

labels(Spectra1,".color")
#> [1] ".color"
    #>  ".color"