library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
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)
library(plotly)
##
## 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
All of the required packages are loaded. Lubridate is used to work with dates, dplyr is used for data manipulation, ggplot2 is used to create plots, and plotly is used to make an interactive ggplot.
hydrometric <- read.csv("daily_20261007T1920.csv")
The observations, variable names, and structure of the dataset are examined to confirm the data were imported correctly.
head(hydrometric)
## X.ID PARAM Date Value SYM
## 1 02GD003 1 12/05/1915 4.25
## 2 02GD003 1 13/05/1915 3.96
## 3 02GD003 1 14/05/1915 3.37
## 4 02GD003 1 15/05/1915 2.92
## 5 02GD003 1 16/05/1915 2.92
## 6 02GD003 1 17/05/1915 2.69
names(hydrometric)
## [1] "X.ID" "PARAM" "Date" "Value" "SYM"
str(hydrometric)
## 'data.frame': 50639 obs. of 5 variables:
## $ X.ID : chr "02GD003" "02GD003" "02GD003" "02GD003" ...
## $ PARAM: int 1 1 1 1 1 1 1 1 1 1 ...
## $ Date : chr "12/05/1915" "13/05/1915" "14/05/1915" "15/05/1915" ...
## $ Value: num 4.25 3.96 3.37 2.92 2.92 2.69 2.92 2.92 2.07 2.46 ...
## $ SYM : chr "" "" "" "" ...
Dataset contains 50,639 observations and five variables. Date was imported as a character variable, so it must be converted into a date format before date-based analyses can be performed.
The original Date variable is stored as text. The lubridate parse_date_time() function converts it into a date-time object using day-month-year format.
df <- hydrometric %>%
mutate(DateFormatted = parse_date_time(Date, orders = "dmy"))
yday function extracts the day of the year from each date. This creates a value from 1 to 366 that allows observations from different years to be compared on the same annual time scale.
df <- df %>%
mutate(DayOfYear = yday(DateFormatted))
head(df)
## X.ID PARAM Date Value SYM DateFormatted DayOfYear
## 1 02GD003 1 12/05/1915 4.25 1915-05-12 132
## 2 02GD003 1 13/05/1915 3.96 1915-05-13 133
## 3 02GD003 1 14/05/1915 3.37 1915-05-14 134
## 4 02GD003 1 15/05/1915 2.92 1915-05-15 135
## 5 02GD003 1 16/05/1915 2.92 1915-05-16 136
## 6 02GD003 1 17/05/1915 2.69 1915-05-17 137
DayOfYear variable created.
The calendar year is extracted from DateFormatted using the year() function.
df <- df %>%
mutate(Year = year(DateFormatted))
head(df)
## X.ID PARAM Date Value SYM DateFormatted DayOfYear Year
## 1 02GD003 1 12/05/1915 4.25 1915-05-12 132 1915
## 2 02GD003 1 13/05/1915 3.96 1915-05-13 133 1915
## 3 02GD003 1 14/05/1915 3.37 1915-05-14 134 1915
## 4 02GD003 1 15/05/1915 2.92 1915-05-15 135 1915
## 5 02GD003 1 16/05/1915 2.92 1915-05-16 136 1915
## 6 02GD003 1 17/05/1915 2.69 1915-05-17 137 1915
Year variable created.
To identify the year with greatest daily discharge, data are first restricted to discharge observations and missing values removed.
max_discharge <- df %>%
filter(PARAM == 1, !is.na(Value)) %>%
group_by(Year) %>%
summarise(MaxDischarge = max(Value, na.rm = TRUE)) %>%
arrange(desc(MaxDischarge))
max_discharge
## # A tibble: 97 × 2
## Year MaxDischarge
## <dbl> <dbl>
## 1 1937 583
## 2 1977 558
## 3 1963 532
## 4 2018 488
## 5 1930 476
## 6 1929 467
## 7 1940 459
## 8 2008 450
## 9 1997 444
## 10 1982 434
## # ℹ 87 more rows
Largest recorded daily discharge occured in 1937, with maximum value at 583.
Water-level observations are plotted against day of year, with each year represented on a seperate line. Alpha value of 0.3 makes the lines transparent so overlapping years can still be visualized.
df %>%
filter(PARAM == 2, !is.na(Value)) %>%
ggplot(aes(x = DayOfYear, y = Value, group = Year)) +
geom_line(alpha = 0.3) +
labs(
x = "Day Of Year",
y = "Water Level",
title = "Water Level by Day of Year"
)
Water levels vary among years, with greater variation during the earlier
part of the year.
Discharges obs. are selected using PARAM == 1 and plotted against day of year.
df %>%
filter(PARAM == 1, !is.na(Value)) %>%
ggplot(aes(x = DayOfYear, y = Value, group = Year)) +
geom_line(alpha = 0.3) +
labs(
x = "Day Of Year",
y = "Discharge",
title = "Discharge by Day of Year"
)
Discharge shows variation among years. Largest events occured during
days 50-120, while discharge is low in the middle of the year.
average_discharge <- df %>%
filter(PARAM == 1, !is.na(Value)) %>%
group_by(DayOfYear) %>%
summarise(
MeanDischarge = mean(Value)
)
hydrograph_plot <- ggplot(
average_discharge,
aes(x = DayOfYear, y = MeanDischarge)
) +
geom_line() +
labs(
x = "Day of Year",
y = "Average Daily Discharge",
title = "Typical Annual Hydrograph"
)
ggplotly(hydrograph_plot)
Typical annual hydrograph shows increase in discharge around day 50, with largest average discharge occuring around days 80-100.
first_peak <- df %>%
filter(
PARAM == 1,
DayOfYear >= 50,
DayOfYear <= 120,
!is.na(Value)
) %>%
group_by(Year) %>%
summarise(
MaxDischarge = max(Value)
)
ggplot(first_peak, aes(x = Year, y = MaxDischarge)) +
geom_point() +
labs(
x = "Year",
y = "Maximum Daily Discharge",
title = "Trend in Maximum Discharge of First Hydrograph Peak"
)
Maximum discharge during first hydrograph peak varies among years,
several years have unusually high peak discharges, there does not appear
to be a simple increase through time based on visual inspection
alone.