# Plotting spells

In hydrology, spells analysis is a way of looking at periods that flows are above or below chosen thresholds.  It was developed by the UK Institute of Hydrology and is covered in hydrology textbooks such as Gordon et al. (2004, section 8.4).  The free hydrology tool, aquapak can be used to undertake spells analysis. Here I’m just looking at the simplest approach to spells analysis which is to plot high and low flow periods.  Example plots of high and low spells for Joyces Creek at Strathlea (Gauge 407230)  are shown below.  Flow data are available here.

Joyces Creek at Strathlea, flow > 10 ML/d

Joyces Creek at Strathlea, flow < 0.1 ML/d

These graphs are based on a tile plot in the ggplot2 package in R.  For the low flow spells, I’ve changed the left hand side of the graph to start in September so the austral summer low flow period (December to March) occurs in the middle of the graph.

Code below and at this gist.

```library(ggplot)
library(grid)
library(devtools)
library(dplyr)
library(lubridate)

# Data is stored in a data frame called 'joyce' with
# columns for the date, flow and quality code.
# You will need to provide your own data.

joyce
# Source: local data frame [19,025 x 3]
#
# Datetime flow QC
# 1 1963-05-14 1.001 1
# 2 1963-05-15 61.000 1
# 3 1963-05-16 32.000 1
# 4 1963-05-17 34.000 1
# 5 1963-05-18 19.000 1
# 6 1963-05-19 19.000 1
# 7 1963-05-20 12.000 1
# 8 1963-05-21 8.000 1
# 9 1963-05-22 9.000 1
# 10 1963-05-23 13.000 1
# .. ... ... ..

str(joyce)

# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 19025 obs. of 3 variables:
# \$ Datetime: POSIXct, format: "1963-05-14" "1963-05-15" "1963-05-16" "1963-05-17" ...
# \$ flow : num 1 61 32 34 19 ...
# \$ QC : int 1 1 1 1 1 1 1 1 1 1 ...

# High flow spell plot

# Calculate day numbers for the start of each month
xdate <- seq(as.Date('2010-01-01'),as.Date('2010-12-31'), by='months')
month.start <- as.numeric(format(xdate,'%j'))

joyce %>%
mutate(high = ifelse(flow > 10, 1, 0)) %>% # We are interested in flows above 10
mutate(high = ifelse(is.na(high), -1, high)) %>% # Specifically code missing values
mutate(my.year = year(Datetime)) %>% # year
mutate(my.day = yday(Datetime) ) %>% # day of the year
ggplot(aes(x = my.day, y = my.year)) +
geom_tile(aes(fill = factor(high, levels = c(1,0,-1) ))) +
scale_fill_manual(name = "High flows", values=c('dark blue', 'light blue','red'), labels = c(' > 10 ML/d', ' < 10 ML/d', ' missing')) +
scale_x_continuous(name="Month", breaks=month.start, labels=month.lab) +
scale_y_continuous(name="Year", breaks=1962:2015, labels=1962:2015) +
theme(axis.text.y = element_text(colour="grey20",size=9)) # small text for year to include all years

# Low flow spell plot
# x-axes starts in September

xdate <- seq(as.Date('2010-01-01'),as.Date('2010-12-31'), by='months')
month.start <- as.numeric(format(xdate,'%j')) + 120
month.start <- ifelse(month.start >365, month.start - 365, month.start)

joyce %>%
mutate(low = ifelse(flow < 0.1, 1, 0)) %>% # flows below 0.1
mutate(low = ifelse(is.na(low), -1, low)) %>% # Specifically code missing values
mutate(my.year = year(Datetime)) %>%
mutate(my.day = yday(Datetime) + 120) %>%
mutate(my.year = ifelse(my.day > 365, my.year + 1, my.year)) %>%
mutate(my.day = ifelse(my.day > 365, my.day - 365, my.day)) %>%
ggplot(aes(x = my.day, y = my.year)) +
geom_tile(aes(fill = factor(low, levels = c(1, 0, -1)))) +
scale_fill_manual(name = "Low flows", values=c('light blue', 'dark blue', 'red'), labels = c(' < 0.1 ML/d', ' > 0.1 ML/d', ' Missing')) +
scale_x_continuous(name="Month", breaks=month.start, labels=month.lab) +
scale_y_continuous(name="Year", breaks=1962:2015, labels=1962:2015) +