1

使用此脚本,我将显示具有三个等时线的地图。我希望地图具有包含每个等时线/多边形表示的最大时间的标签。

我应该如何在 addPolygons() 部分引用空间数据框 (iso1/2/3)?

在下面的三个 addPolygons() 中的每一个中,我都尝试了不同的方法,但没有运气:((尽管脚本仍然有效)。

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)
4

2 回答 2

3

as.character如果您为标签提供最大值,它会起作用。

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))
于 2018-01-24T20:07:27.267 回答
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)

使用 group 参数指定标签。

于 2018-01-24T17:39:12.343 回答