# lynx and Nile are time.series objects recognized by
# ggpp::ggplot.ts() and converted on-the-fly with a default mapping
# numeric, date times and dates are supported with data frames
# using defaults
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red") +
stat_valleys(colour = "blue")
# using wider window
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red", span = 11) +
stat_valleys(colour = "blue", span = 11)
# global threshold for peak height
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red",
global.threshold = 0.5) # half of data range
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red",
global.threshold = I(1100)) + # data unit
expand_limits(y = c(0, 1500))
# local (within window) threshold for peak height
# narrow peaks at the tip and locally tall
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red",
span = 9,
local.threshold = 0.3,
local.reference = "farthest")
# with narrower window
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red",
span = 5,
local.threshold = 0.25,
local.reference = "farthest")
ggplot(lynx) +
geom_line() +
stat_peaks(colour = "red",
local.threshold = 1/5,
local.reference = "median")
ggplot(Nile) +
geom_line() +
stat_valleys(colour = "blue",
global.threshold = I(700))
# orientation is supported
ggplot(lynx, aes(lynx, time)) +
geom_line(orientation = "y") +
stat_peaks(colour = "red", orientation = "y") +
stat_valleys(colour = "blue", orientation = "y")
# default aesthetic mapping supports additional geoms
ggplot(lynx) +
geom_line() +
stat_peaks(colour = "red") +
stat_peaks(colour = "red", geom = "rug")
ggplot(lynx) +
geom_line() +
stat_peaks(colour = "red") +
stat_peaks(colour = "red", geom = "text", hjust = -0.1, angle = 33)
ggplot(lynx, aes(lynx, time)) +
geom_line(orientation = "y") +
stat_peaks(colour = "red", orientation = "y") +
stat_peaks(colour = "red", orientation = "y",
geom = "text", hjust = -0.1)
# Force conversion of time series time into POSIXct date time
ggplot(lynx, as.numeric = FALSE) +
geom_line() +
stat_peaks(colour = "red") +
stat_peaks(colour = "red",
geom = "text",
hjust = -0.1,
x.label.fmt = "%Y",
angle = 33)
ggplot(Nile, as.numeric = FALSE) +
geom_line() +
stat_peaks(colour = "red") +
stat_peaks(colour = "red",
geom = "text_s",
position = position_nudge_keep(x = 0, y = 60),
hjust = -0.1,
x.label.fmt = "%Y",
angle = 90) +
expand_limits(y = 2000)
ggplot(lynx, as.numeric = FALSE) +
geom_line() +
stat_peaks(colour = "red",
geom = "text_s",
position = position_nudge_to(y = 7600),
arrow = arrow(length = grid::unit(1.5, "mm")),
point.padding = 0.7,
x.label.fmt = "%Y",
angle = 90) +
expand_limits(y = 9000)
Run the code above in your browser using DataLab