Load in data
# Let's load in from file the USGS watershed boundaries at Hydrologic Unit Code (HUC) level 4
# Remember drop the provided data in you working directory and/or change the file path to find where the data lives
wb4 <- st_read("C:/Users/qq893/Desktop/data/WBDHU4.shp")
## Reading layer `WBDHU4' from data source `C:\Users\qq893\Desktop\data\WBDHU4.shp' using driver `ESRI Shapefile'
## Simple feature collection with 7 features and 15 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -80.54099 ymin: 36.66942 xmax: -71.78971 ymax: 44.15305
## Geodetic CRS: NAD83
# load HUC8
wb8 <- st_read("C:/Users/qq893/Desktop/data/WBDHU8.shp")
## Reading layer `WBDHU8' from data source `C:\Users\qq893\Desktop\data\WBDHU8.shp' using driver `ESRI Shapefile'
## Simple feature collection with 88 features and 15 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -80.54099 ymin: 36.66942 xmax: -71.78971 ymax: 44.15305
## Geodetic CRS: NAD83
# load National Hydrography Dataset river flowlines (not I already subsetted this to just the Susquehanna Basin). We have to drop some data from this file to clean it up a bit using st_zm()
nhd <- st_read("C:/Users/qq893/Desktop/data/NHD_Flowline_Susq.shp") %>%
st_zm()
## Reading layer `NHD_Flowline_Susq' from data source
## `C:\Users\qq893\Desktop\data\NHD_Flowline_Susq.shp' using driver `ESRI Shapefile'
## Simple feature collection with 30671 features and 131 fields
## Geometry type: LINESTRING
## Dimension: XYZM
## Bounding box: xmin: -78.90935 ymin: 39.54049 xmax: -74.5981 ymax: 42.97751
## z_range: zmin: 0 zmax: 0
## m_range: mmin: 0 mmax: 100
## Geodetic CRS: NAD83
# load in the locations of USGS riverflow gages
gages <- st_read("C:/Users/qq893/Desktop/data/gages02.shp")
## Reading layer `gages02' from data source `C:\Users\qq893\Desktop\data\gages02.shp' using driver `ESRI Shapefile'
## Simple feature collection with 1896 features and 3 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 1384783 ymin: 1720951 xmax: 1928242 ymax: 2675893
## Projected CRS: NAD_1983_Albers
### Let's explore what a shapefile, or sf object in R, looks like.
### Run each line below then inspect the output below this R chunk (or can copy and paste each line into console to print the output in the console)
# Notice the data class is both a dataframe and an sf object. There is a geometry column which holds the coordinates, projection etc, and its a multipolygon
str(wb4)
## Classes 'sf' and 'data.frame': 7 obs. of 16 variables:
## $ OBJECTID : num 1 2 3 4 5 6 7
## $ TNMID : chr "{49F84434-1864-4AE7-9E75-61DEEA2CD019}" "{7ED73B78-B141-47CA-93EE-18EF6D95C252}" "{F646200B-E311-4C3B-B730-7A7A09624D88}" "{6F1A4B05-D4F4-4CCC-B2F6-3145111E2FA8}" ...
## $ MetaSource: chr NA NA NA NA ...
## $ SourceData: chr NA NA NA NA ...
## $ SourceOrig: chr NA NA NA NA ...
## $ SourceFeat: chr NA NA NA NA ...
## $ LoadDate : Date, format: "2012-06-11" "2016-07-29" ...
## $ AreaSqKm : num 15207 38018 32802 18979 71224 ...
## $ AreaAcres : num 3757807 9394497 8105582 4689834 17599782 ...
## $ GNIS_ID : num 0 0 0 0 0 0 0
## $ Name : chr "Upper Chesapeake" "Potomac" "Upper Hudson" "Lower Hudson-Long Island" ...
## $ States : chr "DE,MD,PA" "DC,MD,PA,VA,WV" "MA,NJ,NY,VT" "CT,NJ,NY,RI" ...
## $ HUC4 : chr "0206" "0207" "0202" "0203" ...
## $ Shape_Leng: num 8.37 17.81 18.85 15.69 27.5 ...
## $ Shape_Area: num 1.58 3.96 3.61 2.03 7.65 ...
## $ geometry :sfc_MULTIPOLYGON of length 7; first list element: List of 1
## ..$ :List of 1
## .. ..$ : num [1:13714, 1:2] -75.9 -75.9 -75.9 -75.9 -75.9 ...
## ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
## - attr(*, "sf_column")= chr "geometry"
## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
## ..- attr(*, "names")= chr [1:15] "OBJECTID" "TNMID" "MetaSource" "SourceData" ...
# notice you can index the geometry column and make a plot using base R or ggplot2
plot(wb4$geometry)

# notice if you do not specify the geometry, R will make plots for each attribute
plot(wb4)
## Warning: plotting the first 9 out of 15 attributes; use max.plot = 15 to plot
## all

# We can also use ggplot with the geom_sf()
ggplot() +
geom_sf(data= wb4) +
theme_bw()

# if we want to color the polygons or whatever spatial object type, by some attribute we can use the same ggplot styles
ggplot() +
geom_sf(data= wb4, aes(fill=AreaSqKm)) +
theme_bw()

