r - Using lapply with colorRampPalette -
i have following vector:
x <- c(1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 5, 5)
what want assign color each number using colorramppalette
. why command gives more output expected:
library(rcolorbrewer) mycolorfunction <- colorramppalette(brewer.pal(9,"set1")) unlist(lapply(as.list(x),mycolorfunction))
it gives this:
[1] "#e41a1c" "#e41a1c" "#e41a1c" "#e41a1c" "#999999" "#e41a1c" "#999999" "#e41a1c" "#999999" "#e41a1c" "#999999" [12] "#e41a1c" "#999999" "#e41a1c" "#999999" "#e41a1c" "#999999" "#e41a1c" "#ff7f00" "#999999" "#e41a1c" "#ff7f00" [23] "#999999" "#e41a1c" "#ff7f00" "#999999" "#e41a1c" "#7e6e85" "#e1c62f" "#999999" "#e41a1c" "#4daf4a" "#ff7f00" [34] "#a65628" "#999999" "#e41a1c" "#4daf4a" "#ff7f00" "#a65628" "#999999" "#e41a1c" "#4daf4a" "#ff7f00" "#a65628" [45] "#999999" "#e41a1c" "#4daf4a" "#ff7f00" "#a65628" "#999999" "#e41a1c" "#4daf4a" "#ff7f00" "#a65628" "#999999" [56] "#e41a1c" "#4daf4a" "#ff7f00" "#a65628" "#999999"
i expect produce vector of size 20 5 color hex. what's right way it?
at end of day i'd use final vector rowsidecolors
argument in heatmap.2
.
directly use function mycolorfunction
, x
vector of indices:
x <- c(1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 5, 5) library(rcolorbrewer) mycolorfunction <- colorramppalette(brewer.pal(9,"set1")) mycolorfunction(max(x))[x] # [1] "#e41a1c" "#e41a1c" "#e41a1c" "#4daf4a" "#4daf4a" "#4daf4a" # [7] "#4daf4a" "#4daf4a" "#4daf4a" "#4daf4a" "#ff7f00" "#ff7f00" # [13] "#ff7f00" "#a65628" "#999999" "#999999" "#999999" "#999999" # [19] "#999999" "#999999"