lagged_scatter_plot: Lagged Scatter Plot

View source: R/13-lagged_scatter_plot.R

lagged_scatter_plotR Documentation

Lagged Scatter Plot

Description

This function creates a scatter plot of lagged time series data, highlighting the correlation between two variables at different lags. The plot can be a regular scatter plot or a hexbin plot.

Usage

lagged_scatter_plot(
  object,
  x_name = "x",
  y_name = "y",
  which = c("global", "max"),
  hex = FALSE
)

Arguments

object

An S4 object containing time series data and associated information such as global indices, maximum correlation indices, shift times, and correlation values.

x_name

The name to be used for the x-axis label, defaults to "x".

y_name

The name to be used for the y-axis label, defaults to "y".

which

Determines the type of index to use for plotting, either "global" or "max". "global" uses the global index and "max" uses the index of maximum correlation. Defaults to c("global", "max").

hex

A logical value, if TRUE, a hexbin plot will be created, otherwise a regular scatter plot. Defaults to FALSE.

Details

The function calculates the average y-values at specific lags defined by the index chosen (global or max). The scatter plot is then created using ggplot2 and further annotated with correlation information. If hex is TRUE, the plot uses stat_binhex to create hexagonal binning. Otherwise, points are plotted directly with geom_point. A linear model fit is added in both cases using geom_smooth.

Value

A ggplot object representing the lagged scatter plot with appropriate annotations.

Note

The function expects the object to have specific slots: ⁠@global_idx⁠, ⁠@max_idx⁠, ⁠@shift_time⁠, ⁠@global_cor⁠, ⁠@max_cor⁠, ⁠@which_max_idx⁠, ⁠@time1⁠, ⁠@time2⁠, ⁠@x⁠, and ⁠@y⁠. If hex is TRUE, ⁠@which_global_idx⁠ is also used. It is important to ensure these slots are present in the object passed to the function. Also, the function assumes that base_theme is defined elsewhere in the user's environment.

Author(s)

Xiaotao Shen shenxt1990@outlook.com

Examples

data("object", package = "laggedcor")
lagged_scatter_plot(
  object = object,
  x_name = "Step",
  y_name = "HR",
  hex = TRUE,
  which = "max"
)

lagged_scatter_plot(
  object = object,
  x_name = "Step",
  y_name = "HR",
  hex = TRUE,
  which = "global"
)

jaspershen/laggedcor documentation built on Nov. 9, 2023, 3:44 p.m.