【问题标题】:How to add multiple polygons and layers in leaflet with a for loop?如何使用 for 循环在传单中添加多个多边形和图层?
【发布时间】:2021-10-24 20:51:32
【问题描述】:

我通过根据燃料类型将较大的数据集分解为较小的数据集,在传单中创建了一张包含大量多边形的地图。该地图效果很好,并且符合预期,但是,我想使用一个函数或一个 for 循环来简化我的代码,以重现众多多边形层。

我在下面的地图中包含了我的代码,如您所见,代码很多。我找到了这篇文章How to add multiple polygons in leaflet map using r loop?,但我仍然遇到了这个特定问题,因为我正在尝试从以下包含的总ERCOT创建多个多边形,按燃料类型,而不必将数据子集为较小的数据集(ERCOT_gas,ERCOT_wind, ETC。)。任何帮助将不胜感激!

head(total_ERCOT)
Simple feature collection with 6 features and 13 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -97.60523 ymin: 29.26259 xmax: -95.42412 ymax: 31.86308
geographic CRS: NAD83
# A tibble: 6 x 14
# Groups:   NAME, FUEL [3]
  GEOID NAME  `UNIT NAME` `UNIT CODE` FUEL  ZONE  `IN SERVICE` CAPACITY county_capacity   age                  geometry county_age county_fuel_cap~
  <chr> <chr> <chr>       <chr>       <chr> <chr>        <dbl>    <dbl>           <dbl> <dbl>        <MULTIPOLYGON [°]>      <dbl>            <dbl>
1 48309 MCLE~ SANDY CREE~ SCES_UNIT1  COAL  NORTH         2013     933.            948.     8 (((-97.27726 31.74549, -~       4.33             933.
2 48157 FORT~ W A PARISH~ WAP_WAP_G5  COAL  HOUS~         1977     664            4291     44 (((-96.01644 29.6271, -9~      37.1             2514 
3 48157 FORT~ W A PARISH~ WAP_WAP_G6  COAL  HOUS~         1978     663            4291     43 (((-96.01644 29.6271, -9~      37.1             2514 
4 48157 FORT~ W A PARISH~ WAP_WAP_G7  COAL  HOUS~         1980     577            4291     41 (((-96.01644 29.6271, -9~      37.1             2514 
5 48157 FORT~ W A PARISH~ WAP_WAP_G8  COAL  HOUS~         1982     610            4291     39 (((-96.01644 29.6271, -9~      37.1             2514 
6 48149 FAYE~ FAYETTE PO~ FPPYD1_FPP~ COAL  SOUTH         1979     603            1841     42 (((-97.08646 29.9943, -9~      23.4             1657 
leaflet() %>%
  addTiles() %>%
  addPolygons(data = ERCOT_counties, stroke = FALSE, smoothFactor = 0.3, fillOpacity = 0.5, 
              fillColor = ~pal(county_capacity), 
              label = ~paste0(NAME, ": ", format(round(as.numeric(county_capacity, 0)), 
                                                 big.mark = ","), " MW ", 
                              " Average Generation Facility Age: ", round(county_age, 2), " Years", 
                              " Zone: ", ZONE),group = "Total Capacity") %>%
  addPolygons(data = ERCOT_gas, stroke = FALSE, smoothFactor = 0.3, fillOpacity = 0.5, 
              fillColor = ~pal(county_gas_capacity), 
              label = ~paste0(NAME, ": ", format(round(as.numeric(county_gas_capacity, 0)), 
                                                 big.mark = ","), " MW ", 
                              " Average Gas Facility Age: ", round(county_gas_age, 2), " Years", 
                              " Zone: ", ZONE), group = "Gas Capacity") %>%
  addPolygons(data = ERCOT_wind, stroke = FALSE, smoothFactor = 0.3, fillOpacity = 0.5, 
              fillColor = ~pal(county_wind_capacity), 
              label = ~paste0(NAME, ": ", format(round(as.numeric(county_wind_capacity, 0)), 
                                                 big.mark = ","), " MW ",
                              " Average Wind Facility Age: ", round(county_wind_age, 2), " Years", 
                              " Zone: ", ZONE), group = "Wind Capacity") %>%
  addPolygons(data = ERCOT_solar, stroke = FALSE, smoothFactor = 0.3, fillOpacity = 0.5, 
              fillColor = ~pal(county_solar_capacity), 
              label = ~paste0(NAME, ": ", format(round(as.numeric(county_solar_capacity, 0)), 
                                                 big.mark = ","), " MW ",
                              " Average Solar Facility Age: ", round(county_solar_age, 2), " Years", 
                              " Zone: ", ZONE), group = "Solar Capacity") %>%
  addPolygons(data = ERCOT_coal, stroke = FALSE, smoothFactor = 0.3, fillOpacity = 0.5, 
              fillColor = ~pal(county_coal_capacity), 
              label = ~paste0(NAME, ": ", format(round(as.numeric(county_coal_capacity, 0)), 
                                                 big.mark = ","), " MW ",
                              " Average Coal Facility Age: ", round(county_coal_age, 2), " Years", 
                              " Zone: ", ZONE), group = "Coal Capacity") %>%
  addPolygons(data = ERCOT_nuclear, stroke = FALSE, smoothFactor = 0.3, fillOpacity = 0.5, 
              fillColor = ~pal(county_nuclear_capacity), 
              label = ~paste0(NAME, ": ", format(round(as.numeric(county_nuclear_capacity, 0)), 
                                                 big.mark = ","), " MW ",
                              " Average Nuclear Facility Age: ", round(county_nuclear_age, 2), " Years", 
                              " Zone: ", ZONE), group = "Nuclear Capacity") %>%
  addLayersControl(
    overlayGroups = c("Total Capacity", "Gas Capacity", "Wind Capacity", "Solar Capacity", "Coal Capacity", "Nuclear Capacity"),
    options = layersControlOptions(collapsed = FALSE))

【问题讨论】:

  • 你能为我们提供一个示例数据吗?

标签: r r-leaflet


【解决方案1】:

这样做:

# Create the underlying map
map <- leaflet() %>%
    addTiles()

# Loop over various polygons to include.
for (...) {
    map <- map %>%
            addPolygons(...)
}

map %>%
    addLayersControl(...)

您甚至可以在 for 循环中构建 overlayGroups 向量。

【讨论】:

    猜你喜欢
    • 2017-11-07
    • 2020-01-24
    • 2016-10-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多