Continuous Water Quality Station Maps

Author

Dave Bosworth

Published

December 9, 2025

Code
# Load packages
library(tidyverse)
library(viridisLite)
library(khroma)
library(sf)
library(mapview)
library(DT)
library(here)
library(conflicted)

# Declare package conflict preferences
conflicts_prefer(dplyr::filter())

Import and Prepare Data

Code
# Import continuous WQ station coordinates
sf_stations <- read_sf(here("data/processed/spatial/cont_wq_stations.shp"))

# Import continuous WQ station metadata
df_metadata <- readRDS(here("data/processed/wq/cont_wq_metadata.rds"))

# Add metadata to station coordinates
sf_stations_c1 <- sf_stations %>%
  left_join(df_metadata, by = join_by(Source, Station_ID)) %>%
  relocate(Station_Name, .after = Station_ID) %>%
  mutate(across(where(is.POSIXct), \(x) format(x, "%Y-%m-%d")))

# Import regions shapefile
sf_regions <- read_sf(here("data/processed/spatial/delta_regions.shp"))

# Create color palettes for station points (using Source) and region polygons
pal_stations <- plasma(n = length(unique(sf_stations_c1$Source)))
pal_regions <- color("muted")(length(unique(sf_regions$Region)))

Interactive Maps

Code
ndf_maps <- sf_stations_c1 %>%
  nest(sf_data = everything(), .by = Parameter) %>%
  arrange(Parameter) %>%
  mutate(
    maps = map(
      sf_data,
      \(x) {
        mapview(
          x,
          zcol = "Source",
          col.regions = pal_stations,
          label = "Station_Name",
          layer.name = "Source",
          cex = 5
        ) +
          mapview(
            sf_regions,
            zcol = "Region",
            col.regions = pal_regions,
            layer.name = "Region",
            alpha.regions = 0.4
          )
      }
    )
  )

Station Metadata

Code
sf_stations_c1 %>%
  st_drop_geometry() %>%
  select(-c(Station_ID, SubRegion, CDEC_ID)) %>%
  mutate(
    across(c(Start, End), ymd),
    across(c(Source, Region, Parameter, Interval), factor)
  ) %>%
  datatable(filter = "top")