如何使用 R 中的地图函数绘制纽约市地图
How to map New York City using map function in R
我想使用 R 中的 map() 函数绘制纽约市地图。
我试过这样做:
map('state', region = c('new york', 'new jersey'),
xlim=c(-74.12,-73.85), ylim=c(40.58,40.87))
使用坐标放大,但地图看起来很小,可读性差。
有一个更好的方法吗?
谢谢,
埃琳娜
我可能是错的,但我不认为 maps
包有这种东西的高分辨率地图数据。
您可以试试 ggmap 包:
library(ggmap)
mymap <- get_map(location = "New York", maptype = "roadmap")
ggmap(mymap)
有关包的简要介绍,请参阅 the ggmap paper。
您也可以留在基础 graphics/ggplot 并使用更好的 shapefile。有 many NYC shapefiles(这只是少数)。我抓住了自治市镇的边界:
library(sp)
library(rgdal)
library(rgeos)
library(ggplot2)
library(ggthemes)
url <- "http://www.nyc.gov/html/dcp/download/bytes/nybb_15b.zip"
fil <- basename(url)
if (!file.exists(fil)) download.file(url, fil)
fils <- unzip(fil)
nyc <- readOGR(fils[1], ogrListLayers(fils[1])[1], stringsAsFactors=FALSE)
# base
plot(nyc, lwd=0.5, asp=1)
# ggplot2
# simplifying the polygons speeds up ggplot2 a bit
nyc_map <- fortify(gSimplify(nyc, 0.05))
gg <- ggplot()
gg <- gg + geom_map(data=nyc_map, map=nyc_map,
aes(x=long, y=lat, map_id=id),
color="black", fill="white", size=0.25)
gg <- gg + coord_equal()
gg <- gg + theme_map()
gg
那个特定的 shapefile 是预先投影的,所以你只需要确保绘图的 1:1 纵横比。
其他 shapefile 提供不同级别的详细信息。
我想使用 R 中的 map() 函数绘制纽约市地图。 我试过这样做:
map('state', region = c('new york', 'new jersey'),
xlim=c(-74.12,-73.85), ylim=c(40.58,40.87))
使用坐标放大,但地图看起来很小,可读性差。 有一个更好的方法吗?
谢谢,
埃琳娜
我可能是错的,但我不认为 maps
包有这种东西的高分辨率地图数据。
您可以试试 ggmap 包:
library(ggmap)
mymap <- get_map(location = "New York", maptype = "roadmap")
ggmap(mymap)
有关包的简要介绍,请参阅 the ggmap paper。
您也可以留在基础 graphics/ggplot 并使用更好的 shapefile。有 many NYC shapefiles(这只是少数)。我抓住了自治市镇的边界:
library(sp)
library(rgdal)
library(rgeos)
library(ggplot2)
library(ggthemes)
url <- "http://www.nyc.gov/html/dcp/download/bytes/nybb_15b.zip"
fil <- basename(url)
if (!file.exists(fil)) download.file(url, fil)
fils <- unzip(fil)
nyc <- readOGR(fils[1], ogrListLayers(fils[1])[1], stringsAsFactors=FALSE)
# base
plot(nyc, lwd=0.5, asp=1)
# ggplot2
# simplifying the polygons speeds up ggplot2 a bit
nyc_map <- fortify(gSimplify(nyc, 0.05))
gg <- ggplot()
gg <- gg + geom_map(data=nyc_map, map=nyc_map,
aes(x=long, y=lat, map_id=id),
color="black", fill="white", size=0.25)
gg <- gg + coord_equal()
gg <- gg + theme_map()
gg
那个特定的 shapefile 是预先投影的,所以你只需要确保绘图的 1:1 纵横比。
其他 shapefile 提供不同级别的详细信息。