library(here)
library(tidyverse)
library(sf)
library(tmap)
1. 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()
)
More map making in R
There are MANY different ways to make maps in R, all with different pros/cons. Check out this resource for examples of what map making looks like in ggplot2
, leaflet
, and more!
2. Get Started
- Create an
.Rproj
as your version controlled project for Week 2 - Create a Quarto document inside your
.Rproj
- Download this folder from Google Drive and move it inside your
.Rproj
- Load all necessary packages
3. Your Task
You will be wokring with the following datasets:
- Colombia’s Terrestrial Ecosystems (The Nature Conservancy/NatureServe)
- Colombia’s Roads (Esri)
- Bird Observations (DATAVES)
Now, to meet this week’s 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 Challenge: 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