Harold Nelson
2023-03-10
## ── 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()
## terra 1.7.3
##
## Attaching package: 'terra'
##
## The following object is masked from 'package:tidyr':
##
## extract
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
To get elevation data, we’ll use the USDA site. It’s fairly easy to use. I had some problems with Chrome, but Safari worked smoothly.
Go to https://datagateway.nrcs.usda.gov and click on the Get Data Button.
This video will show you how to get elevation data. You’ll need to copy this link to your browser.
The four tif files I downloaded from the USDA site are available in Moodle as T1.tif, etc.
Download all of these and create rasters T1,T2,T3,T4 using the terra package.
Use terra::merge to combine the four rasters into a single raster called Thurston.
Use the methods presented in section 6.1 to examine the characteristics of Thurston.
## class : SpatRaster
## dimensions : 7432, 5168, 1 (nrow, ncol, nlyr)
## resolution : 30, 30 (x, y)
## extent : 422485.1, 577525.1, 5093929, 5316889 (xmin, xmax, ymin, ymax)
## coord. ref. : NAD83 / UTM zone 10N (EPSG:26910)
## source(s) : memory
## name : T1
## min value : -10.39135
## max value : 2543.97705
## Warning: [summary] used a sample
## T1
## Min. : -1.806
## 1st Qu.: 72.064
## Median : 158.066
## Mean : 325.648
## 3rd Qu.: 459.016
## Max. :2451.856
## NA's :2260
## [1] 38408576
Use tmap to see Thurston. Play with the breaks in tm_raster() until you get something you like.
breaks = c(-20,20,50,75,100,200,500,2500)
tm_shape(Thurston) +
tm_raster(breaks = breaks,midpoint = NA)
## stars object downsampled to 834 by 1200 cells. See tm_shape manual (argument raster.downsample)
## Warning: Values have found that are higher than the highest break
Create a histogram of the data.
## Warning: [hist] a sample of3% of the cells was used (of which 2% was NA)
The data has a strong right skew, which can be problematic for graphics. A lot of the data has been forced into a fairly small area to the left in order to accomodate the rare data much farther to the right.
Using a square root transformation can be useful in this situation.
Create a square root transformation of the data in Thurston. Note that this will make values less than or equal to zero NA.
## Warning: [summary] used a sample
## T1
## Min. : 0.000
## 1st Qu.: 8.515
## Median :12.584
## Mean :15.054
## 3rd Qu.:21.446
## Max. :49.516
## NA's :2415
## [1] "SpatRaster"
## attr(,"package")
## [1] "terra"
## Warning: [hist] a sample of3% of the cells was used (of which 2% was NA)
Note that there is more visible detail in the histogram of the transformed data.
Instead of specifying breaks, use style = “cont”.
Do this for both the raw Thurston and the transformed SQThurston.
Compare these two side-by-side using tmap_arrange.
ml = tm_shape(SQThurston) +
tm_raster(style = "cont",
palette = "PuRd") +
tm_layout(legend.show = F,
title = "SQRT")
m = tm_shape(Thurston) +
tm_raster(style = "cont",
palette = "PuRd") +
tm_layout(legend.show = F,
title = "Raw")
tmap_arrange(m,ml)
## stars object downsampled to 834 by 1200 cells. See tm_shape manual (argument raster.downsample)
## stars object downsampled to 834 by 1200 cells. See tm_shape manual (argument raster.downsample)
## stars object downsampled to 834 by 1200 cells. See tm_shape manual (argument raster.downsample)
## stars object downsampled to 834 by 1200 cells. See tm_shape manual (argument raster.downsample)
Load the file “thurston_sf.Rdata”. It contains the boundary data for Thurston County. Use it to overlay the raster data.
load("thurston_sf.Rdata")
tm_shape(SQThurston) +
tm_raster(style = "cont",
palette = "PuRd") +
tm_shape(thurston_sf) +
tm_borders() +
tm_layout(legend.show = F,
title = "SQRT")
## stars object downsampled to 834 by 1200 cells. See tm_shape manual (argument raster.downsample)
Crop the raster data to the boundaries of Thurston County.
thurston_sf = st_transform(thurston_sf,crs(SQThurston))
cropped = crop(SQThurston,thurston_sf)
tm_shape(cropped) +
tm_raster(style = "cont",
palette = "PuRd") +
tm_shape(thurston_sf) +
tm_borders(lwd = 3) +
tm_layout(legend.show = F)
## stars object downsampled to 1261 by 793 cells. See tm_shape manual (argument raster.downsample)
Run all of the data-building code in this section.
# Read the yearly temperature data.
temp2012 <- rast("NLDAS_FORA0125_M.A201207.002.grb.temp.tif")
temp2013 <- rast("NLDAS_FORA0125_M.A201307.002.grb.temp.tif")
temp2014 <- rast("NLDAS_FORA0125_M.A201407.002.grb.temp.tif")
temp2015 <- rast("NLDAS_FORA0125_M.A201507.002.grb.temp.tif")
temp2016 <- rast("NLDAS_FORA0125_M.A201607.002.grb.temp.tif")
temp2017 <- rast("NLDAS_FORA0125_M.A201707.002.grb.temp.tif")
# Build the stack.
tempstack <- c(temp2012, temp2013, temp2014,
temp2015, temp2016, temp2017)
names(tempstack) <- c("July 2012", "July 2013", "July 2014",
"July 2015", "July 2016", "July 2017")
# Convert the data from Kelvin to Celsius.
tempstack <- tempstack - 273.15
Use tmap to display a graphic similar to what the author did with ggplot2.