plot_parallel_trends: Plots parallel trends figures

View source: R/plot_parallel_trends.r

plot_parallel_trendsR Documentation

Description

The plot_parallel_trends() function combines the various trends data CSV files and plots parallel trends figures. All treatment and all control groups can be combined so that there is one control line and one treatment line by setting combine = TRUE.

Usage

plot_parallel_trends(
  dir_path,
  covariates = FALSE,
  save_csv = FALSE,
  combine = FALSE,
  pch = NA,
  pch_control = NA,
  pch_treated = NA,
  control_colour = c("darkgrey", "lightgrey"),
  control_color = NULL,
  treatment_colour = c("darkred", "lightcoral"),
  treatment_color = NULL,
  lwd = 2,
  xlab = NA,
  ylab = NA,
  title = NA,
  xticks = 4,
  date_format = "%Y-%m-%d",
  xdates = NULL,
  xaxlabsz = 0.8,
  save_png = FALSE,
  width = 800,
  height = 600,
  ylim = NULL,
  yaxlabsz = 0.8,
  ylabels = NULL,
  yticks = 4,
  ydecimal = 2,
  legend_location = "topright",
  simplify_legend = TRUE,
  legend_cex = 0.7,
  legend_on = TRUE,
  treatment_indicator_col = "grey",
  treatment_indicator_alpha = 0.5,
  treatment_indicator_lwd = 2,
  treatment_indicator_lty = 2,
  interpolate = FALSE,
  filepath = tempdir(),
  filenamecsv = "combined_trends_data.csv",
  filenamepng = "undid_plot.png"
)

Arguments

dir_path

A character filepath to the folder containing all of the trends data CSV files.

covariates

A logical value (defaults to FALSE) indicating whether or not to consider covariates, i.e. whether or not to use the mean_outcome column or the mean_outcome_residualized column from the trends data CSV files.

save_csv

A logical value (defaults to FALSE) indicating whether or not to save the combined_trends_data.csv.

combine

A logical value (defaults to FALSE) indicating whether to plot each silo separately or to combine silos based on treatment status.

pch

An integer (0 to 25) or vector of integers (from 0 to 25) which determine the style of points used on the plot. Setting to NA (default) will omit points from the plot.

pch_control

An integer (from 0 to 25) or vector of integers (from 0 to 25) which determine the style of points used on the plot for control silos. Takes value of pch if set to NULL (default).

pch_treated

An integer (from 0 to 25) or vector of integers (from 0 to 25) which determine the style of points used on the plot for treated silos. Takes value of pch if set to NULL (default).

control_colour

A character vector of colours (defaults to c("darkgrey", "lightgrey")) for the control silo lines. If combine = TRUE, takes the 1st value to determine the colour of the control line.

control_color

Overrides control_colour if used. Defaults to NULL.

treatment_colour

A character vector of colours (defaults to c("darkred", "lightcoral")) for the treatment silos. If combine = TRUE, takes the 1st value to determine the colour of the control line.

treatment_color

Overrides control_colour if used. Defaults to NULL.

lwd

An integer (defaults to 2) for selecting the line widths.

xlab

A character value for the x-axis label (defaults to NA).

ylab

A character value for the y-axis label (defaults to NA).

title

A character value for the title of the plot (defaults to NA).

xticks

An integer value denoting how many ticks to display on the x-axis (defaults to 4).

date_format

A string value denoting the format with which to display the dates along the x-axis (defaults to "%Y"). Uses standard R date formatting styles.

xdates

Takes in a vector of date objects to be used as the dates shown along the x-axis (defaults to NULL).

xaxlabsz

A double indicating the x-axis label sizes in comparison to a standardized default size (defaults to 0.8).

save_png

A logical value indicating whether or not to save the plot as a PNG file (defaults to FALSE).

width

An integer denoting the width of the saved PNG file.

height

An integer denoting the height of the saved PNG file.

ylim

A vector of two doubles defining the min and max range of the values on the y-axis. Defaults to the min and max values of the values to be plotted.

yaxlabsz

A double for specifying the y-axis label sizes (defaults to 0.8) in comparison to a standardized default size.

ylabels

A vector of values that you would like to appear on the y-axis (defaults to NULL).

yticks

An integer denoting how many values to display along the y-axis (defaults to 4).

ydecimal

An integer value denoting to which decimal point the values along the y-axis are rounded to.

legend_location

A character value for determining the location of the legend (defaults to "topright"). Options are: "topright", "topleft", "bottomright", "bottomleft", "top", "bottom", "left", "right", "center".

simplify_legend

A logical value which if set to TRUE shows one colour for the treatment silos in the legend and one colour for the control silos. Defaults to TRUE.

legend_cex

A double for adjusting the size of the text in the legend compared to a standard default size. Defaults to 0.7.

legend_on

A logical value for turning the legend on or off (defaults to TRUE).

treatment_indicator_col

A character value for determining the colour of the dashed vertical lines showing when treatment times were (defaults to "grey").

treatment_indicator_alpha

A double for for determining the transparency level of the dashed vertical lines showing the treatment times (defaults to 0.5).

treatment_indicator_lwd

A double for selecting the line width of the treatment indicator lines (defaults to 2).

treatment_indicator_lty

An integer for the selecting the lty option, i.e. the line style, for the treatment_indicator lines (defaults to 2).

interpolate

A logical value (either TRUE or FALSE) which determines if interpolation should be used to fill missing trends data. Defaults to FALSE. Uses a piecewise linear function.

filepath

Filepath to save the CSV file. Defaults to tempdir().

filenamecsv

A string filename for the combined trends data Defaults to "combined_trends_data.csv".

filenamepng

A string filename for the PNG file output. Defaults to "undid_plot.png".

Value

A data frame built from the trends data from all CSV files in the specified directory. If combine = FALSE, the data frame includes all silos joined by row. If combine = TRUE, the data frame merges treated silos into a single treatment group and control silos into a single control group.

Examples

# Get path to example data included with package
dir_path <- system.file("extdata/staggered", package = "undidR")

# Basic usage with default parameters
plot_parallel_trends(dir_path)

# Custom plot with modified parameters
plot_parallel_trends(dir_path, combine = TRUE, lwd = 4,
                     xdates = as.Date(c("1989-01-01", "1991-01-01",
                                        "1993-01-01", "1995-01-01",
                                        "1997-01-01", "1999-01-01")))

undidR documentation built on April 3, 2025, 5:42 p.m.