Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. # Imports
  2. library(kiwisR)
  3. library(dplyr)
  4. library(tidyr)
  5. library(ggplot2)
  6.  
  7. # ---- Configuration ---- #
  8.  
  9. # WISKI station_id, WSC staiton number
  10. station_we_care_about <- "02KF005"
  11. station_id <- '145374'
  12.  
  13. # Start date and WISKI ts_name to gather vals for
  14. query_start_date <- '2019-01-01'
  15. query_ts_name <- "Q.15"
  16.  
  17. # Parameter name (for threshold table filtering)
  18. parameter_name <- "Flow"
  19.  
  20. # Thresholds csv
  21. thresh_dat <- read.csv(
  22. "Y:\\Staff\\Cameron\\PercentilePlotting_CL_RW\\all_active_ont_stn_thresh.csv",
  23. stringsAsFactors = FALSE
  24. ) %>%
  25. # Filter for only the WSC number specified
  26. # and specified parameter
  27. filter(
  28. station_number == station_we_care_about &
  29. Parameter == parameter_name
  30. ) %>%
  31. # Collapse all threshold columns into Thresh Type/Val columns
  32. gather(
  33. key = "Threshold",
  34. value = "thresh_val",
  35. # Don't collapse these columns
  36. -c(station_number, Parameter, n)
  37. )
  38.  
  39. # Refactor the threshold type columns for legend
  40. thresh_dat$Threshold <- factor(
  41. thresh_dat$Threshold,
  42. # Levels are the unique entires
  43. unique(thresh_dat$Threshold),
  44. # Labels are the clean version
  45. labels = c(
  46. "25%",
  47. "50%",
  48. "75%",
  49. "90%",
  50. "100%"
  51. )
  52. )
  53.  
  54. # Grabs timeseries metadata for station
  55. stn_ts <- ki_timeseries_list(
  56. hub = 'swmc',
  57. station_id = station_id,
  58. ts_name = query_ts_name
  59. )
  60.  
  61. # Grab values for specified time series
  62. stn_values <- ki_timeseries_values(
  63. hub = 'swmc',
  64. ts_id = stn_ts$ts_id,
  65. start_date = query_start_date,
  66. end_date = Sys.Date()
  67. )
  68.  
  69. # ---- Plot ---- #
  70. ggplot(stn_values, aes(x = Timestamp)) +
  71. # Actual values
  72. geom_line(aes(y = Value)) +
  73. # Threshold lines
  74. geom_hline(
  75. data = thresh_dat,
  76. aes(yintercept = thresh_val, col = Threshold, lty = Threshold)
  77. ) +
  78. # Set threshold line colors
  79. scale_color_manual(
  80. values = c("green", "yellow", "orange", "red", "dark red")
  81. ) +
  82. # Set x-axis labels/breaks
  83. scale_x_datetime(
  84. breaks = "1 month",
  85. date_labels = "%b"
  86. ) +
  87. # Axis label titles
  88. ylab(
  89. paste0(
  90. unique(stn_values$ts_name)[1],
  91. " (", unique(stn_values$Units)[1], ")"
  92. )
  93. ) +
  94. xlab("Date") +
  95. # Main plot title
  96. ggtitle(
  97. paste0(
  98. unique(stn_values$station_name)[1],
  99. " (", station_we_care_about, ")"
  100. )
  101. ) +
  102. # Plot subtitle (number of measurements)
  103. labs(
  104. subtitle = paste0(
  105. "Thresholds calculate from ", thresh_dat$n[1], " values."
  106. )
  107. ) +
  108. theme_minimal()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement