Skip to content

Instantly share code, notes, and snippets.

@troyhill
Last active December 17, 2015 03:08
Show Gist options
  • Save troyhill/5540721 to your computer and use it in GitHub Desktop.
Save troyhill/5540721 to your computer and use it in GitHub Desktop.
A function to allow visual examination of the high and low tides found by function HL()
### Important note: I'm no longer maintaining this gist, since this code is incorporated into the
### VulnToolkit package.
### Because it's not being maintained, the code below might not work. The latest, updated version
### is available at https://github.com/troyhill/VulnToolkit/blob/master/R/HL_plot.R
### Function to visually examine high and low tides found by function HL()
### Arguments are the same as for HL()
###
### Arguments:
### - "level" is a numeric vector of water levels.
### - "time" is a vector (numeric or POSIX*) indicating the time of water level measurements. Units must be minutes.
### - "interval" is a single numeric or integer estimate of tidal period (full tidal cycle). Units must be hours.
### - "phantom" (TRUE by default) is a protective measure taken to prevent the inclusion of an artificial
### high or low tide at the end of the dataset. If the water level measurements
### end precisely at a low or high tide, this can be changed to FALSE.
### - "tides" is used to optionally subset the output to include only high or low tides.
### This argument can be "all" (default), "H", or "L"
###
### Function returns a plot of the original water level data and superimposes points representing the high
### and low tides picked out by HL().
HL.plot <- function(level, time, period = 12, phantom = TRUE, tides = "all") {
hl <- HL(level, time, period, phantom, tides)
wll.2 <- data.frame(1:length(level), level, time)
plot(wll.2$level ~ wll.2$time, type = "l", ylab = "water level",
xlab = "", xaxt = "n", col = "darkgray")
points(hl$level[hl$tide == "H"] ~ hl$time[hl$tide == "H"], pch = 19, cex = 0.75, col="red")
points(hl$level[hl$tide == "L"] ~ hl$time[hl$tide == "L"], pch = 19, cex = 0.75, col="cornflowerblue")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment