graph_ma: Function focuses on calculating and plotting the moving...

View source: R/graph_ma.R

graph_maR Documentation

Function focuses on calculating and plotting the moving average of an observed time series.

Description

Function returns either an overlapped or multi-paneled plot of the observed time series, along with the plot for the moving average. The actual moving average values are also returned in a data.frame.

Usage

graph_ma(
  df = NULL,
  time_col = NULL,
  value_col = NULL,
  window_n = 4,
  ma_type = "sma",
  overlap = TRUE,
  title = NULL,
  ma_caption = NULL,
  ob_caption = NULL,
  x_title = NULL,
  y_title = NULL,
  x_limits = NULL,
  x_major_breaks = waiver(),
  x_major_date_breaks = waiver(),
  x_date_labels = waiver(),
  y_limits = NULL,
  y_major_breaks = waiver(),
  show_major_grids = TRUE,
  show_minor_grids = TRUE,
  show_pts = TRUE,
  show_observe = TRUE,
  col_width = 18,
  row_height = 5,
  display_plot = TRUE,
  png_file_path = NULL
)

Arguments

df

A data frame with variables for times and corresponding values.

time_col

Names the column from df for the time values. Values can be numeric or Date/POSIXct.

value_col

Names the value column from df.

window_n

An integer that controls the backward window length of the moving average.

ma_type

A string that sets the type of moving average. Accepted values are "sma" simple, "tri" triangular, "wma" weighted, "exp" exponential, "mod" modified, and "spe" Spencer weighted 15 point average.

overlap

A logical which if TRUE overlaps both the observed and the moving average series'. If FALSE the plots are in separate panels.

title

A string that sets the overall title to the plots.

ma_caption

A string that sets the caption for the ma plot.

ob_caption

A string that sets the caption for the observed plot.

x_title

A string that defines the x axis title.

y_title

A string that defines the y axis title.

x_limits

A Date/POSIXct 2 element vector that sets the minimum and maximum for the x axis. Use NA to refer to the existing minimum and maximum.

x_major_breaks

A Date/POSIXct vector or function that defines the exact major tic locations along the x axis.

x_major_date_breaks

For Date/POSIXct, a string containing the number and date unit for major breaks. Examples: "1 year", "4 sec", "3 month", "2 week".

x_date_labels

For Date/POSIXct, a string containing the format codes for the x axis date format. This can be a strftime format for each x axis tic date label. Examples: "%Y-%m", "%Y/%b/%d", "%H-%M-%S".

y_limits

A numeric 2 element vector that sets the minimum and maximum for the y axis. The default is c(1,10).

y_major_breaks

A numeric vector or function that defines the exact major tic locations for the moving average y axis'.

show_major_grids

A logical that controls the appearance of major grids.

show_minor_grids

A logical that controls the appearance of minor grids.

show_pts

A logical which if FALSE will plot only the lines.

show_observe

A logical that controls the appearance of the observed time series.

col_width

An numeric that sets the width of each plot in centimeters.

row_height

A numeric that sets the height of each plot in centimeters.

display_plot

A logical that if TRUE displays the plot.

png_file_path

A character string with the directory and file name to produce a png image of the plot.

Details

The function uses the RplotterPkg::multi_panel_grid package to draw the multi-paneled plot.

If display_plot is TRUE then the plots will be displayed. If display_plot is FALSE then the function returns a named list that includes a plot object which can be displayed from the console by entering:

  1. grid::grid.newpage()

  2. grid::grid.draw(plot object)

Value

Returning a named list with:

  1. "ma_df" – A data.frame/data.table with column variables for time "DateTime" and the moving average values "Value" and source of the values "Source".

  2. "plots" – A multi-panelled/overlapped TableGrob object plotting the observed series and the moving averages. Use grid::grid.draw(plots) to display the plots.

Author(s)

Rick Dean


deandevl/RtsaPkg documentation built on Oct. 5, 2023, 5:23 p.m.