“A coordinate reference system (CRS)… defines how the two-dimensional, projected map… relates to real places on the earth. The decision of which map projection and CRS to use depends on the regional extent of the area you want to work in, on the analysis you want to do, and often on the availability of data.” 1
There are potentially many components to a CRS. Here are a few of the most important components of a CRS:
Each of these in treated in more detail below.
A projection is the way you go from a 3 dimensional reality (like the earth), to a two-dimensional representation (like a map). In making a projection, or a map, there are 3 things you would like to represent: area, distance and direction. Unfortunately, as these videos point out, you cannot represent all three things perfectly. Difficult trade-offs must be made. (Watch one of the below videos, though both are entertaining).
The coordinate system refers to how you will label, or number, the coordinates on your projection. There are two general types of coordinate systems: geographic and projected. Geographic coordinate systems are very common. They use degrees of latitude and longitude to specify a location. Projected coordinate systems identify points on the coordinates some other way, for example how many meters from the equator in the y direction, or how many meters from the central meridian in the x direction. They may also define which direction (east or west) represents an increment in the coordinate.
“A datum reference or just datum is some important part of an object—such as a point, line, plane, hole, set of holes, or pair of surfaces—that serves as a reference in defining the geometry of the object and (often) in measuring aspects of the actual geometry.”2
It may be helpful to think of the datum as the (0,0) point of your coordinate reference system.
These two videos do a good job explaining datums with regards to map making and Geographic Information Systems. You should watch them both.
There are numerous formats that are used to document a CRS. Three common formats include:
You should read the article at this link, paying special attention to understanding conceptually how to interpret a proj4 string, what is an EPSG code, and how the two relate:
Remember, commands from the sf package usually start with st_ . The
code below gets the shape data for Idaho counties. Shape files come with
a default CRS defined. The code below will output the definition of the
CRS for the Idaho counties sf file (probably in wkt
format).
library(sf)
ID_counties <- USAboundaries::us_counties(states = "ID")
st_crs(ID_counties)
## Coordinate Reference System:
## User input: EPSG:4326
## wkt:
## GEOGCRS["WGS 84",
## DATUM["World Geodetic System 1984",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["geodetic latitude (Lat)",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["geodetic longitude (Lon)",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## USAGE[
## SCOPE["Horizontal component of 3D system."],
## AREA["World."],
## BBOX[-90,-180,90,180]],
## ID["EPSG",4326]]
This section will illustrate the following:
Changing the CRS of an sf object dataset
First create the CRS using the proj4 string format. Note, you don’t have to define much, in this case the datum and the projection method is sufficient.
my_proj <- "+proj=robin +datum=WGS84"
Then use st_transform()
to change the existing crs to
the one defined in the line of code above. Then view the CRS of the
newly created ID_tr
to verify the change took place.
ID_tr <- ID_counties %>% st_transform(crs = my_proj)
st_crs(ID_tr)
## Coordinate Reference System:
## User input: +proj=robin +datum=WGS84
## wkt:
## PROJCRS["unknown",
## BASEGEOGCRS["unknown",
## DATUM["World Geodetic System 1984",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]],
## ID["EPSG",6326]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8901]]],
## CONVERSION["unknown",
## METHOD["Robinson"],
## PARAMETER["Longitude of natural origin",0,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8802]],
## PARAMETER["False easting",0,
## LENGTHUNIT["metre",1],
## ID["EPSG",8806]],
## PARAMETER["False northing",0,
## LENGTHUNIT["metre",1],
## ID["EPSG",8807]]],
## CS[Cartesian,2],
## AXIS["(E)",east,
## ORDER[1],
## LENGTHUNIT["metre",1,
## ID["EPSG",9001]]],
## AXIS["(N)",north,
## ORDER[2],
## LENGTHUNIT["metre",1,
## ID["EPSG",9001]]]]
Now compare the maps: they both use the same geometries but have different CRS’s.
ggplot() + geom_sf(data = ID_counties) + theme_bw()
ggplot() + geom_sf(data = ID_tr) + theme_bw()
Change the CRS of a map on the fly with a proj4 string
In the code below, the CRS of the visualization is changed, but the
default CRS associated with ID_counties
has not been
changed.
ggplot() +
geom_sf(data = ID_counties) +
theme_bw() +
coord_sf(crs = st_crs("+proj=robin +datum=WGS84"))
The next block of code illustrates using an EPSG code instead of a
proj4 string as an argument input to the st_crs()
command
and/or to the st_transform()
command.
ggplot() +
geom_sf(data = ID_counties) +
theme_bw() +
coord_sf(crs = st_crs(5041))
Note that st_crs()
can work outside of the ggplot
statement too, but only if the object doesn’t already have a CRS
defined. That’s why we use st_transform()
outside of the
ggplot statement, because it will alter the CRS regardless of whether
there is already CRS information attached to an object. However,
st_transform()
doesn’t work well in the
coord_sf()
statement of a ggplot, but st_crs()
does.