Learn R Programming

healthyR.ts (version 0.3.2)

ts_ma_plot: Time Series Moving Average Plot

Description

This function will produce a ggplot2 plot with facet wrapping. The plot contains three moving average panels stacked on top of each other using facet_wrap. The panels show the main time series with moving average, and two difference calculations: Diff A shows sequential period-over-period percentage changes (e.g., month-over-month or week-over-week), and Diff B shows year-over-year percentage changes.

Usage

ts_ma_plot(
  .data,
  .date_col,
  .value_col,
  .ts_frequency = "monthly",
  .main_title = NULL,
  .secondary_title = NULL,
  .tertiary_title = NULL
)

Value

A list containing the ggplot2 plot object and the summary data table.

Arguments

.data

The data you want to visualize. This should be pre-processed and the aggregation should match the .frequency argument.

.date_col

The data column from the .data argument.

.value_col

The value column from the .data argument

.ts_frequency

The frequency of the aggregation, quoted, ie. "monthly", anything else will default to weekly, so it is very important that the data passed to this function be in either a weekly or monthly aggregation.

.main_title

The title of the main plot.

.secondary_title

The title of the second plot.

.tertiary_title

The title of the third plot.

Author

Steven P. Sanderson II, MPH

Details

This function expects to take in a data.frame/tibble. It will return a list object so it is a good idea to save the output to a variable and extract from there.

Examples

Run this code
suppressPackageStartupMessages(library(dplyr))

data_tbl <- ts_to_tbl(AirPassengers) %>%
  select(-index)

output <- ts_ma_plot(
  .data = data_tbl,
  .date_col = date_col,
  .value_col = value
)

output$pgrid
output$data_summary_tbl %>% tail()

output <- ts_ma_plot(
  .data = data_tbl,
  .date_col = date_col,
  .value_col = value,
  .ts_frequency = "month"
)

output$pgrid
output$data_summary_tbl %>% tail()

Run the code above in your browser using DataLab