1

With this script, I am displaying a map with three isochrones. I would like the map to have labels containing the max time represented by each isochrone/polygon.

How should I reference the spatial data frames (iso1/2/3) in the addPolygons() section?

In each of the three addPolygons() bellow I tried a different approach but no luck :( (although the script still works).

library(osrm)
library(leaflet)
library(viridisLite)

# Making isochrones
iso1 = osrmIsochrone(loc = c(9.2,45.5), 
                    breaks = seq(from = 0,
                                 to = 45, 
                                 by = 5),
                    res=75)
iso2 = osrmIsochrone(loc = c(12.51182,41.92631), 
                     breaks = seq(from = 0,
                                  to = 45, 
                                  by = 15),
                     res=100)
iso3 = osrmIsochrone(loc = c(11.25581,43.76956), 
                     breaks = seq(from = 0,
                                  to = 45, 
                                  by = 15),
                     res=100)

# colors for leaflet 
vir = viridis(9)

# palette
pal1 <- colorNumeric(
  palette = vir,
  domain = iso1@data$id)
pal2 <- colorNumeric(
  palette = "Blues",
  domain = iso2@data$id)
pal3 <- colorNumeric(
  palette = "Reds",
  domain = iso3@data$id)

# Plotting interactive map using spdf
leaflet()%>%
  addTiles("http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", attribution = 'Google')%>%
  addPolygons(data = iso1,
              fill = TRUE, 
              fillOpacity = 0.7,
              fillColor = ~pal1(id),stroke = FALSE, label = iso1@data$max)%>%
  addPolygons(data = iso2,
              fill = TRUE, 
              fillOpacity = 0.7,
              fillColor = ~pal2(id),stroke = FALSE, label = ~max)%>%
  addPolygons(data = iso3,
              fill = TRUE, 
              fillOpacity = 0.7,
              fillColor = ~pal3(id),stroke = FALSE, label = ~iso3@data$max)
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Claudio Paladini
  • 1,000
  • 1
  • 10
  • 20

2 Answers2

3

It works if you provide the max values as.character to the label.

library(osrm)
library(leaflet)
library(viridisLite)

# Making isochrones
iso1 = osrmIsochrone(loc = c(9.2,45.5), 
                     breaks = seq(from = 0,
                                  to = 45, 
                                  by = 5),
                     res=75)
iso2 = osrmIsochrone(loc = c(12.51182,41.92631), 
                     breaks = seq(from = 0,
                                  to = 45, 
                                  by = 15),
                     res=100)
iso3 = osrmIsochrone(loc = c(11.25581,43.76956), 
                     breaks = seq(from = 0,
                                  to = 45, 
                                  by = 15),
                     res=100)

# colors for leaflet 
vir = viridis(9)

# palette
pal1 <- colorNumeric(
  palette = vir,
  domain = iso1@data$id)
pal2 <- colorNumeric(
  palette = "Blues",
  domain = iso2@data$id)
pal3 <- colorNumeric(
  palette = "Reds",
  domain = iso3@data$id)

# Plotting interactive map using spdf
leaflet()%>%
  addTiles("http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", attribution = 'Google')%>%
  addPolygons(data = iso1,
              fill = TRUE, 
              fillOpacity = 0.7,
              fillColor = ~pal1(id),stroke = FALSE, label = as.character(iso1@data$max))%>%
  addPolygons(data = iso2,
              fill = TRUE, 
              fillOpacity = 0.7,
              fillColor = ~pal2(id),stroke = FALSE, label = as.character(iso2@data$max))%>%
  addPolygons(data = iso3,
              fill = TRUE, 
              fillOpacity = 0.7,
              fillColor = ~pal3(id),stroke = FALSE, label = as.character(iso3@data$max))
TimSalabim
  • 5,604
  • 1
  • 25
  • 36
0
leaflet()%>%
  addTiles("http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", attribution = 'Google')%>%
  addPolygons(data = iso1,
              fill = TRUE, 
              group = "label1",
              fillOpacity = 0.7,
              fillColor = ~pal1(id),stroke = FALSE, label = iso1@data$max)%>%
  addPolygons(data = iso2,
              fill = TRUE, 
              group = "label2",
              fillOpacity = 0.7,
              fillColor = ~pal2(id),stroke = FALSE, label = ~max)%>%
  addPolygons(data = iso3,
              fill = TRUE, 
              group = "label3",
              fillOpacity = 0.7,
              fillColor = ~pal3(id),stroke = FALSE, label = ~iso3@data$max)

Use the group argument to specify the label.

troh
  • 1,354
  • 10
  • 19