<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/beyond-basic-r-mapping/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://owi.usgs.gov/blog/beyond-basic-mapping/"> The USGS OWI blog </a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Introduction</h2>
<p>There are many different R packages for dealing with spatial data. The main distinctions between them involve the types of data they work with — raster or vector — and the sophistication of the analyses they can do. Raster data can be thought of as pixels, similar to an image, while vector data consists of points, lines, or polygons. Spatial data manipulation can be quite complex, but creating some basic plots can be done with just a few commands. In this post, we will show simple examples of raster and vector spatial data for plotting a watershed and gage locations, and link to some other more complex examples.</p>
<h1>Setting up</h1>
<p>First, let’s download an example shapefile (a polygon) of a HUC8 from western Pennsylvania, using the <code>sbtools</code> package to access <a rel="nofollow" href="https://www.sciencebase.gov/catalog/" target="_blank">ScienceBase</a>. The <code>st_read</code> function from the <code>sf</code> (for “simple features”) package reads in the shapefile. We will be using <code>sf</code> throughout these examples to manipulate the points and polygon for the gages and HUC. Then we’ll retrieve gages with discharge from this watershed using the <code>dataRetrieval</code> package. Both <code>dataRetrieval</code> and <code>sbtools</code> are covered in our <a rel="nofollow" href="https://owi.usgs.gov/R/training-curriculum/usgs-packages/" target="_blank">USGS Packages curriculum</a>.</p>
<pre><code>library(sbtools)
library(dataRetrieval)
library(sf)

item_file_download(sb_id = &quot;5a83025ce4b00f54eb32956b&quot;, 
                   names = &quot;huc8_05010007_example.zip&quot;, 
                   destinations = &quot;huc8_05010007_example.zip&quot;, 
                   overwrite_file = TRUE)
</code></pre>
<pre><code>## [1] &quot;huc8_05010007_example.zip&quot;
</code></pre>
<pre><code>unzip('huc8_05010007_example.zip', overwrite = TRUE)
#note st_read takes the folder, not a particular file
huc_poly &lt;- st_read('huc8_05010007_example')
</code></pre>
<pre><code>## Reading layer `wbdhu8_alb_simp' from data source `D:\LADData\RCode\owi-blog\content\huc8_05010007_example' using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 9 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -79.45512 ymin: 39.91875 xmax: -78.55573 ymax: 40.77377
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
</code></pre>
<pre><code>class(huc_poly)
</code></pre>
<pre><code>## [1] &quot;sf&quot;         &quot;data.frame&quot;
</code></pre>
<pre><code>str(huc_poly)
</code></pre>
<pre><code>## Classes 'sf' and 'data.frame':   1 obs. of  10 variables:
##  $ REGION    : Factor w/ 1 level &quot;Ohio Region&quot;: 1
##  $ SUBREGION : Factor w/ 1 level &quot;Allegheny&quot;: 1
##  $ BASIN     : Factor w/ 1 level &quot;Allegheny&quot;: 1
##  $ SUBBASIN  : Factor w/ 1 level &quot;Conemaugh&quot;: 1
##  $ HUC_2     : Factor w/ 1 level &quot;05&quot;: 1
##  $ HUC_4     : Factor w/ 1 level &quot;0501&quot;: 1
##  $ HUC_6     : Factor w/ 1 level &quot;050100&quot;: 1
##  $ HUC_8     : Factor w/ 1 level &quot;05010007&quot;: 1
##  $ HU_8_STATE: Factor w/ 1 level &quot;PA&quot;: 1
##  $ geometry  :sfc_POLYGON of length 1; first list element: List of 1
##   ..$ : num [1:256, 1:2] -79 -79 -79 -79 -79 ...
##   ..- attr(*, &quot;class&quot;)= chr  &quot;XY&quot; &quot;POLYGON&quot; &quot;sfg&quot;
##  - attr(*, &quot;sf_column&quot;)= chr &quot;geometry&quot;
##  - attr(*, &quot;agr&quot;)= Factor w/ 3 levels &quot;constant&quot;,&quot;aggregate&quot;,..: NA NA NA NA NA NA NA NA NA
##   ..- attr(*, &quot;names&quot;)= chr  &quot;REGION&quot; &quot;SUBREGION&quot; &quot;BASIN&quot; &quot;SUBBASIN&quot; ...
</code></pre>
<pre><code>st_geometry(huc_poly)
</code></pre>
<pre><code>## Geometry set for 1 feature 
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -79.45512 ymin: 39.91875 xmax: -78.55573 ymax: 40.77377
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
</code></pre>
<pre><code>st_bbox(huc_poly)
</code></pre>
<pre><code>##      xmin      ymin      xmax      ymax 
## -79.45512  39.91875 -78.55573  40.77377
</code></pre>
<pre><code>st_crs(huc_poly)
</code></pre>
<pre><code>## Coordinate Reference System:
##   EPSG: 4326 
##   proj4string: &quot;+proj=longlat +datum=WGS84 +no_defs&quot;
</code></pre>
<pre><code>huc_gages &lt;- whatNWISdata(huc = &quot;05010007&quot;, parameterCd = &quot;00060&quot;, service=&quot;uv&quot;)
head(huc_gages)
</code></pre>
<pre><code>##     agency_cd  site_no                                        station_nm
## 197      USGS 03039925 North Fork Bens Creek at North Fork Reservoir, PA
## 591      USGS 03040000                  Stonycreek River at Ferndale, PA
## 621      USGS 03041029                Conemaugh River at Minersville, PA
## 731      USGS 03041500                     Conemaugh River at Seward, PA
## 824      USGS 03042000                  Blacklick Creek at Josephine, PA
## 964      USGS 03042280                  Yellow Creek near Homer City, PA
##     site_tp_cd dec_lat_va dec_long_va coord_acy_cd dec_coord_datum_cd
## 197         ST   40.26619   -79.01669            S              NAD83
## 591         ST   40.28563   -78.92058            S              NAD83
## 621         ST   40.34139   -78.92611            S              NAD83
## 731         ST   40.41924   -79.02614            S              NAD83
## 824         ST   40.47701   -79.18670            S              NAD83
## 964         ST   40.57257   -79.10337            S              NAD83
##       alt_va alt_acy_va alt_datum_cd   huc_cd data_type_cd parm_cd stat_cd
## 197     </code></pre>