Skip to content

Instantly share code, notes, and snippets.

@boshek
Created May 1, 2019 16:27
Show Gist options
  • Save boshek/3ba99447a7722d585d107891a091c271 to your computer and use it in GitHub Desktop.
Save boshek/3ba99447a7722d585d107891a091c271 to your computer and use it in GitHub Desktop.
# Copyright 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
library(tidyhydat)
library(dplyr)
library(ggplot2)
library(lubridate)
duncan_level <- hy_daily_levels("08NH127")
# Variables ---------------------------------------------------------------
year_of_interest <- 2018
## A bit of hack. Convert Date to an integer, group_by then convert back to dates all in 2018
duncan_level_average <- duncan_level %>%
mutate(day_of_year = yday(Date)) %>%
group_by(day_of_year) %>%
summarise(`Daily Average` = mean(Value, na.rm=TRUE), `Daily SD` = sd(Value, na.rm = TRUE)) %>%
mutate(Date = as.Date(day_of_year, origin = "2017-12-31"))
## Isolate the year of interest
duncan_level_year_of_interest <- duncan_level %>%
filter(year(Date) == year_of_interest)
## Plot it
ggplot(duncan_level_average, aes(x = Date, y = `Daily Average`)) +
geom_line() +
geom_ribbon(aes(ymax = `Daily Average` + `Daily SD`, ymin = `Daily Average` - `Daily SD`), alpha = 0.1) +
geom_line(data = duncan_level_year_of_interest, aes(y = Value), colour = "red") +
theme_minimal() +
scale_x_date(date_labels = "%b") +
scale_colour_discrete(name = "") +
labs(x = "Date", y = "Elevation (m)", #title = "Duncan Reservoir Elevation (#08NH127)",
caption ="Data source: Water Survey of Canada")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment