rr-leafletmapzen

Changing color to leaflet polygon


I am trying to change the color of the polygons drawn using leaflet and Mapzen. The colors are clearly visible with the current tiles but not as much with others, such as the addTiles(). How should I change the argument for the three polygons?

For the code to work you must enter a mapzen key.

library(rmapzen)
library(leaflet)

Sys.setenv(MAPZEN_KEY = "mapzen-******")
#https://tarakc02.github.io/rmapzen/#introduction

ucb <- mz_geocode("Via Giovanni Spadolini 7, Milan, Italy")

isos <- mz_isochrone(
  ucb,
  costing_model = mz_costing$auto(),
  contours = mz_contours(c(5, 10, 15)),
  polygons = TRUE
)

leaflet(as_sp(isos)) %>%

 addProviderTiles("CartoDB.DarkMatter") %>%

 addPolygons(color = ~color, weight = 1) %>%

 addLegend(colors = ~color, 
        labels = ~paste(contour, "minutes"),
            title = "Drive times from <br/> Centro Leoni")

Solution

  • I realized that the polygons should be added separately and instead of:

    leaflet(as_sp(isos))
    

    Solution:

    iso10 <- as_sp(mz_isochrone(
      ucb,
      costing_model = mz_costing$auto(),
      contours = mz_contours(10),
      polygons = TRUE
    ))
    iso30 <- as_sp(mz_isochrone(
      ucb,
      costing_model = mz_costing$auto(),
      contours = mz_contours(30),
      polygons = TRUE
    ))
    iso60 <- as_sp(mz_isochrone(
      ucb,
      costing_model = mz_costing$auto(),
      contours = mz_contours(60),
      polygons = TRUE
    ))
    
    m = leaflet() %>%
      addProviderTiles("CartoDB.DarkMatter") %>%
      addPolygons(data = iso10, color = "red", fillColor = "red")%>%
      addPolygons(data = iso30, color = "green", fillColor = "green")%>%
      addPolygons(data = iso60, color = "blue", fillColor = "blue")