Skip to contents

Draw interactive timeseries plots using plotly

Usage

draw_ts(
  x,
  time,
  window = 7L,
  group = NULL,
  roll_fn = c("mean", "median", "max", "none"),
  roll_col = NULL,
  roll_alpha = 1,
  roll_lwd = 2,
  roll_name = NULL,
  alpha = NULL,
  align = "center",
  group_names = NULL,
  xlab = "Time",
  n_xticks = 12,
  scatter_type = "scatter",
  legend = TRUE,
  x_showspikes = TRUE,
  y_showspikes = FALSE,
  spikedash = "solid",
  spikemode = "across",
  spikesnap = "hovered data",
  spikecolor = NULL,
  spikethickness = 1,
  displayModeBar = TRUE,
  modeBar_file_format = "svg",
  theme = rtemis_theme,
  palette = rtemis_palette,
  filename = NULL,
  file_width = 500,
  file_height = 500,
  file_scale = 1,
  ...
)

Arguments

x

Numeric vector of values to plot or list of vectors

time

Numeric or Date vector of time corresponding to values of x

window

Integer: apply roll_fn over this many units of time

group

Factor defining groups

roll_fn

Character: "mean", "median", "max", or "sum": Function to apply on rolling windows of x

roll_col

Color for rolling line

roll_alpha

Numeric: transparency for rolling line

roll_lwd

Numeric: width of rolling line

roll_name

Rolling function name (for annotation)

alpha

Numeric [0, 1]: Transparency

align

Character: "center", "right", or "left"

group_names

Character vector of group names

xlab

Character: x-axis label

n_xticks

Integer: number of x-axis ticks to use (approximately)

scatter_type

Character: "scatter" or "lines"

legend

Logical: If TRUE, show legend

x_showspikes

Logical: If TRUE, show x-axis spikes on hover

y_showspikes

Logical: If TRUE, show y-axis spikes on hover

spikedash

Character: dash type string ("solid", "dot", "dash", "longdash", "dashdot", or "longdashdot") or a dash length list in px (eg "5px,10px,2px,2px")

spikemode

Character: If "toaxis", spike line is drawn from the data point to the axis the series is plotted on. If "across", the line is drawn across the entire plot area, and supercedes "toaxis". If "marker", then a marker dot is drawn on the axis the series is plotted on

spikesnap

Character: "data", "cursor", "hovered data". Determines whether spikelines are stuck to the cursor or to the closest datapoints.

spikecolor

Color for spike lines

spikethickness

Numeric: spike line thickness

displayModeBar

Logical: If TRUE, display plotly's modebar

modeBar_file_format

Character: modeBar image export file format

theme

Character: theme name or list of theme parameters

palette

Character: palette name, or list of colors

filename

Character: Path to filename to save plot

file_width

Numeric: image export width

file_height

Numeric: image export height

file_scale

Numeric: image export scale

...

Additional arguments to be passed to draw_scatter

Author

EDG

Examples

if (FALSE) { # \dontrun{
time <- sample(seq(as.Date("2020-03-01"), as.Date("2020-09-23"), length.out = 140))
x1 <- rnorm(140)
x2 <- rnorm(140, 1, 1.2)
# Single timeseries
draw_ts(x1, time)
# Multiple timeseries input as list
draw_ts(list(Alpha = x1, Beta = x2), time)
# Multiple timeseries grouped by group, different lengths
time1 <- sample(seq(as.Date("2020-03-01"), as.Date("2020-07-23"), length.out = 100))
time2 <- sample(seq(as.Date("2020-05-01"), as.Date("2020-09-23"), length.out = 140))
time <- c(time1, time2)
x <- c(rnorm(100), rnorm(140, 1, 1.5))
group <- c(rep("Alpha", 100), rep("Beta", 140))
draw_ts(x, time, 7, group)
} # }