R leaflet adding colors for character variables

18,704

Extending on MLavoie's comment, you need to define a palette function first. There's a couple ways you can do this, two of which I've illustrated below, with a completely made up data set. Hopefully, it will work for your data as well.

If you don't have a whole lot of factors, and want to set colors manually, you can certainly do that. That's the first pal function below.

If you have a lot of factors, such that setting colors manually isn't a good option, try using a RColorBrewer palette. That's the second pal function below. Use whichever you prefer - you don't need both pal functions, I just included them as a reference. If you want to see all palette name options, call RColorBrewer::display.brewer.all().

library('leaflet')

# Fake data
df <- data.frame(lng = c(-5, -10, -15, -20, 25),
                 lat = c(8, 12, 33, 4, 18),
                 size = c(200000, 100000, 800000, 250000, 350000),
                 popup = c('A', 'B', 'C', 'D', 'E'),
                 type = c('A', 'B', 'C', 'D', 'E'),
                 stringsAsFactors = FALSE)

# If you want to set your own colors manually:
pal <- colorFactor(
  palette = c('red', 'blue', 'green', 'purple', 'orange'),
  domain = df$type
)

# If you want to use predefined palettes in the RColorBrewer package:
# Call RColorBrewer::display.brewer.all() to see all possible palettes
pal <- colorFactor(
  palette = 'Dark2',
  domain = df$type
)

leaflet(df) %>%
  addTiles() %>%
  addCircles(lng = ~lng, lat = ~lat, weight = 1, 
             radius = ~size, popup = ~popup, color = ~pal(type))
Share:
18,704

Related videos on Youtube

dnsko
Author by

dnsko

Updated on May 15, 2022

Comments

  • dnsko
    dnsko 14 days

    I am attempting to create a Leaflet map in R, but the only thing I would like to add from now on are the colors for the circles that should differ per variable. In my data I have a column Data.Type that has approximately 5 different values in the dataset. What I would like is to have circles for these data points, in 5 different colors.

      output$map <- renderLeaflet({
        leaflet() %>%
          addTiles(
            urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png"
          ) %>%
          setView(lng = x, lat = x, zoom = 4)
    
        leaflet(getData()) %>% addTiles() %>%
          addCircles(lng = ~Longitude, lat = ~Latitude, weight = 1, radius=~Var, popup = ~Var, color=~Data.Type
          )
      })
    

    Only this will return grey-ish circles only, not 5 different. How should this be done?

    Edit: Data looks somewhat like this, actually it is more than 20.000 rows

    ID    Var    Var    Var   Data.Type
    0     99     2016   743    Type A
    1     99     2016   209    Type B
    2     99     2016   349    Type A
    3     99     2016   349    Type D
    4     99     2016   993    Type A
    5     99     2016   122    Type E
    6     99     2016   194    Type B
    7     99     2016   660    Type A
    8     99     2016   221    Type C
    ...
    

    The idea is to have a color for each var in Data.Type, which can only have approximately 6 different values

  • Allen Wang
    Allen Wang over 4 years
    Thanks for your answer Sam, spent two days messing around with no luck until I found this!