Skip to content

Instantly share code, notes, and snippets.

@TonyLadson
Created November 16, 2015 00:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TonyLadson/894c5919b5f56a9c5231 to your computer and use it in GitHub Desktop.
Save TonyLadson/894c5919b5f56a9c5231 to your computer and use it in GitHub Desktop.
Plot a flow duration curve
FDC <- function(Q, xnormal=TRUE, xlab='%time flow equalled or exceeded',
ylab='flow',Qreturn=FALSE, plotFlow=FALSE, add=FALSE, alpha=0.5, ...) {
# Q - flow data (vector of flow values)
# xnormal - logical value to determine if the percentiles should be plotted in a normal (probit) scale
# Qreturn - logical value indicating if quantiles should be returned
# plotFlow - logical value indicating if a time series of flow should be overplotted on flow duration curve
# add - should fdc be added to an existing plot?
# alpha - transparency value for overplotting of flow time series
if(!xnormal){
y <- quantile(Q, probs=seq(0,1,0.01), na.rm=TRUE, type=8)
if(add){
lines(seq(100,0,-1),y,...)
} else {
plot(seq(100,0,-1),y, type='l',xlab=xlab, ylab=ylab,yaxt='n',... )
axis(side=2, at=axTicks(2), labels=prettyNum(axTicks(2), scientific=FALSE))
}
} else {
# make labels
my.labels <- c(1,2,5,seq(10,90,10),95,98,99)
my.z <- qnorm(my.labels/100)
#quantile calculations
my.probs <- seq(0,1,0.01)
y <- quantile(Q, probs=my.probs, na.rm=TRUE, type=8)
# plot
if(add){
lines(qnorm(1-my.probs),y, ...)
} else {
plot(qnorm(1-my.probs),y, xaxt='n',yaxt='n',xlab=xlab, ylab=ylab, type='l',...)
axis(side=1, at=my.z,labels=my.labels)
axis(side=2, at=axTicks(2), labels=prettyNum(axTicks(2), scientific=FALSE))
}
}
if(plotFlow){ # should a time series of data be added to the flow duration curve?
par(new=TRUE)
plot(Q, type='l', col=rgb(0,0,0,alpha=alpha), xaxt='n', ann=FALSE, yaxt='n',...)
}
if(Qreturn) return(y) # return quantiles if requested
invisible()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment