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")Working with spatial data in R: Vector
EVR 628- Intro to Environmental Data Science
Exercise 0 - Set up
- Open your
EVR628R project - Install / update the packages as needed using the code below:
- Start a new R script called
vector_data.Rand save it underscripts/02_analysis.R - Add a code header
- Load the
tidyverse,janitor,sf,rnaturalearth,mapview, andEVR628tools
Exercise 1 - Inspecting spatial data
Part 1: FL counties
Post-it up
- Go to the FDOT website and download the county Geopackage file1. Make sure you save them to your
data/rawfolder. - Look at the documentation for
?read_sf - Read the FL counties geopackage and assign it to an object called
FL_counties <- - Immediately after that, pipe into
clean_names()and then again intost_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
- How many columns do we have? How many rows?
- What are the column names?
- Use R’s base
plot()to visualize them. CAREFUL, make sure you specifymax.plot = 1. - Now use
mapviewto 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)