R – How to map New York City using map function in R

mapsr

I want to draw a map of NYC using the function map() in R.
I tried doing something like this:

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

Using the coordinates to zoom in, but the map looks small and not very readable.
Is there a better way to do this?

Thanks,

Elena

Best Answer

You can also stay in base graphics/ggplot and use better shapefiles. There are many NYC shapefiles (those are just a few). I grabbed the borough borders:

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)

enter image description here

# 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

enter image description here

That particular shapefile is pre-projected, so you just need to ensure a 1:1 aspect ratio for the plots.

Other shapefiles give varying levels of detail.

Related Topic