USGS - science for a changing world

Application - Part 3, plots and maps

In this section, you will use data downloaded from the previous section to create time series plots and a map. The goal is to have 3 timeseries plots per site/HUC (precipitation, nitrogen concentration, and phosphorus concentration), and one summary map of cumulative precipition for each HUC.

This will be put together as a single script at the end, but use the code in Part 5 to remind yourself what we have done up to this point.

Prepare data for plotting

First re-organize the precipitation into a long format: one column for HUC and one for precip, rather than a separate column of precip for each HUC. See gather from the package tidyr for ideas on how to do it.

Choose one site to use to prototype the plots. After we establish the plotting code, we will automate this for all sites. Separate the data so each data frame contains only one site (see ?filter from dplyr for hints).

Create time series plots

Create time series plots of precipitation, nitrogen, and phosphorus for the site you chose using the separated data frame. See ?layout to see how to include all three in one graphics device.

Automate the plots for all sites

Now that we have established the code required to make all three timeseries plots for one site, automate this so it happens for each site (i.e., make a for loop). Save each graphics device as a PNG file: see ?png and add this code to the loop before the plots are rendered (use dev.off() to close each graphics device).

Map site locations

Finally, create a map that shows the locations of each USGS site used. Determine the latitude and longitude for each site, then put them on a map of the US. You can use your own mapping method, or use the map function from the package maps for a simple implementation. Use png() to save the map as a PNG file (use before the map code).

Lindsay R. Carr