Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created November 10, 2015 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/4797245a917b32504844 to your computer and use it in GitHub Desktop.
Save timelyportfolio/4797245a917b32504844 to your computer and use it in GitHub Desktop.
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
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment