In this report, we'll walk through the process of retrieving, preparing, analyzing, and visualizing water use data, using an R Markdown document. R Markdown allows for dynamic report generation. Here, 'dynamic' means that when data get updated or modified, or if there is a new or different visualization or table you want to share with your collaborators, you can easily re-generate the pdf report. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

library(ggplot2)
library(awudsDemo)
library(psych)
library(rgdal)
library(maptools)
#gpclibPermit()
library(plyr)
library(reshape2)
library(ggmap)
library(googleVis)

R Markdown has the option to embed R code, called a 'chunk', like the one shown below in the gray box. You can use chunks to indicate to the reader what you are doing in R. Here's what the chunk included below does: The first line of R code pulls data from the NWIS internal site and loads it into the R environment. The second line renames the data column names. Any text you see in italics and following the hashtag is an R 'comment'- this is text that is not evaluated by R and is intended for human eyes.

# First we'll pull the latest data from AWUDS: 
awudsData<-getAWUDSdump('http://nwis.usgs.gov/awuds/dump/older_files/countydata')

# Next we'll modify the column names using a function in the awudsDemo package
awudsData<-addSummaryColumns(awudsData)
# First we'll pull the latest data from AWUDS: 
awudsData<-getAWUDSdump('http://nwis.usgs.gov/awuds/dump/older_files/countydata')

# Next we'll modify the column names using a function in the awudsDemo package
awudsData<-addSummaryColumns(awudsData)

You can also print data summaries or results in an R Markdown document. Next, we'll print two statistical summaries of the public supply data using the R functions 'summary' and describe', which provide range and quartile information, and some standard univariate statistics:

summary(awudsData$publicSupply)
describe(awudsData$publicSupply)

For the rest of this report, I'm going to 'supress' the R code that is used to generate the figures and maps. However, that code remains editable and shareable.

Plots can be embedded in R Markdown files. Here is an example the R script and output demostrating a custom function we built to display a histogram of public supply for all states in the US, normalized by population:

summaryHistogram(awudsData, 'publicSupply', popNorm=TRUE, truncateLower=1)

Here is a bar chart of irrigation in NJ counties:

# Load subset of data for New Jersey
njAWUDS<-subset(awudsData, USSTATEALPHACODE=='NJ' & YEAR == 2005)

# Bar chart of water use category by county
x=barplot(njAWUDS$irrigation,  ylab="Irrigation by county",xaxt="n")
labs <- njAWUDS$COUNTYNAME
text(cex=1, x=x+0.25, y=-1.25, labs, xpd=TRUE, srt=45, pos=2)

We can can also compare water use categories across all states.

# Box and whiskers of multiple water use categories by state
#par(mfrow=c(5,1))
boxplot(mining~USSTATEALPHACODE, awudsData, ylab='Mining', xlab='State')

Here is a pie chart of water use for NJ:

# Pie chart of water use categories for a given county
i=8
pie(unlist(njAWUDS[i,c("publicSupply", "domestic", "irrigation", "thermoelectricPower", "livestockAndAquaculture", "industrial" )]), main = njAWUDS[i,'COUNTYNAME'])

Next we'll pull in some geospatial data and map it together with water use data on public supply in New Jersey.

Note that the data is being accessed from a ScienceBase Item.

Can also access base maps from google and other sources for mapping.

# Get Spatial Data from ScienceBase and load it up. Original source of shapefile is here: http://publications.newberry.org/ahcbp/downloads/

counties<-getCountyLayer()
counties@data$id <- rownames(counties@data)
summary(counties)
njCounties <- counties[counties$STATE_TERR == "New Jersey",]

njCounties@data$id <- rownames(njCounties@data)
njCountiesMap<-fortify(njCounties,region="id")
njCountiesMap <- join(njCountiesMap, njCounties@data, by="id")
njCountiesAWUDS <- join(njCountiesMap, awudsData, by="FIPS", match="all")

# Choropleth of counties colored by water use category
bounds<-bbox(njCounties)
base_layer<-ggmap(get_map(location = bounds, zoom=8))

map<-base_layer +
      geom_polygon(data = njCountiesAWUDS, aes(long, lat, group=group, fill=publicSupply)) +
      xlab("Longitude") +
      ylab("Latitude") +
      scale_fill_gradient(low="darkgreen",high="green")
map

Getting started

To work with R Markdown:



dblodgett-usgs/awudsDemo documentation built on May 15, 2019, 1:22 a.m.