library(here)
library(tidyverse)
library(sf)
library(tmap)
Reference
Refer to the Plotting Geospatial Data resource to help get you started.
Learning Objectives
- Use
sf::st_read()
to read multiple vector data types - Retreive the CRS of a vector object with
sf::st_crs()
- Transform CRS and match across all vector data types with
sf::st_transform()
- Perform
dplyr
attribute manipulations (e.g.filter()
,mutate()
,select()
)
Data
- Colombia’s Terrestrial Ecosystems (The Nature Conservancy/NatureServe)
- Colombia’s Roads (Esri)
- Bird Observations (DATAVES)
Get Started
Let’s load the necessary packages:
Now, to meet our learning objectives, your task:
- Read in the data for Colombia’s ecoregions, roads, and bird observations
- Use
st_read()
to read vector data (e.g., .shp, .gdb) - Use
rename()
ormutate()
to rename the columnsdecimal_longitude
anddecimal_latitude
tolong
andlat
inaves
andsf::st_as_sf()
to convert it into an sf object- Hint: To convert a table into a vector object, you can use
st_as_sf()
but remember to check theclass()
of an object first!
- Hint: To convert a table into a vector object, you can use
- Check
class()
of all vector objects (including the convertedaves
) and usesf::st_geometry_type()
to check the geometry type - Use
filter()
to select a macro region of interest fromN1_MacroBi
in Colombia’s ecoregions dataset and save as a new vector data
- Check
class()
of the new vector data - Plot the new vector data using
tmap
- Use
st_crs()
to retrieve CRS of all vector objects and assign a new CRS
- Bonus: Check units of your object with
st_crs()$units
- Check CRS of all vector objects with
st_crs()
st_crs() <- NA
is a brute force way to remove a CRS, instead:- For the bird observations dataset, extract the longitude and latitude from the
geometry
column and usesf::st_drop_geometry()
- Convert
long
andlat
into a geometry again withst_as_sf()
to obtain a propersf
data frame
- For the bird observations dataset, extract the longitude and latitude from the
- Let’s bring all vector data types together
- Check that the CRS of the ecoregions and roads datasets match
- Transform CRS of the bird observations data using
sf::st_transform()
to match with the other datasets - Use
tmap
to plot the ecoregions, roads, and bird observations together