用传单绘制特定国家的地图 [英] Draw a map of a specific country with leaflet
问题描述
我想用 R 的 leaflet
包来绘制特定国家的地图,例如意大利、西班牙等.
I would like to use the package leaflet
with R to draw a map of a specific countries such as Italy, Spain, etc.
我使用函数 setView()
检查了基本示例,并尝试为纬度和经度 arg 提供两个值的向量:
I checked the basic examples with the function setView()
and I tried to give a vector of two values for the arg of latitude and longitutde :
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
setView(lng=c(46.00,48.00), lat=c(2.00,6.00), zoom = 4)
m # Print the map (map is not centered on a country, it's just a test)
但我永远无法在我的屏幕上显示特定国家/地区,例如此功能的结果:
But I will never be able to have a specific country on my screen such as the result of this function :
library(maps)
map('italy', fill = TRUE, col = 1:10)
最后,我只是想通过在我的地图上进行地理定位来绘制一些点(带有经纬度)
In the end, I just want to draw some points by geographically locating them on my maps (with latitude and longtitude)
是否有可能,或者 maps
包是否足以完成这项任务(即使我没有找到放大的方法)?
Is it possible or is the package maps
is adequate for this task (even though I didn't find a way to zoom in) ?
推荐答案
maps
包将 shapefile 数据作为顶点传送.Afaik 没有像这样的东西包含在传单中.因此,您将不得不在其他地方获取数据.这是我的建议:
The maps
package ships shapefile data as vertices. Afaik nothing like this is included in leaflet. So you will have to get your data elsewhere. Here is my suggestion:
# Get an Italy shapefile
download.file(url = 'http://biogeo.ucdavis.edu/data/diva/adm/ITA_adm.zip',
destfile = 'italy.zip')
unzip(zipfile = 'italy.zip')
# Load libraries
library(sp)
library(rgdal)
italy <- readOGR('ITA_adm0.shp')
library(leaflet)
leaflet(italy) %>%
addPolygons() %>%
addTiles()
附加功能:
您可以使用以下代码查看保存在 maps
包中的构成意大利的顶点.
You can look at the vertices that make up italy as saved in the maps
package with the code below.
library(maps)
italy <- map('italy', fill = TRUE, col = 1:10)
italy_coords <- cbind(italy$x, italy$y)
plot(italy_coords)
这篇关于用传单绘制特定国家的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!