add_polygon: Add Polygon to Plot

Description Usage Arguments Author(s) Examples

Description

Helper function to plot a filled polygon based on a zoo time series object with nice missing data handling.

Usage

1
add_polygon(x, col = "#ff0000", lower.limit = 0, lwd = 1)

Arguments

x

an univariate zoo time series object.

col

character, a hex color. Default is "#ff0000" (red).

lower.limit

numeric, the lower limit used to plot the polygon. default is 0.

lwd

line width argument.

Author(s)

Reto Stauffer

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
library("zoo")
# Create a time series object
set.seed(3)
a <- zoo(sin(1:100/80*pi) + 3 + rnorm(100, 0, 0.3), 201:300)

# Plot
par(mfrow = c(1,3))
plot(a, type = "n", main = "Demo Plot 1",
     ylim = c(-1, max(a)+1), xaxs = "i", yaxs = "i")
add_polygon(a)

# Specify lower.limit to -1 (lower limit of our ylim),
# add different color, change line style.
plot(a, type = "n", main = "Demo Plot 2",
     ylim = c(-1, max(a)+.2), xaxs = "i", yaxs = "i")
add_polygon(a, col = "#00CCBB", lower.limit = -1, lwd = 3)

# Using an "upper limit".
plot(a, type = "n", main = "Demo Plot 3",
     ylim = c(-1, max(a)+.2), xaxs = "i", yaxs = "i")
add_polygon(a, col = "#00BBFF", lower.limit = par()$usr[4L])

# Make a copy and add some missing values
b <- a
b[2:10]  <- NA
b[50:55] <- NA
b[70]    <- NA

# Plot
par(mfrow = c(1,1))

# Same as "Demo Plot 2" with the time series which
# contains missing values (b).
plot(b, type = "n", main = "Demo Plot 2 With Missing Values",
     ylim = c(-1, max(b, na.rm = TRUE)+.2), xaxs = "i", yaxs = "i")
add_polygon(b, col = "#00CCBB", lower.limit = -1, lwd = 3)

retostauffer/Rmosmix documentation built on May 22, 2019, 2:45 p.m.