r - How to add multiple polygons and layers in leaflet with a for loop?
问题描述
I have created a map in leaflet with numerous polygons by breaking the larger dataset down into smaller ones based on their fuel types. The map works great and as intended, however, I want to slim my code down with either a function or a for loop that will reproduce the numerous polygon layers.
I have included my code for the map below and as you can see it is a lot. I found this post, How to add multiple polygons in leaflet map using r loop?, but am still having trouble with this particular issue since I am trying create multiple polygons from total_ERCOT, head included below, by fuel type without having to subset the data into smaller data sets (ERCOT_gas, ERCOT_wind, etc.). Any help would be much appreciated!
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))
解决方案
这样做:
# Create the underlying map
map <- leaflet() %>%
addTiles()
# Loop over various polygons to include.
for (...) {
map <- map %>%
addPolygons(...)
}
map %>%
addLayersControl(...)
您甚至可以overlayGroups
在 for 循环中构建向量。
推荐阅读
- javascript - Javascript vs VBScript vs VBA中的OLEDB / ADO查询速度
- c++ - 是否可以使用整数数组初始化双向量?
- css - 如何将 2 个按钮彼此相邻居中
- java - 我的任务是创建一个 Java 应用程序,该应用程序使用 2 个同步线程来计算每月利息并更新帐户余额
- c++ - msgpack:在不知道类型的情况下解压自定义类
- php - 在 SQL 中将字符串从 PHP 转换为 int
- c - 在C中通过http发送图像
- c++ - 下一个排列定义
- spring-cloud-stream - 用于多租户服务的 Spring Cloud Stream
- python - 获取函数的源文件行号