[R] ggplot2 applying a function based on facet

stephen sefick ssefick at gmail.com
Tue Oct 6 04:45:54 CEST 2009


Sorry, I want the cumsum of precipitation by gauge name.
that can then be used with the appropriate datetime stamp to pot a
cumulative rainfall plot.

On Mon, Oct 5, 2009 at 9:03 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Oct 5, 2009, at 9:39 PM, stephen sefick wrote:
>
>> 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)
>
> #trimmed long comment that broke function
>
>> 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
>
> You want "the cumsum" of <something> but you have wrapped so much up in that
> function (inlcuding library calls???)  that I cannot see what that
> <something> would be. Not all of us read ggplot calls. The canonical route
> to getting cumsums by a factor is with ave. Something like:
>
>  DF$cum_x <- ave(DF$x, DF$fac, cumsum)
>
> --
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
>



-- 
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