```{r}
pkgset <- '102702'
DEM <- glue::glue("~/data/raw/FIM4/inputs/dems/3dep_dems/10m_5070/20250320/HUC6_{substr(pkgset, 1, 6)}_dem.tif") |> terra::rast()
slope_rad <- terra::terrain(DEM, "slope", unit = "radians")
slope_pct <- tan(slope_rad) * 100
m <- c(0, 5, 10,
5, 20, 20,
20, 40, 30,
40, max(terra::values(slope_pct),na.rm = T), 40)
rclmat <- matrix(m, ncol=3, byrow=TRUE)
slope_rcl <- terra::classify(slope_pct, rclmat, include.lowest=TRUE)
asp <- terra::terrain(DEM, v = "aspect", unit = "degrees")
m <- c(0,22.5,1,
22.5,67.5,2,
67.5,112.5,3,
112.5,157.5,4,
157.5,202.5,5,
202.5,247.5,6,
247.5,292.5,7,
292.5,337.5,8,
337.5,max(terra::values(asp),na.rm = T),1)
rclmat <- matrix(m, ncol=3, byrow=TRUE)
asp_rcl <- terra::classify(asp, rclmat, include.lowest=TRUE)
aspect_slope <- asp_rcl + slope_rcl
aspect_slope_colors <- c(
"11"="#E1E1E1", "12"="#E1E1E1", "13"="#E1E1E1", "14"="#E1E1E1",
"15"="#E1E1E1", "16"="#E1E1E1", "17"="#E1E1E1", "18"="#E1E1E1", "19"="#E1E1E1", "20"="#E1E1E1",
"21"="#ADC0A8", "22"="#ABD0D1", "23"="#B0C8D6", "24"="#B5BDD6", "25"="#B7B1C9", "26"="#C9B1B9", "27"="#D6B8B0", "28"="#D4C4AA",
"31"="#6DA368", "32"="#68B1B6", "33"="#74A1C2", "34"="#7F8DC2", "35"="#8674A6", "36"="#A67484", "37"="#C28674", "38"="#BBA168",
"41"="#24821E", "42"="#1D8E94", "43"="#317AA8", "44"="#435AA8", "45"="#543180", "46"="#80314C", "47"="#A84C31", "48"="#9E7A1D"
)
levels_df <- data.frame(ID = as.numeric(names(aspect_slope_colors)),
category = names(aspect_slope_colors))
# 2. Set the raster as categorical without copying the whole object
levels(aspect_slope) <- levels_df
# 2. Use as.factor() inside the aes() call to fix the discrete vs continuous error
ggplot2::ggplot() +
tidyterra::geom_spatraster(data = aspect_slope) +
ggplot2::scale_fill_manual(
values = aspect_slope_colors,
na.value = "transparent",
guide = "none"
) +
ggplot2::theme_void() +
ggplot2::coord_sf(expand = FALSE)
```