USGS-R / smwrGraphs

Moved to: https://code.usgs.gov/water/analysis-tools/smwrGraphs
https://code.usgs.gov/water/analysis-tools/smwrGraphs
Other
18 stars 16 forks source link

Duration plot error #51

Closed katherinejchase closed 4 years ago

katherinejchase commented 4 years ago

I have been following the duration plot demo, and successfully plotting duration hydrographs for several gages. However for certain sites in my current batch of gages, I get this message after the

Create the empty graph lines:

AA.pl <- with(Milk_dv_data, timePlot(Date, Flow, Plot = list(what = "none"), ytitle="Streamflow, in cubic feet per second", yaxis.log=TRUE, xaxis.range=as.Date(c("2018-10-01", "2019-09-30"))))

Error in if (labels.per.cycle > 8) { : missing value where TRUE/FALSE needed In addition: Warning message: In logPretty(drange, hard = hard, labels = axis.labels, ...) : NAs introduced by coercion to integer range

ldecicco-USGS commented 4 years ago

Is the "Milk_dv_data" coming from a dataRetrieval call? If so, could you paste that code so that the issue is reproducible? (ie... so I can get the error on my computer too)

katherinejchase commented 4 years ago

Here is the code. I think the error has something to do with dv=0. When I replace 0 with 0.1 the error doesn't occur.

site <- "06155030"

Get data

Milk <- readNWISdv(site = site, parameterCd = "00060") Milk_dv_data <- renameNWISColumns(Milk)

smwr doesn't work if Q=0, therefore

Milk_dv_data$Flow <- ifelse(Milk_dv_data$Flow == 0, 0.1, Milk_dv_data$Flow)

Construct the daily duration statistics by water year The final output

is a matrix that contains the 6 columns of the selected quantiles.

Milk_dur <- with(Milk_dv_data, tapply(Flow, baseDay(Date, FALSE, year = "water"), FUN = quantile, probs = c(0, .1, .25, .75, .9, 1), na.rm = TRUE))

Milk_dur <- do.call(rbind, Milk_dur)

Set up the page, for this demo, set up room for explanation at bottom

setGD("Milk") AA.lo <- setLayout(explanation=list(bottom=1.5)) setGraph(1, AA.lo)

Create the empty graph

AA.pl <- with(Milk_dv_data, timePlot(Date, Flow, Plot = list(what = "none"), ytitle="Streamflow, in cubic feet per second", yaxis.log=TRUE, xaxis.range=as.Date(c("2018-10-01", "2019-09-30"))))

ldecicco-USGS commented 4 years ago

So it looks like you are doing a log scale, and the smwr function can't handle taking the log of 0. Some packages like ggplot2 will remove that data and spit out a message, but I don't think it's unreasonable to have an error when trying to do a log scale with zero's. I think you could choose between setting your 0's to something very small (like you mentioned), or removing the rows with 0 flow. Not sure if a linear scale is on the table for your application, but that might work too.

katherinejchase commented 4 years ago

Thank you for looking into this! The error message itself is hard to relate to the data including 0's. I appreciate your help.