Skip to contents

Plot timeseries data

Usage

draw_xt(
  x,
  y,
  x2 = NULL,
  y2 = NULL,
  which_xy = NULL,
  which_xy2 = NULL,
  shade_bin = NULL,
  shade_interval = NULL,
  shade_col = NULL,
  shade_x = NULL,
  shade_name = "",
  shade_showlegend = FALSE,
  ynames = NULL,
  y2names = NULL,
  xlab = NULL,
  ylab = NULL,
  y2lab = NULL,
  xunits = NULL,
  yunits = NULL,
  y2units = NULL,
  yunits_col = NULL,
  y2units_col = NULL,
  zt = NULL,
  show_zt = TRUE,
  show_zt_every = NULL,
  zt_nticks = 18L,
  main = NULL,
  main_y = 1,
  main_yanchor = "bottom",
  x_nticks = 0,
  y_nticks = 0,
  show_rangeslider = NULL,
  slider_start = NULL,
  slider_end = NULL,
  theme = choose_theme(),
  palette = rtpalette(rtemis_palette),
  font_size = 16,
  yfill = "none",
  y2fill = "none",
  fill_alpha = 0.2,
  yline_width = 2,
  y2line_width = 2,
  x_showspikes = TRUE,
  spike_dash = "solid",
  spike_col = NULL,
  x_spike_thickness = -2,
  tickfont_size = 16,
  x_tickmode = "auto",
  x_tickvals = NULL,
  x_ticktext = NULL,
  x_tickangle = NULL,
  legend_x = 0,
  legend_y = 1.1,
  legend_xanchor = "left",
  legend_yanchor = "top",
  legend_orientation = "h",
  margin = list(l = 75, r = 75, b = 75, t = 75),
  x_standoff = 20L,
  y_standoff = 20L,
  y2_standoff = 20L,
  hovermode = "x",
  displayModeBar = TRUE,
  modeBar_file_format = "svg",
  scrollZoom = TRUE,
  filename = NULL,
  file_width = 960,
  file_height = 500,
  file_scale = 1
)

Arguments

x

Datetime vector or list of vectors.

y

Numeric vector or named list of vectors: y-axis data.

x2

Datetime vector or list of vectors, optional: must be provided if y2 does not correspond to values in x. A single x-axis will be drawn for all values in x and x2.

y2

Numeric vector, optional: If provided, a second y-axis will be added to the right side of the plot.

which_xy

Integer vector: Indices of x and y to plot. If not provided, will select up to the first two x-y traces.

which_xy2

Integer vector: Indices of x2 and y2 to plot. If not provided, will select up to the first two x2-y2 traces.

shade_bin

Integer vector {0, 1}: Time points in x to shade on the plot. For example, if there are 10 time points in x, and you want to shade time points 3 to 7, shade_bin = c(0, 0, 1, 1, 1, 1, 1, 0, 0, 0). Only set shade_bin or shade_interval, not both.

shade_interval

List of numeric vectors: Intervals to shade on the plot. Only set shade_bin or shade_interval, not both.

shade_col

Color: Color to shade intervals.

shade_x

Numeric vector: x-values to use for shading.

shade_name

Character: Name for shaded intervals.

shade_showlegend

Logical: If TRUE, show legend for shaded intervals.

ynames

Character vector, optional: Names for each vector in y.

y2names

Character vector, optional: Names for each vector in y2.

xlab

Character: x-axis label.

ylab

Character: y-axis label.

y2lab

Character: y2-axis label.

xunits

Character: x-axis units.

yunits

Character: y-axis units.

y2units

Character: y2-axis units.

yunits_col

Color for y-axis units.

y2units_col

Color for y2-axis units.

zt

Numeric vector: Zeitgeber time. If provided, will be shown on the x-axis instead of x. To be used only with a single x vector and no x2.

show_zt

Logical: If TRUE, show zt on x-axis, if zt is provided.

show_zt_every

Integer: Show zt every show_zt_every ticks. If NULL, will be calculated to be x_nticks +/- 1 if x_nticks is not 0, otherwise 12 +/- 1.

zt_nticks

Integer: Number of zt ticks to show. Only used if show_zt_every is NULL. The actual number of ticks shown will depend on the periodicity of zt, so that zt = 0 is always included.

main

Character: Main title.

main_y

Numeric: Y position of main title.

main_yanchor

Character: "top", "middle", "bottom".

x_nticks

Integer: Number of ticks on x-axis.

y_nticks

Integer: Number of ticks on y-axis.

show_rangeslider

Logical: If TRUE, show a range slider.

slider_start

Numeric: Start of range slider.

slider_end

Numeric: End of range slider.

theme

Theme object.

palette

Color list: will be used to draw each vector in y and y2, in order.

font_size

Numeric: Font size for text.

yfill

Character: Fill type for y-axis: "none", "tozeroy", "tonexty".

y2fill

Character: Fill type for y2-axis: "none", "tozeroy", "tonexty".

fill_alpha

Numeric: Fill opacity for y-axis.

yline_width

Numeric: Line width for y-axis lines.

y2line_width

Numeric: Line width for y2-axis lines.

x_showspikes

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

spike_dash

Character: Dash type for spikes: "solid", "dot", "dash", "longdash", "dashdot", "longdashdot".

spike_col

Color for spikes.

x_spike_thickness

Numeric: Thickness of spikes. -2 avoids drawing border around spikes.

tickfont_size

Numeric: Font size for tick labels.

x_tickmode

Character: "auto", "linear", "array".

x_tickvals

Numeric vector: Tick positions.

x_ticktext

Character vector: Tick labels.

x_tickangle

Numeric: Angle of tick labels.

legend_x

Numeric: X position of legend.

legend_y

Numeric: Y position of legend.

legend_xanchor

Character: "left", "center", "right".

legend_yanchor

Character: "top", "middle", "bottom".

legend_orientation

Character: "v" for vertical, "h" for horizontal.

margin

Named list with 4 numeric values: "l", "r", "t", "b" for left, right, top, bottom margins.

x_standoff

Numeric: Distance from x-axis to x-axis label.

y_standoff

Numeric: Distance from y-axis to y-axis label.

y2_standoff

Numeric: Distance from y2-axis to y2-axis label.

hovermode

Character: "closest", "x", "x unified".

displayModeBar

Logical: If TRUE, display plotly mode bar.

modeBar_file_format

Character: "png", "svg", "jpeg", "webp", "pdf": file format for mode bar image export.

scrollZoom

Logical: If TRUE, enable zooming by scrolling.

filename

Character: Path to save the plot image.

file_width

Numeric: Width of the saved plot image.

file_height

Numeric: Height of the saved plot image.

file_scale

Numeric: Scale of the saved plot image.

Value

plotly object.

Details

We are switching to palette being a color vector instead of the name of a built-in palette.

Author

EDG

Examples

if (FALSE) { # \dontrun{
datetime <- seq(
  as.POSIXct("2020-01-01 00:00"),
  as.POSIXct("2020-01-02 00:00"),
  by = "hour"
)
df <- data.frame(
  datetime = datetime,
  value1 = rnorm(length(datetime)),
  value2 = rnorm(length(datetime))
)
draw_xt(df, x = df[, 1], y = df[, 2:3])
} # }