The projection problem, and how to check your CRS.
An important subtlety of geographic data visualization is that all our maps are in 2D, but earth is 3D. The process of associated points on the earth with 2D coordinates is called « projection. » All projections introduce some level of distortion, and there is no universal, ideal projection.
Here are a few examples of famous global projections. There are also many projections designed to give optimal representations locally within a particular geographic area.
This means that there is no universal standard for how to represent coordinates on the earth, and it’s common to find different projections in practice. For example, the block below shows how North America gets projected according to two different CRSs.
# some test projections, don't worry about this syntax
miller <- "+proj=mill +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
lambert <- "+proj=lcc +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
north_america <- world %>%
filter(continent == "North America")
ggplot(st_transform(north_america, miller)) + geom_sf()
ggplot(st_transform(north_america, lambert)) + geom_sf()
Both vector and raster spatial data will be associated with CRS’s. In either sf
or raster
objects, they can be accessed using the crs
function.
A common source of bugs is to use two different projections for the same analysis. In this class, we will always use the EPSG:4326 projection, which is what is used in most online maps. But in your own projects, you should always check that the projections are consistent across data sources. If you find an inconsistency, it will be important to « reproject » the data into the same CRS.