Getting data using a package
Reading in spatial data from a file is one way to get spatial data into R, but there are also some packages that provide commonly used spatial data. For example, the rnaturalearth package provides data from Natural Earth, a source of high resolution world maps including coastlines, states, and populated places. In fact, this was the source of the data from Chapter 2.
You will be examining median income at the census tract level in New York County (a.k.a. the Bourough of Manhattan), but to do this you'll need to know the boundaries of the census tracts. The tigris package in R provides a way to easily download and import shapefiles based on US Census geographies. You'll use the tracts() function to download tract boundaries, but tigris also provides states(), counties(), places() and many other functions that match the various levels of geographic entities defined by the Census.
Let's grab the spatial data for the tracts.
Deze oefening maakt deel uit van de cursus
Visualizing Geospatial Data in R
Oefeninstructies
- Call
tracts()withstate = "NY",county = "New York", andcb = TRUE. Store the result innyc_tracts. - Use
summary()onnyc_tractsto verify the retuned object is aSpatialPolygonsDataFrame. - Plot
nyc_tractsto check the contents withplot().
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
library(sp)
library(tigris)
# Call tracts(): nyc_tracts
# Call summary() on nyc_tracts
# Plot nyc_tracts