Associate colors to values in R

225 Views Asked by At

I needed help knowing how to associate colors with values. For example, I have a vector for colors and values as follows i.e.

cols <- c("red", "gray60")
and 
numEra <- c(values)
numEra
[1] 1 1 1 2 2 2

How do I associate these values with the colors "red" and "gray60" ??

1

There are 1 best solutions below

0
Quinten On

Maybe you want to use cut like this:

cols <- c("red", "gray60")
numEra <- c(1, 1, 1, 2, 2, 2)

cols[cut(numEra, 2)]
#> [1] "red"    "red"    "red"    "gray60" "gray60" "gray60"

Created on 2022-10-01 with reprex v2.0.2

So each number is now associated with a color.