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.
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"
)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.
A character filepath to the folder containing all of the trends data CSV files.
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.
A logical value (defaults to FALSE)
indicating whether or not to save the combined_trends_data.csv.
A logical value (defaults to FALSE) indicating whether
to plot each silo separately or to combine silos based on treatment status.
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.
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).
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).
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.
Overrides control_colour if used. Defaults to NULL.
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.
Overrides control_colour if used.
Defaults to NULL.
An integer (defaults to 2) for selecting the line widths.
A character value for the x-axis label (defaults to NA).
A character value for the y-axis label (defaults to NA).
A character value for the title of the plot (defaults to NA).
An integer value denoting how many ticks to display
on the x-axis (defaults to 4).
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.
Takes in a vector of date objects to be used as the dates shown
along the x-axis (defaults to NULL).
A double indicating the x-axis label sizes in comparison
to a standardized default size (defaults to 0.8).
A logical value indicating whether or not to save the plot
as a PNG file (defaults to FALSE).
An integer denoting the width of the saved PNG file.
An integer denoting the height of the saved PNG file.
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.
A double for specifying the y-axis label sizes
(defaults to 0.8) in comparison to a standardized default size.
A vector of values that you would like to appear
on the y-axis (defaults to NULL).
An integer denoting how many values to display
along the y-axis (defaults to 4).
An integer value denoting to which decimal point the values along the y-axis are rounded to.
A character value for determining the location
of the legend (defaults to "topright"). Options are: "topright",
"topleft", "bottomright", "bottomleft", "top", "bottom",
"left", "right", "center".
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.
A double for adjusting the size of the text in the legend
compared to a standard default size. Defaults to 0.7.
A logical value for turning the legend on or off
(defaults to TRUE).
A character value for determining the colour
of the dashed vertical lines showing when treatment times were
(defaults to "grey").
A double for for determining the
transparency level of the dashed vertical lines showing the treatment
times (defaults to 0.5).
A double for selecting the line width
of the treatment indicator lines (defaults to 2).
An integer for the selecting the lty option,
i.e. the line style, for the treatment_indicator lines (defaults to 2).
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 to save the CSV file. Defaults to tempdir().
A string filename for the combined trends data
Defaults to "combined_trends_data.csv".
A string filename for the PNG file output.
Defaults to "undid_plot.png".
# 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")))
Run the code above in your browser using DataLab