# Aggregate to daily mean (hourly is too dense to plot directly for a multi-year view)
daily_wide <- temp %>%
  mutate(date = as_date(datetime)) %>%
  group_by(site, date) %>%
  summarise(temp_c = mean(temp_c, na.rm = TRUE), .groups = "drop") %>%
  pivot_wider(names_from = site, values_from = temp_c)   # one column per site

# Convert to xts (dygraphs' required format) — date column becomes the time index
daily_xts <- xts(daily_wide[,-1], order.by = daily_wide$date)

dygraph(daily_xts, main = "Daily Mean Water Temperature (°C) by Site") %>%
  dyRangeSelector() %>%          # draggable zoom slider at the bottom
  dyLegend(show = "follow") %>%  # legend follows cursor, shows values on hover
  dyAxis("y", label = "Temp (°C)") %>%
  dyOptions(colors = RColorBrewer::brewer.pal(7, "Set2"))  # distinct colors per site