【问题标题】:How to map New York City using map function in R如何使用 R 中的地图功能绘制纽约市地图
【发布时间】:2015-08-27 14:43:52
【问题描述】:

我想使用 R 中的函数 map() 绘制纽约市的地图。 我试着做这样的事情:

map('state', region = c('new york', 'new jersey'),
    xlim=c(-74.12,-73.85), ylim=c(40.58,40.87)) 

使用坐标放大,但是地图看起来很小,不太可读。 有一个更好的方法吗?

谢谢,

埃琳娜

【问题讨论】:

标签: r maps


【解决方案1】:

您也可以留在基础图形/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 提供不同级别的详细信息。

【讨论】:

    【解决方案2】:

    我可能是错的,但我不认为maps 包有这种东西的高分辨率地图数据。

    您可以尝试使用 ggmap 包:

    library(ggmap)
    mymap <- get_map(location = "New York", maptype = "roadmap")
    ggmap(mymap)
    

    请参阅 the ggmap paper 以了解该软件包的简要介绍。

    【讨论】:

      猜你喜欢
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多