Get Started

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.

This is a part of the course

“Visualizing Geospatial Data in R”

View Course

Exercise instructions

  • 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 called neighborhoods.
  • Check the contents by calling summary() on neighborhoods.
  • Check the contents by plotting neighborhoods.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

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
Edit and Run Code

This exercise is part of the course

Visualizing Geospatial Data in R

IntermediateSkill Level
4.4+
9 reviews

Learn to read, explore, and manipulate spatial data then use your skills to create informative maps using R.

In this chapter you'll follow the creation of a visualization from raw spatial data files to adding a credit to a map. Along the way, you'll learn how to read spatial data into R, more about projections and coordinate reference systems, how to add additional data to a spatial object, and some tips for polishing your maps.

Exercise 1: Reading in spatial dataExercise 2: Reading in a shapefile
Exercise 3: Reading in a raster fileExercise 4: Getting data using a packageExercise 5: Coordinate reference systemsExercise 6: Merging data from different CRS/projectionsExercise 7: Converting from one CRS/projection to anotherExercise 8: Adding data to spatial objectsExercise 9: The wrong wayExercise 10: Checking data will matchExercise 11: Merging data attributesExercise 12: A first plotExercise 13: Polishing a mapExercise 14: Subsetting the neighborhoodsExercise 15: Adding neighborhood labelsExercise 16: Tidying up the legend and some final tweaksExercise 17: Wrap up

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free