Example using US state boundaries and NCEP-NARR 2m Air Temperature data
library(terra)
library(tidyterra)
library(dplyr)
library(ggplot2)
# import state boundaries from tigris
us <- tigris::states(cb = FALSE, year = 2022)
# convert to SpatVector
us <- terra::vect(us)
# import NCEP-NARR 2m air temperature
air <- terra::rast(
"/Volumes/manwareme/EnviroTools_shiny_app/input/air.2m.2023.nc"
)
# select only 2 layers for example
air_subset <- c(air$air_1, air$air_2)
# uniform crs
us <- terra::project(us,terra::crs(air))
#### zonal statistics using dplyr syntax
#### example flow for function building
us_air <-
air_subset %>%
zonal(us, fun = "mean") %>%
mutate(NAME = paste(us$NAME)) %>%
terra::merge(x = us, by.x = "NAME", by.y = "NAME")
# plot state average 2m temperature for January 1, 2023
ggplot()+
geom_spatvector(data = us_air,
aes(fill = air_1)) +
scale_fill_continuous(type = "viridis") +
theme_bw()
Script flow:
calculates mean within each vector zone (state in this example)
creates new column with vector zone identifier (state name in this example)
merges calculated values back onto the original vector dataset
Remerging is required because the terra::zonal() function returns a geometry-less data frame
Can easily be applied to other census boundaries using the tigris package, which is a package of data-download functions for census-defined boundaries
Transfer from CHORDS data analysis:
Example using US state boundaries and NCEP-NARR 2m Air Temperature data
Script flow:
terra::zonal()
function returns a geometry-less data frameCan easily be applied to other census boundaries using the
tigris
package, which is a package of data-download functions for census-defined boundaries