This post demonstrates time series analysis for fish passage time at Willamette Falls from 2001 - 2010.
# Read in data and replace NA with 0
passage <- read_csv(here('data/willamette_fish_passage.csv')) %>%
mutate(across(everything(), ~ ifelse(is.na(.), 0, .)))
# Turn passage into time series data
passage_ts <- passage %>%
mutate(Date = lubridate::mdy(Date)) %>%
as_tsibble(key = NULL, index = Date) %>%
janitor::clean_names() %>%
select(date, coho, jack_coho, steelhead)
#Transform the format of passage_ts
passage_ts_clean <- passage_ts %>%
pivot_longer(cols = c(coho, jack_coho, steelhead)) %>%
rename(species = name)
ggplot(data = passage_ts_clean, aes(x = date, y = value)) +
geom_line(aes(color = species)) +
scale_color_manual(values = c("red","blue","green")) +
labs(x = 'Date',
y = 'Counts by Species' ) +
theme_classic()
#create monthly dataset
passage_ts_month <- passage_ts_clean %>%
index_by(yr_mo = ~yearmonth(.)) %>%
group_by(species) %>%
summarize(monthly_species = sum(value))
#Make seasonal plot
gg_season(data = passage_ts_month, y = monthly_species) +
labs(x = 'Month', y = 'Counts for Species') +
theme_classic()
# Create annual dataset
passage_ts_annual <- passage_ts_clean %>%
index_by(yr = ~year(.)) %>%
group_by(species) %>%
summarize(annual_species = sum(value))
# Create line graph
ggplot(data = passage_ts_annual, aes(x = yr, y = annual_species)) +
geom_line(aes(color = species)) +
scale_x_continuous(breaks = c(2001:2010)) +
scale_color_manual(values = c("red","blue","green")) +
labs(x = 'Year',
y = 'Counts by Species' ) +
theme_classic()