Reading in a shapefile
Shapefiles are one of the most common ways spatial data are shared and are easily read into R using readOGR()
from the rgdal
package. readOGR()
has two important arguments: dsn
and layer
. Exactly what you pass to these arguments depends on what kind of data you are reading in. You learned in the video that for shapefiles, dsn
should be the path to the directory that holds the files that make up the shapefile and layer
is the file name of the particular shapefile (without any extension).
For your map, you want neighborhood boundaries. We downloaded the Neighborhood Tabulation Areas, as defined by the City of New York, from the Open Data Platform of the Department of City Planning. The download was in the form of a zip archive and we have put the result of unzipping the downloaded file in your working directory.
You'll use the dir()
function from base R to examine the contents of your working directory, then read in the shapefile to R.
Este exercício faz parte do curso
Visualizing Geospatial Data in R
Instruções do exercício
- Use
dir()
with no arguments to find out the name of the directory of the shapefile. - Use
dir()
, passing in the path to the shapefile directory, to see the files inside. - Now you know the directory and file name. Use
readOGR()
to read the neighborhood shapefile into an object calledneighborhoods
. - Check the contents by calling
summary()
onneighborhoods
. - Check the contents by plotting
neighborhoods
.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
library(sp)
library(rgdal)
# Use dir() to find directory name
# Call dir() with directory name
# Read in shapefile with readOGR(): neighborhoods
# summary() of neighborhoods
# Plot neighborhoods