Chapter 7 Notes Part 1

Harold Nelson

2023-03-16

Setup

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(terra)
## terra 1.7.3
## 
## Attaching package: 'terra'
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(tmap)

Get Data

Get the land cover data for Washington state downloaded from https://datagateway.nrcs.usda.gov/.

wa_lcd = rast("data/data_2001.tiff")

Look

load("thurston_sf.Rdata")
tm_shape(wa_lcd) +
  tm_raster() +
tm_shape(thurston_sf) +
  tm_borders(lwd = 3, col = "green") +
tm_layout(legend.show = F)
## stars object downsampled to 1039 by 963 cells. See tm_shape manual (argument raster.downsample)
## Warning: Duplicated levels found. They have been omitted

## Crop to Thurston County

thurston_sf = st_transform(thurston_sf,crs(wa_lcd))
thurston_rast = crop(wa_lcd,thurston_sf)
tm_shape(thurston_rast) +
  tm_raster() +
tm_shape(thurston_sf) +
  tm_borders(lwd = 3,col = "green") +
tm_layout(legend.show = F)
## stars object downsampled to 1076 by 929 cells. See tm_shape manual (argument raster.downsample)

Use ggplot2

source("rasterdf.R")
thurston_df <- rasterdf(thurston_rast)

ggplot(data = thurston_df) +
  geom_raster(aes(x = x, 
                  y = y, 
                  fill = value))

Get Land Cover Codes

LCcodes <- unique(thurston_rast)[, 1]
LCcodes
##  [1] 11 21 22 23 24 31 41 42 43 52 71 81 82 90 95

Copy LCnames

Also get the official colors matching these names.

LCnames <-c(
  "Water",
  "DevelopedOpen",
  "DevelopedLow",
  "DevelopedMed",
  "DevelopedHigh",
  "Barren",
  "DeciduousForest",
  "EvergreenForest",
  "MixedForest",
  "ShrubScrub",
  "GrassHerbaceous",
  "PastureHay",
  "CultCrops",
  "WoodyWetlands",
  "EmergentHerbWet")

nlcdcols <- data.frame(coltab(thurston_rast))
nlcdcols <- nlcdcols[LCcodes + 1,]
LCcolors <- rgb(red = nlcdcols$red,
                green = nlcdcols$green,
                blue = nlcdcols$blue,
                names = as.character(LCcodes),
                maxColorValue = 255)
LCcolors
##        11        21        22        23        24        31        41        42 
## "#466B9F" "#DEC5C5" "#D99282" "#EB0000" "#AB0000" "#B3AC9F" "#68AB5F" "#1C5F2C" 
##        43        52        71        81        82        90        95 
## "#B5C58F" "#CCB879" "#DFDFC2" "#DCD939" "#AB6C28" "#B8D9EB" "#6C9FB8"

ggplot2 with Proper Colors

ggplot(data = thurston_df) +
  geom_raster(aes(x = x, 
                  y = y, 
                  fill = as.character(value))) + 
  scale_fill_manual(name = "Land cover",
                    values = LCcolors,
                    labels = LCnames,
                    na.translate = FALSE) +
  coord_sf(expand = FALSE) +
  theme_void()