Skip to content

Instantly share code, notes, and snippets.

@boshek
Last active September 4, 2018 20:12
Show Gist options
  • Save boshek/3e86593e6182204d1abcae4be99467bd to your computer and use it in GitHub Desktop.
Save boshek/3e86593e6182204d1abcae4be99467bd to your computer and use it in GitHub Desktop.
library(gganimate)
library(ggplot2)
library(tidyhydat)
library(tidyverse)

Just do it with one station

hy_data <- hy_annual_stats("08MF005") %>% 
  filter(Parameter == "Flow")
ggplot(hy_data, aes(Year, Value, group = Sum_stat)) + 
  geom_line() + 
  geom_segment(aes(xend = 2018, yend = Value), linetype = 2, colour = 'grey') + 
  geom_point(size = 2) + 
  geom_text(aes(x = 2018.1, label = Sum_stat), hjust = 0) + 
  transition_reveal(Sum_stat, Year) + 
  coord_cartesian(clip = 'off') + 
  labs(title = 'Discharge', y = 'Daily Mean Discharge (m^3/s)') + 
  theme_minimal() + 
  theme(plot.margin = margin(5.5, 40, 5.5, 5.5))

Many stations above flow threshold

Any stations that pass 10000 cms

stns <- hy_annual_stats() %>%
  filter(Parameter == "Flow") %>% 
  filter(Value > 10000) %>% 
  pull_station_number() %>%
  hy_stn_data_range() %>% 
  filter(DATA_TYPE == "Q", Year_to >= 2016, Year_from <= 1950) %>% 
  pull_station_number()

stns_names <- hy_stations(stns) %>% 
  select(STATION_NUMBER, STATION_NAME)

big_rivers <- hy_annual_stats(stns) %>% 
  filter(Sum_stat == "MAX", Parameter == "Flow") %>% 
  left_join(stns_names, by = c("STATION_NUMBER"))
ggplot(big_rivers, aes(Year, Value, group = STATION_NAME, colour = STATION_NAME)) + 
  geom_line() + 
  geom_segment(aes(xend = 2018, yend = Value), linetype = 2, colour = 'grey') + 
  geom_point(size = 2) + 
  geom_text(aes(x = 2018.1, label = STATION_NAME), hjust = 0) + 
  transition_reveal(STATION_NAME, Year) + 
  scale_color_viridis_d() +
  coord_cartesian(clip = 'off') + 
  labs(title = 'Hydrometric Records that Exceed 10000 cms at least once a year', y = 'Max Annual Discharge (m^3/s)') + 
  theme_minimal() + 
  guides(colour = FALSE) +
  theme(plot.margin = margin(10.5, 150, 10.5, 20.5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment