2018-11-13-a-few-mapping-tips.utf8.md

Some More Watershed Mapping Tricks with sf and USGS code

Just to make sure that I post something at least annually, I’m jump-starting my posts again. It’s been quite a journey to finish my PhD, and I’m still re-calibrating/re-adjusting. Hopefully I’ll be posting a bit more regularly, instead of annually or semi-annually. For the half-dozen folks that actually read this, if you have additional topics you’d be interested in seeing/reading, please let me know!

But let’s get on to content before this turns into a food blog where the recipe is at the very bottom after 100 pictures of diced tomatoes and artsy grains.

Mapping with sf

About a year ago, I wrote a series of posts on using the sf and riverdist packages (among others) to do some stream analysis and make stream maps. Since that time, I feel like the capabilities and content for spatial work in R has exploded. There have been many excellent examples, and kudos to the folks tweeting, posting, and sharing all the code! If you haven’t seen some of the really fantastic visualizations and data wrangling that the USGS OWI folks have been putting out (along with the code!), you’re missing out. Same goes for many others who have posted some great stuff. Here’s just a few interesting examples:

Main objectives of the post:

Assume all of these will be using sf:

  • How to grab NHD Streamline data using any spatial polygon
  • How to grab waterbodies for a given spatial polgyon
  • How to quickly add these to a interactive map in mapview or plot with ggplot
  • How to modify a polygon (e.g., a species range) with the smoothr package

Packages

The main packages I’ll be using for this blog are as follows:

  • dplyr and ggplot2 for wrangling and plotting
  • cowplot: an amazingly useful package for formatting/manipulating/making multi plots
  • sf: core package for working with spatial data
  • mapview: easy way to make html maps with sf objects
  • smoothr: cool package for simplifying or smoothing polygons in R

Let’s load the packages:

# load libraries
library(dplyr) # data munging and piping
library(ggplot2) # plotting
library(sf) # spatial everything
library(mapview) # html mapping
library(smoothr) # smoothing spatial objects
library(purrr); # wrangling lists and such
library(ggrepel) # nice labels in ggplot
#library(sbtools)
#library(dataRetrieval)

Get a HUC Watershed Boundary

First, let’s use one of the NHD HUC boundaries that we can use as our background for grabbing other data. I’m just using something I have handy, which is HUC8 boundaries for the American/Yuba/Bear watersheds. Alternatively, thanks to the awesome folks over at USGS OWI (David Watkins, David Blodgett, Laura DeCicco, and many more), there’s already plenty of code we can adapt to use to download all sorts of NHD/Watershed things.

For example, we can use some of the code from very handy function courtesy of Laura DeCicco, in a github repository for an inactive package called hydroMap. To make that work, you’ll need a local USGS gaging station ID, as well as rgdal installed. If