使用 R 在传单地图上显示 SpatialPolygonsDataFrame
Display SpatialPolygonsDataFrame on leaflet map with R
我想在传单地图上显示加拿大的多边形。
# create map
library(leaflet)
m = leaflet() %>% addTiles()
m
我找到了加拿大的多边形:http://www.gadm.org/country。
我为 R 选择了 SpatialPolygonsDataFrame 格式,但还有其他格式可用(例如 Shapefile)
# load object in R
load("country_polygons/CAN_adm0.RData")
pol_can <- gadm
如何在地图上显示形状?我想我必须利用 sp 包,但我不知道该怎么做。
非常感谢您的帮助!
您可以根据文档 here.
的第 2.2 节将 SpatialPolygons*
对象传递给 addPolygons
函数
例如(请注意,以下内容包括约 11.4 MB 的下载):
library(sp)
download.file('http://biogeo.ucdavis.edu/data/gadm2/R/CAN_adm0.RData', f <- tempfile())
load(f)
leaflet() %>% addTiles() %>% addPolygons(data=gadm, weight=2)
注意GADM数据也可以通过raster
包中的getData
功能下载:
library(raster)
can <- getData('GADM', country='VAT', level=0)
编辑
针对评论,我非常喜欢 Natural Earth 提供的轻量级多边形数据集。下面是一个示例,我从 Natural Earth 下载 1:50,000,000 个国家/地区 shapefile (Admin 0),将其子集化为英联邦的当前成员,并绘制它们。压缩后的形状文件小于 1 MB。
library(rgdal)
library(leaflet)
download.file(file.path('http://www.naturalearthdata.com/http/',
'www.naturalearthdata.com/download/50m/cultural',
'ne_50m_admin_0_countries.zip'),
f <- tempfile())
unzip(f, exdir=tempdir())
world <- readOGR(tempdir(), 'ne_50m_admin_0_countries', encoding='UTF-8')
commonwealth <- c("Antigua and Barb.", "Australia", "Bahamas", "Bangladesh",
"Barbados", "Belize", "Botswana", "Brunei", "Cameroon", "Canada", "Cyprus",
"Dominica", "Fiji", "Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya",
"Kiribati", "Lesotho", "Malawi", "Malaysia", "Maldives", "Malta", "Mauritius",
"Mozambique", "Namibia", "Nauru", "New Zealand", "Nigeria", "Pakistan", "Papua
New Guinea", "Rwanda", "St. Kitts and Nevis", "Saint Lucia", "St. Vin. and
Gren.", "Samoa", "Seychelles", "Sierra Leone", "Singapore", "Solomon Is.",
"South Africa", "Sri Lanka", "Swaziland", "Tanzania", "Tonga", "Trinidad and
Tobago", "Tuvalu", "Uganda", "United Kingdom", "Vanuatu", "Zamibia")
leaflet() %>% addTiles() %>%
addPolygons(data=subset(world, name %in% commonwealth), weight=2)
我想在传单地图上显示加拿大的多边形。
# create map
library(leaflet)
m = leaflet() %>% addTiles()
m
我找到了加拿大的多边形:http://www.gadm.org/country。 我为 R 选择了 SpatialPolygonsDataFrame 格式,但还有其他格式可用(例如 Shapefile)
# load object in R
load("country_polygons/CAN_adm0.RData")
pol_can <- gadm
如何在地图上显示形状?我想我必须利用 sp 包,但我不知道该怎么做。 非常感谢您的帮助!
您可以根据文档 here.
的第 2.2 节将SpatialPolygons*
对象传递给 addPolygons
函数
例如(请注意,以下内容包括约 11.4 MB 的下载):
library(sp)
download.file('http://biogeo.ucdavis.edu/data/gadm2/R/CAN_adm0.RData', f <- tempfile())
load(f)
leaflet() %>% addTiles() %>% addPolygons(data=gadm, weight=2)
注意GADM数据也可以通过raster
包中的getData
功能下载:
library(raster)
can <- getData('GADM', country='VAT', level=0)
编辑
针对评论,我非常喜欢 Natural Earth 提供的轻量级多边形数据集。下面是一个示例,我从 Natural Earth 下载 1:50,000,000 个国家/地区 shapefile (Admin 0),将其子集化为英联邦的当前成员,并绘制它们。压缩后的形状文件小于 1 MB。
library(rgdal)
library(leaflet)
download.file(file.path('http://www.naturalearthdata.com/http/',
'www.naturalearthdata.com/download/50m/cultural',
'ne_50m_admin_0_countries.zip'),
f <- tempfile())
unzip(f, exdir=tempdir())
world <- readOGR(tempdir(), 'ne_50m_admin_0_countries', encoding='UTF-8')
commonwealth <- c("Antigua and Barb.", "Australia", "Bahamas", "Bangladesh",
"Barbados", "Belize", "Botswana", "Brunei", "Cameroon", "Canada", "Cyprus",
"Dominica", "Fiji", "Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya",
"Kiribati", "Lesotho", "Malawi", "Malaysia", "Maldives", "Malta", "Mauritius",
"Mozambique", "Namibia", "Nauru", "New Zealand", "Nigeria", "Pakistan", "Papua
New Guinea", "Rwanda", "St. Kitts and Nevis", "Saint Lucia", "St. Vin. and
Gren.", "Samoa", "Seychelles", "Sierra Leone", "Singapore", "Solomon Is.",
"South Africa", "Sri Lanka", "Swaziland", "Tanzania", "Tonga", "Trinidad and
Tobago", "Tuvalu", "Uganda", "United Kingdom", "Vanuatu", "Zamibia")
leaflet() %>% addTiles() %>%
addPolygons(data=subset(world, name %in% commonwealth), weight=2)