ts_ma_plt: Time Series Moving Average Plot

Description Usage Arguments Details Value Author(s) Examples

View source: R/ts_ma_plt.R

Description

This function will produce two plots. Both of these are moving average plots. One of the plots is from xts::plot.xts() and the other a ggplot2 plot. This is done so that the user can choose which type is best for them. The plots are stacked so each graph is on top of the other.

Usage

1
2
3
4
5
6
7
8
9
ts_ma_plt(
  .data,
  .date_col,
  .value_col,
  .ts_frequency = "monthly",
  .main_title = NULL,
  .secondary_title = NULL,
  .tertiary_title = NULL
)

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.

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.

Value

The original time series, the simulated values and a some plots

Author(s)

Steven P. Sanderson II, MPH

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
suppressPackageStartupMessages(library(timetk))
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(tidyquant))
suppressPackageStartupMessages(library(xts))
suppressPackageStartupMessages(library(cowplot))
suppressPackageStartupMessages(library(healthyR.data))

data_tbl <- healthyR_data %>%
  select(visit_end_date_time) %>%
  summarise_by_time(
    .date_var = visit_end_date_time,
    .by       = "month",
    value     = n()
  ) %>%
  set_names("date_col","value")

ts_ma_plt(
  .data = data_tbl,
  .date_col = date_col,
  .value_col = value
)

data_tbl <- healthyR_data %>%
  select(visit_end_date_time) %>%
  summarise_by_time(
    .date_var = visit_end_date_time,
    .by = "week",
    value = n()
  ) %>%
  set_names("date_col","value")

ts_ma_plt(
  .data = data_tbl,
  .date_col = date_col,
  .value_col = value,
  .ts_frequency = "week"
)

spsanderson/LICHospitalR documentation built on Jan. 6, 2022, 12:32 a.m.