block by timelyportfolio 4797245a917b32504844

some examples of colors in R htmlwidget leaflet

library(leaflet)

# leaflet color reference
#  https://rstudio.github.io/leaflet/colors.html

# ?colorFactor

# in this case RdYlBu auto assigned to A through E
previewColors(colorFactor("RdYlBu", domain = NULL), LETTERS[1:5])
# let's reverse it by providing levels and specify an order
#   and use tagList so we can compare
library(htmltools)
browsable(
  tagList(
    previewColors(colorFactor("RdYlBu", domain = NULL), LETTERS[1:5]),
    previewColors(colorFactor("RdYlBu", levels = rev(LETTERS[1:5])), LETTERS[1:5]),
    previewColors(colorFactor("RdYlBu", levels = c("B","D","A","E","C")), LETTERS[1:5])
  )
)


# let's say we have a whole bunch of data points
#   with categories A through E
letter_cat <- LETTERS[round(runif(100,1,5))]
# even with multiple, colorFactor figures it out and same result
previewColors(colorFactor("RdYlBu", domain = NULL), letter_cat)
# same result if we provide a domain
previewColors(colorFactor("RdYlBu", domain = letter_cat), letter_cat)


# now let's exert some control by specifying order
#  in this case reverse
previewColors(
  colorFactor(
    "RdYlBu",
    levels = sort(unique(letter_cat),decreasing=TRUE)
  ),
  letter_cat
)
# in this case some other order
previewColors(
  colorFactor(
    "RdYlBu",
    levels = c("B","D","A","E","C")
  ),
  letter_cat
)