Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Imports
- library(kiwisR)
- library(dplyr)
- library(tidyr)
- library(ggplot2)
- # ---- Configuration ---- #
- # WISKI station_id, WSC staiton number
- station_we_care_about <- "02KF005"
- station_id <- '145374'
- # Start date and WISKI ts_name to gather vals for
- query_start_date <- '2019-01-01'
- query_ts_name <- "Q.15"
- # Parameter name (for threshold table filtering)
- parameter_name <- "Flow"
- # Thresholds csv
- thresh_dat <- read.csv(
- "Y:\\Staff\\Cameron\\PercentilePlotting_CL_RW\\all_active_ont_stn_thresh.csv",
- stringsAsFactors = FALSE
- ) %>%
- # Filter for only the WSC number specified
- # and specified parameter
- filter(
- station_number == station_we_care_about &
- Parameter == parameter_name
- ) %>%
- # Collapse all threshold columns into Thresh Type/Val columns
- gather(
- key = "Threshold",
- value = "thresh_val",
- # Don't collapse these columns
- -c(station_number, Parameter, n)
- )
- # Refactor the threshold type columns for legend
- thresh_dat$Threshold <- factor(
- thresh_dat$Threshold,
- # Levels are the unique entires
- unique(thresh_dat$Threshold),
- # Labels are the clean version
- labels = c(
- "25%",
- "50%",
- "75%",
- "90%",
- "100%"
- )
- )
- # Grabs timeseries metadata for station
- stn_ts <- ki_timeseries_list(
- hub = 'swmc',
- station_id = station_id,
- ts_name = query_ts_name
- )
- # Grab values for specified time series
- stn_values <- ki_timeseries_values(
- hub = 'swmc',
- ts_id = stn_ts$ts_id,
- start_date = query_start_date,
- end_date = Sys.Date()
- )
- # ---- Plot ---- #
- ggplot(stn_values, aes(x = Timestamp)) +
- # Actual values
- geom_line(aes(y = Value)) +
- # Threshold lines
- geom_hline(
- data = thresh_dat,
- aes(yintercept = thresh_val, col = Threshold, lty = Threshold)
- ) +
- # Set threshold line colors
- scale_color_manual(
- values = c("green", "yellow", "orange", "red", "dark red")
- ) +
- # Set x-axis labels/breaks
- scale_x_datetime(
- breaks = "1 month",
- date_labels = "%b"
- ) +
- # Axis label titles
- ylab(
- paste0(
- unique(stn_values$ts_name)[1],
- " (", unique(stn_values$Units)[1], ")"
- )
- ) +
- xlab("Date") +
- # Main plot title
- ggtitle(
- paste0(
- unique(stn_values$station_name)[1],
- " (", station_we_care_about, ")"
- )
- ) +
- # Plot subtitle (number of measurements)
- labs(
- subtitle = paste0(
- "Thresholds calculate from ", thresh_dat$n[1], " values."
- )
- ) +
- theme_minimal()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement