This is a (minimal) demo of the hydrometric data exercise.
We need dplyr and ggplot2 for this demo.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.2
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(plotly)
## Warning: package 'plotly' was built under R version 4.5.2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
We want to import Daily02GD003, which we already prepared in Excel.
Calculate year with maximum discharge. This is 1937. Was this a significant event (ask Google)?
test1 <- read.csv("Daily02GD003.csv", header = T, na.strings="")
# Check the structure of the data.
glimpse(test1)
## Rows: 50,639
## Columns: 5
## $ ID <chr> "02GD003", "02GD003", "02GD003", "02GD003", "02GD003", "02GD003"…
## $ PARAM <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ Date <chr> "5/12/1915", "5/13/1915", "5/14/1915", "5/15/1915", "5/16/1915",…
## $ Value <dbl> 4.25, 3.96, 3.37, 2.92, 2.92, 2.69, 2.92, 2.92, 2.07, 2.46, 2.58…
## $ SYM <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
# when converting Date from character to POSIXct, pay attention to order of date components, which may differ among users
test2 <- test1 %>%
mutate(PARAM = as.factor(PARAM),
Date = parse_date_time(Date,"mdY"),
yday = yday(Date),
year=year(Date))
# Show year with maximum daily discharge
test2 %>%
filter(PARAM == 1,
Value == max(na.omit(Value)))
## ID PARAM Date Value SYM yday year
## 1 02GD003 1 1937-04-26 583 <NA> 116 1937
Plot water level by day of year. Water level is PARAM = 2.
test3 <- test2 %>%
filter(PARAM == 2)
ggplot(test3,aes(x=yday,y=Value,col=year))+
geom_point(size=1)+
xlab("Day of year")+
ylab("Water level (m)")+
ggtitle("Water level of North Thames River")+
theme_bw()
## Warning: Removed 157 rows containing missing values or values outside the scale range
## (`geom_point()`).
Plot discharge by day of year. Water discharge is PARAM = 1.
test4 <- test2 %>%
filter(PARAM == 1,
Value >= 0)
# use a ggplotly wrapper around ggplot to make an iteractive plot
# hover over the highest value to see the year
ggplotly (ggplot(test4,aes(x=yday,y=Value,col=year))+
geom_point(size=0.5)+
xlab("Day of year")+
ylab("Discharge (m<sup>3</sup>/s)")+
ggtitle("Discharge of North Thames River")+
theme_bw() )
Plot average daily discharge to see the typical hydrograph across the annual cycle. Water discharge is PARAM = 1.
test5 <- test4 %>%
group_by(yday) %>%
summarize(mean = mean(na.omit(Value)),
n = length(na.omit(Value)))
test5
## # A tibble: 366 × 3
## yday mean n
## <dbl> <dbl> <int>
## 1 1 27.6 93
## 2 2 28.4 93
## 3 3 25.2 93
## 4 4 24.2 93
## 5 5 26.8 93
## 6 6 25.3 93
## 7 7 22.1 93
## 8 8 19.9 93
## 9 9 20.6 93
## 10 10 20.9 93
## # ℹ 356 more rows
ggplot(test5,aes(x=yday,y=mean))+
geom_point(size=1)+
xlab("Day of year")+
ylab(expression(paste("Mean discharge (",m^3,"/s)")))+
ggtitle("Discharge of North Thames River")+
theme_bw()
Examine if there is a trend in maximum water discharge for the first peak. There isn’t much of any trend.
test5 <- test4 %>%
mutate(year = year(Date)) %>%
filter(yday > 50 & yday < 150,
year > 1915) %>%
group_by(year) %>%
summarize(max = max(na.omit(Value)))
head(test5)
## # A tibble: 6 × 2
## year max
## <dbl> <dbl>
## 1 1916 181
## 2 1917 334
## 3 1918 213
## 4 1919 215
## 5 1920 147
## 6 1921 218
ggplotly (ggplot(test5,aes(x=year,y=max))+
geom_point(size=1)+
geom_smooth(method="loess")+
xlab("Day of year")+
ylab("Max discharge in spring (m<sup>3</sup>/3)")+
ggtitle("Max Spring Discharge of North Thames River")+
theme_bw() )
## `geom_smooth()` using formula = 'y ~ x'
# note that html tags required for superscripts when using plotly