r - 在带有传单的数据框列表中绘制多边形

r - Plot polygons in a list of dataframes with leaflet

我有一个包含 88 个多边形的列表,其中每个多边形只是一个带有纬度列和长列的数据框,有没有一种简单的方法可以在传单中绘制这些多边形? 例如mi数据就是变量example的格式,这里显示

df1 <- data.frame(lon=c(-71.53700 -71.51676 -71.51682 -71.51524 -71.53921 -71.53700), lat=c(-32.94082, -32.93327, -32.93325, -32.92814, -32.93408, -32.94082)) 
df2 <- data.frame(lon=c(-72.11527, -72.09601, -72.08983, -72.10905, -72.11527), lat=c(-36.61267, -36.61720, -36.60192, -36.59682, -36.61267))
example <- list(df1, df2)

这是一个通过 sf 将点转换为多边形的示例。请注意,您的第一个多边形基本上是一条线,所以我只绘制了第二个。

library(tidyverse)
library(sf)
library(leaflet)

df1 <- data.frame(lon=c(-71.53700 -71.51676 -71.51682 -71.51524 -71.53921 -71.53700), lat=c(-32.94082, -32.93327, -32.93325, -32.92814, -32.93408, -32.94082)) 
df2 <- data.frame(lon=c(-72.11527, -72.09601, -72.08983, -72.10905, -72.11527), lat=c(-36.61267, -36.61720, -36.60192, -36.59682, -36.61267))
example <- list(df1, df2)

polys <- example %>%
  bind_rows(.id = "df_id") %>%
  st_as_sf(coords = c("lon", "lat"), crs = 4326) %>%
  group_by(df_id) %>%
  summarise(geometry = st_combine(geometry)) %>%
  st_cast("POLYGON")

leaflet(polys[2, ]) %>%
  addPolygons()