根据许多国家代码列表,我正在尝试将多个多边形添加到传单世界地图。我试图使用 R 循环来添加多边形。这是我从列表 1 中手动添加多边形的代码:
library(sp)
library(raster)
library(leaflet)
library(maps)
library(tidyverse)
countries_1 <- c('PAK','TUR','BGR')
adm1 <- getData('GADM', country='PAK', level=0)
adm2 <- getData('GADM', country= 'TUR', level=0)
adm3 <- getData('GADM', country= 'BGR', level=0)
leaflet() %>%
addTiles() %>%
addPolygons(data=adm1, weight = 3, fillColor = 'purple', color = 'purple') %>%
addPolygons(data=adm2, weight = 3, fillColor = 'purple', color = 'purple') %>%
addPolygons(data=adm3, weight = 3, fillColor = 'purple', color = 'purple')
我正在考虑使用循环添加多个多边形层,以便为 list_n:
countries_n <- ('ctry1','ctry2','ctry3',...'ctryn')
for (i in country_n) {
countries <- basemap %>% addPolygons(data=getData('GADM',country = i, level = 0),
weight = 3, fillColor = 'purple', color = 'purple')
}
问题是如何将循环嵌入到 "leflet() %>%" ?
*此处注意:如果尝试在 addPolygons() 中添加多个数据,它将仅绘制数据中的第一个元素,在以下情况下,将仅绘制国家“PAK”:
addPolygons(data=c('PAK','TUR'), weight = 3, fillColor = 'purple', color = 'purple')