Learn R Programming

staggR (version 0.1.1)

ts_plot: Generates time-series plots, optionally faceted by groups

Description

Generates time-series plots, optionally faceted by specified groups. The resulting object can be customized using ggplot2 functions and themes.

Usage

ts_plot(
  formula = NULL,
  y = NULL,
  group = NULL,
  time_var = NULL,
  intervention_var = NULL,
  df,
  tsi = NULL,
  weights = NULL,
  ncol = 2
)

Value

Returns an object of class "ggplot"

Arguments

formula

An object of class formula (or one that can be coerced to that class): a symbolic description of the model to be fitted. The details of model specification are given under 'Details'.

y

Name of the variable in df that contains the outcome of interest. If NULL, this is assumed to be the column named in the left-hand side of formula.

group

Name of the variable in df that contains cohort assignments or other groups by which the plot should be faceted. If NULL, this is assumed to be the first column named in the right-hand side of formula. If no formula is specified, the resulting plot will aggregate all results into a single panel.

time_var

Name of the variable in df that contains time periods. If NULL, this is assumed to be the second column named in the right-hand side of formula.

intervention_var

Name of the cohort-level variable in df that specifies which values in time_var correspond to the first post-intervention time period for each cohort. If NULL, vertical lines indicating the intervention period will be omitted from the plot.

df

A data frame containing the variables in the model.

tsi

An object of class tsi, created by tsi(), that defines the number of time periods relative to the intervention time period for each cohort observation.

weights

An optional vector of weights to be passed to lm() to be used in the fitting process. Should be NULL or a numeric vector.

ncol

Number of columns in the faceted plot

Examples

Run this code
# Use a formula to specify the setup of the time-series plot. Here we set
# hospitalized as the outcome, faceted by county, with yr on the X axis.
ts_plot(hospitalized ~ county + yr,
        df = hosp,
        intervention_var = "intervention_yr")

# We can specify the same plot without using a formula.
ts_plot(y = "hospitalized",
        group = "county",
        time_var = "yr",
        df = hosp,
        intervention_var = "intervention_yr")

Run the code above in your browser using DataLab