Working with spatial data in R: Vector

EVR 628- Intro to Environmental Data Science

Author
Affiliation

Juan Carlos Villaseñor-Derbez (JC)

Rosenstiel School of Marine, Atmospheric, and Earth Science and Institute for Data Science and Computing

Exercise 0 - Set up

  1. Open your EVR628 R project
  2. Install / update the packages as needed using the code below:
install.packages("sf")            # You should have this from Tuesday
install.packages("rnaturalearth") # You should have this from Tuesday
install.packages("mapview")       # You should have this from Tuesday
remotes::install_github("jcvdav/EVR628tools")
  1. Start a new R script called vector_data.R and save it under scripts/02_analysis.R
  2. Add a code header
  3. Load the tidyverse, janitor, sf, rnaturalearth, mapview, and EVR628tools

Exercise 1 - Inspecting spatial data

Part 1: FL counties

Post-it up

  1. Go to the FDOT website and download the county Geopackage file1. Make sure you save them to your data/raw folder.
  2. Look at the documentation for ?read_sf
  3. Read the FL counties geopackage and assign it to an object called FL_counties <-
  4. Immediately after that, pipe into clean_names() and then again into st_set_geometry("geometry"). FDOT data are weird, I will explain why we are doing this.

Post-it down

Code
# Load packages
library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'dplyr' was built under R version 4.5.2
Code
library(janitor)
library(sf)
library(rnaturalearth)
library(mapview)
library(EVR628tools)

# Load FL counties
FL_counties <- read_sf("data/raw/Florida_County_Boundaries_with_FDOT_Districts_-801746881308969010.gpkg") |> 
  clean_names() |> 
  st_set_geometry("geometry")
Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, : GDAL
Message 1: Non-conformant content for record 1 in column CreationDate,
2025-10-07T20:05:32.0Z, successfully parsed

Part 2: Inspect FL counties

Post-it up

  1. How many columns do we have? How many rows?
  2. What are the column names?
  3. Use R’s base plot() to visualize them. CAREFUL, make sure you specify max.plot = 1.
  4. Now use mapview to visualize your data.

Post-it down

Code
# Inspect attributes
dim(FL_counties)
[1] 67 16
Code
colnames(FL_counties)
 [1] "name"             "first_fips"       "objectid_1"       "fdot_distr"      
 [5] "district_string"  "depcode"          "depcodenum"       "d_code"          
 [9] "county"           "fdot_county_code" "global_id"        "creation_date"   
[13] "creator"          "edit_date"        "editor"           "geometry"        
Code
# Base plot
plot(FL_counties, max.plot = 1)

Code
# Mapview
mapview(FL_counties)