Get startedGet started for free

Merging data from different CRS/projections

Every spatial object has a coordinate reference system (CRS) associated with it. Generally, this is set when the data are imported and will be read directly from the spatial files. This is how the neighborhoods and nyc_tracts obtained their coordinate system information.

Both the sp and raster packages have a proj4string() function that returns the CRS of the object it's called on.

Trying to work with spatial data using different CRSs is a bit like trying to work with a dataset in miles and another in kilometers. They are measuring the same thing, but the numbers aren't directly comparable.

Let's take a look at our two polygon objects.

This exercise is part of the course

Visualizing Geospatial Data in R

View Course

Exercise instructions

  • Call proj4string() on neighborhoods, then again on nyc_tracts. Verify the two strings are different.
  • Take a look at the head() of the coordinates() of neighborhoods and repeat for nyc_tracts. Can you see the problem? nyc_tracts has x coordinates around -70, but neighborhoods is around 1,000,000!
  • Plot neighborhoods, then plot nyc_tracts with col = "red" and add = TRUE to add them on top.

Hands-on interactive exercise

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

library(sp)

# proj4string() on nyc_tracts and neighborhoods



# coordinates() on nyc_tracts and neighborhoods



# plot() neighborhoods and nyc_tracts

Edit and Run Code