[R] ggplot2 applying a function based on facet

stephen sefick ssefick at gmail.com
Tue Oct 6 03:39:48 CEST 2009


Look at the bottom of the message for my question
#here is a little function that I wrote
USGS <- function(input="discharge", days=7){
library(chron)
library(gsubfn)
#021973269 is the Waynesboro Gauge on the Savannah River Proper (SRS)
#02102908 is the Flat Creek Gauge (ftbrfcms)
#02133500 is the Drowning Creek (ftbrbmcm)
#02341800 is the Upatoi Creek Near Columbus (ftbn)
#02342500 is the Uchee Creek Near Fort Mitchell (ftbn)
#02203000 is the Canoochee River Near Claxton (ftst)

a <- "http://waterdata.usgs.gov/nwis/uv?format=rdb&period="
b <- "&site_no=021973269,02102908,02133500,02341800,02342500,02203000"
z <- paste(a, days, b, sep="")
L <- readLines(z)

#look for the data with USGS in front of it (this take advantage of
the agency column)
L.USGS <- grep("^USGS", L, value = TRUE)
DF <- read.table(textConnection(L.USGS), fill = TRUE)
colnames(DF) <- c("agency", "gauge", "date", "time", "gauge_height",
"discharge", "precipitation")
pat <- "^# +USGS +([0-9]+) +(.*)"
L.DD <- grep(pat, L, value = TRUE)
library(gsubfn)
DD <- strapply(L.DD, pat, c, simplify = rbind)
DDdf <- data.frame(gauge = as.numeric(DD[,1]), gauge_name = DD[,2])
both <- merge(DF, DDdf, by = "gauge", all.x = TRUE)

dts <- as.character(both[,"date"])
tms <- as.character(both[,"time"])
date_time <- as.chron(paste(dts, tms), "%Y-%m-%d %H:%M")
DF <- data.frame(date_time, both)
library(ggplot2)
#discharge
if(input=="discharge"){
qplot(as.POSIXct(date_time), discharge, data=DF,
geom="line")+facet_wrap(~gauge_name,
scales="free_y")+coord_trans(y="log10")
}else{
#precipitation

qplot(as.POSIXct(date_time),
precipitation, data=subset(DF, precipitation!="NA"),
geom="line")+facet_wrap(~gauge_name, scales="free_y")
}
}

USGS("precip")

I would like to have the cumsum based on the facet gauge_name - in
other words a cummulative rainfall amount for each gauge_name

-- 
Stephen Sefick

Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods.  We are mammals, and have not exhausted the
annoying little problems of being mammals.

								-K. Mullis




More information about the R-help mailing list