# we can filter and manipulate sf objects in R just like any other dataframe. Each row typical represents one geometry (1 point, 1 line with 2 ends, 1 polygon) in a multipoint/line/polygon data type
wb4 %>%
filter(AreaSqKm > 60000)
## Simple feature collection with 1 feature and 15 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -78.91438 ymin: 39.53421 xmax: -74.58096 ymax: 42.98733
## Geodetic CRS: NAD83
## OBJECTID TNMID MetaSource SourceData
## 1 5 {0067D865-23F8-458D-A526-A8691E422229} <NA> <NA>
## SourceOrig SourceFeat LoadDate AreaSqKm AreaAcres GNIS_ID Name
## 1 <NA> <NA> 2018-02-01 71223.85 17599782 0 Susquehanna
## States HUC4 Shape_Leng Shape_Area geometry
## 1 MD,NY,PA 0205 27.50398 7.653421 MULTIPOLYGON (((-75.19653 4...
# Check the CRS projection of each file and make sure they are all the same for future analysis
st_crs(wb4)
## Coordinate Reference System:
## User input: NAD83
## wkt:
## GEOGCRS["NAD83",
## DATUM["North American Datum 1983",
## ELLIPSOID["GRS 1980",6378137,298.257222101,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["latitude",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["longitude",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4269]]
st_crs(wb8)
## Coordinate Reference System:
## User input: NAD83
## wkt:
## GEOGCRS["NAD83",
## DATUM["North American Datum 1983",
## ELLIPSOID["GRS 1980",6378137,298.257222101,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["latitude",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["longitude",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4269]]
st_crs(nhd)
## Coordinate Reference System:
## User input: NAD83
## wkt:
## GEOGCRS["NAD83",
## DATUM["North American Datum 1983",
## ELLIPSOID["GRS 1980",6378137,298.257222101,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["latitude",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["longitude",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4269]]
st_crs(gages)
## Coordinate Reference System:
## User input: NAD_1983_Albers
## wkt:
## PROJCRS["NAD_1983_Albers",
## BASEGEOGCRS["NAD83",
## DATUM["North American Datum 1983",
## ELLIPSOID["GRS 1980",6378137,298.257222101,
## LENGTHUNIT["metre",1]],
## ID["EPSG",6269]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["Degree",0.0174532925199433]]],
## CONVERSION["unnamed",
## METHOD["Albers Equal Area",
## ID["EPSG",9822]],
## PARAMETER["Latitude of false origin",23,
## ANGLEUNIT["Degree",0.0174532925199433],
## ID["EPSG",8821]],
## PARAMETER["Longitude of false origin",-96,
## ANGLEUNIT["Degree",0.0174532925199433],
## ID["EPSG",8822]],
## PARAMETER["Latitude of 1st standard parallel",29.5,
## ANGLEUNIT["Degree",0.0174532925199433],
## ID["EPSG",8823]],
## PARAMETER["Latitude of 2nd standard parallel",45.5,
## ANGLEUNIT["Degree",0.0174532925199433],
## ID["EPSG",8824]],
## PARAMETER["Easting at false origin",0,
## LENGTHUNIT["metre",1],
## ID["EPSG",8826]],
## PARAMETER["Northing at false origin",0,
## LENGTHUNIT["metre",1],
## ID["EPSG",8827]]],
## CS[Cartesian,2],
## AXIS["(E)",east,
## ORDER[1],
## LENGTHUNIT["metre",1,
## ID["EPSG",9001]]],
## AXIS["(N)",north,
## ORDER[2],
## LENGTHUNIT["metre",1,
## ID["EPSG",9001]]]]
# One of these does not have the same projection as all the other ones. Which file is it?
# FINISH the code below to reproject the file using the same CRS as the others with the st_transform() function.
gages <- gages %>%
st_transform(4269)
st_crs(gages)
## Coordinate Reference System:
## User input: EPSG:4269
## wkt:
## GEOGCRS["NAD83",
## DATUM["North American Datum 1983",
## ELLIPSOID["GRS 1980",6378137,298.257222101,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["geodetic latitude (Lat)",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["geodetic longitude (Lon)",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## USAGE[
## SCOPE["Geodesy."],
## AREA["North America - onshore and offshore: Canada - Alberta; British Columbia; Manitoba; New Brunswick; Newfoundland and Labrador; Northwest Territories; Nova Scotia; Nunavut; Ontario; Prince Edward Island; Quebec; Saskatchewan; Yukon. Puerto Rico. United States (USA) - Alabama; Alaska; Arizona; Arkansas; California; Colorado; Connecticut; Delaware; Florida; Georgia; Hawaii; Idaho; Illinois; Indiana; Iowa; Kansas; Kentucky; Louisiana; Maine; Maryland; Massachusetts; Michigan; Minnesota; Mississippi; Missouri; Montana; Nebraska; Nevada; New Hampshire; New Jersey; New Mexico; New York; North Carolina; North Dakota; Ohio; Oklahoma; Oregon; Pennsylvania; Rhode Island; South Carolina; South Dakota; Tennessee; Texas; Utah; Vermont; Virginia; Washington; West Virginia; Wisconsin; Wyoming. US Virgin Islands. British Virgin Islands."],
## BBOX[14.92,167.65,86.46,-47.74]],
## ID["EPSG",4269]]