David Cheminet David Cheminet - 2 months ago 12
R Question

Leaflet in R - Multi marker depending a variable in a data frame

I'm using leaflet in R and I want the markers to be "red" or "green" or "orange", depending a variable in a data frame.

How could I do that?

and and
Answer

You can not change the color of the normal marker - so far I know. What you can do is use circles:

library(leaflet)

m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  setView(13, 42, zoom = 4) %>% 
  addCircles(c(13,0,10), c(42,40,45), radius = c(50000, 1, 100000), color=c("red", "orange", "blue"))
m  # Print the map

or create your own marker, this function like this:

Rlogo = file.path(R.home('doc'), 'html', 'logo.jpg')
m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  setView(13, 42, zoom = 4) %>% 
  addMarkers(
    5, 47, icon = list(
      iconUrl = Rlogo, iconSize = c(100, 76)/2
    )
  )
m