rleafletshiny

Generate random colors for leaflet map


Great R Gurus,

I am struggling to find a way to randomly mix discrete colors for my leaflet map for a large number of observations. The problem is the following command put similar colors next to each other which makes it hard to distinguish on the map.

library(leaflet) 
previewColors(colorFactor("RdYlBu", domain = NULL), LETTERS[1:26])

enter image description here

Is there any way to mix colors in a way it becomes A,Z,B,Y and so on...

Your time to answer is highly appreciated...


Solution

  • Maybe something like this:

    library(leaflet) 
    set.seed(50)
    previewColors(colorFactor(sample(colors(),26), domain = NULL), LETTERS[1:26])
    

    To get an output as follows:enter image description here

    Or you could do something like this: library(leaflet) set.seed(500) previewColors(colorFactor(sample(rainbow(26),26), domain = NULL), LETTERS[1:26])

    To get the output as follows: enter image description here