horizon: Horizon chart

Description Usage Arguments Details Examples

View source: R/horizon.R

Description

Horizon chart of multiple time series

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
horizon(
  dates,
  df,
  date_format = "%Y-%m-%d",
  digits = NULL,
  width = NULL,
  height = NULL,
  axis_height = 30,
  axis_ticks = 4,
  padding = 15,
  colors = NULL,
  tick_format = NULL,
  focus_format = NULL
)

Arguments

dates

Vector of dates as character strings (e.g., as "YYYY-MM-DD"). They need to be equally-spaced. (And actually, on the first, second, and last values are actually used: first and second define gap, and first and last define the time axes.

df

Data frame with rows = dates, columns = values to plot

date_format

A character string representing the format of dates. See D3 v3 Time Formatting for the specifier strings (such as %m for decimal month).

digits

Number of digits to show when hovering.

width

width in pixels

height

height in pixels

axis_height

height of axis in pixels

axis_ticks

Number of ticks on axes

padding

Padding around figure

colors

A vector of character strings with RGB colors (like "#rrggbb"). Must have an event number of elements. length(colors)/2 determines the number of bands; the first half are the colors used for the negative bands, and the second half are the colors for the positive bands.

tick_format

A character string representing the format for date/times in the top and bottom axes. See D3 v3 Time Formatting for the specifier strings (such as %m for decimal month).

focus_format

A character string representing the format for date/time at the focus line. See D3 v3 Time Formatting for the specifier strings (such as %m for decimal month).

Details

The input dates need to be converted from character strings to JSON dates.

The argument date_format is used for this. It's a character string like "%Y-%m-%d" that is used to parse the dates. Here are some of the available codes. (For a complete list, see the d3.js documentation.)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# data included with package
file <- system.file("extdata", "test.csv", package="horizon")
x <- read.csv(file)
horizon(x[,1], x[,-1])

# second example with simulated data
n <- 600  # number of time points
p <- 26   # number of time series

# construct sequence of dates
dates <- as.character( seq( lubridate::ymd("1969-12-20"), by=7, length=n) )

# simulate brownian motion, all starting at 0
y <- matrix(0, nrow=n, ncol=p)
y[-1,] <- rnorm((n-1)*p, 0, 0.25)
y <- apply(y, 2, cumsum)
colnames(y) <- letters[1:p]

# make the horizon plot
horizon(dates, y)

kbroman/horizon documentation built on Jan. 17, 2021, 4:15 p.m.