| esplot | R Documentation |
Visualize dynamic treatment effects and create an event study plot. This function offers flexibility in displaying estimates, confidence intervals, and various annotations. It can handle data directly or from 'did_wrapper' objects, calculate confidence intervals from standard errors if needed, and allows for connected (line/ribbon) or point-range style plots.
esplot(data, Period = NULL, Estimate = "ATT", SE = NULL,
CI.lower = "CI.lower", CI.upper = "CI.upper", Count = NULL,
proportion = 0.3, est.lwidth = NULL, est.pointsize = NULL,
show.points = TRUE, fill.gap = TRUE, start0 = FALSE,
only.pre = FALSE, only.post = FALSE, show.count = TRUE,
stats = NULL, stats.labs = NULL, highlight.periods = NULL,
highlight.colors = NULL, highlight.shapes = NULL,
highlight.fill = FALSE,
lcolor = NULL, lwidth = NULL,
ltype = NULL, connected = FALSE, ci.outline = FALSE,
main = NULL, xlim = NULL, ylim = NULL, xlab = NULL, ylab = NULL,
gridOff = FALSE, stats.pos = NULL, theme.bw = TRUE,
legacy.style = FALSE,
cex.main = NULL, cex.axis = NULL, cex.lab = NULL,
cex.text = NULL, axis.adjust = FALSE, color = "#000000",
pre.color = NULL, post.color = NULL,
count.color = "gray70", count.alpha = 0.4,
count.outline.color = "grey69",
xangle = NULL, yangle = NULL,
xbreaks = NULL, ybreaks = NULL,
legendOff = FALSE, cex.legend = NULL,
pre.label = "Pre-treatment", post.label = "Post-treatment")
data |
The input data for the event study plot. Can be a |
Period |
The name of the column in |
Estimate |
The name of the column in |
SE |
The name of the column in |
CI.lower |
The name of the column in |
CI.upper |
The name of the column in |
Count |
Optional. The name of the column in |
proportion |
Numeric, between 0 and 1. If |
est.lwidth |
Numeric. The line width for the estimate line (if |
est.pointsize |
Numeric. The size of the points. If |
show.points |
Logical. If |
fill.gap |
Logical. If |
start0 |
Logical. If |
only.pre |
Logical. If |
only.post |
Logical. If |
show.count |
Logical. Whether to display a bar plot of the values from the |
stats |
Optional. A numeric vector of statistics (e.g., p-values) to be printed on the plot. |
stats.labs |
Optional. A character vector of labels corresponding to the |
highlight.periods |
Optional. A numeric vector of time periods to highlight with different colors. For |
highlight.colors |
Optional. A character vector of colors corresponding to |
highlight.shapes |
Optional. An integer vector of point shapes corresponding to |
highlight.fill |
Logical. If |
lcolor |
Optional. Color(s) for the reference lines. Can be a single color (applied to both horizontal y=0 line and vertical pre/post separator line) or a vector of two colors (first for horizontal, second for vertical). If |
lwidth |
Optional. Line width(s) for the reference lines. Can be a single width or a vector of two widths (similar to |
ltype |
Optional. Linetype(s) for the reference lines. Can be a single linetype (applied to both horizontal y=0 line and vertical pre/post separator line) or a vector of two linetypes (first for horizontal, second for vertical). Default is |
connected |
Logical. If |
ci.outline |
Logical. If |
main |
Optional. The main title for the plot. If |
xlim |
Optional. A numeric vector of length 2 specifying the x-axis limits ( |
ylim |
Optional. A numeric vector of length 2 specifying the y-axis limits ( |
xlab |
Optional. The label for the x-axis. If |
ylab |
Optional. The label for the y-axis. If |
gridOff |
Logical. Whether to turn off major and minor grid lines. Default is |
stats.pos |
Optional. A numeric vector of length 2 ( |
theme.bw |
Logical. Whether to use |
legacy.style |
Logical. If |
cex.main |
Optional. Numeric scaling factor for the plot title font size. The base size used by ggplot is 16. Default is |
cex.axis |
Optional. Numeric scaling factor for the axis tick mark labels font size. The base size used by ggplot is 15. Default is |
cex.lab |
Optional. Numeric scaling factor for the axis title (x and y labels) font size. The base size used by ggplot is 15. Default is |
cex.text |
Optional. Numeric scaling factor for annotated text elements (e.g., |
axis.adjust |
Logical. If |
color |
Character. The primary color for plotting estimates, points, lines, and confidence interval fills/lines (unless overridden by |
pre.color |
Optional. Character. Color for pre-treatment period estimates. If |
post.color |
Optional. Character. Color for post-treatment period estimates. If |
count.color |
Character. The fill color for the bars if |
count.alpha |
Numeric. Alpha transparency for the count bars if |
count.outline.color |
Character. The color for the outline of count bars if |
xangle |
Optional. Numeric. Rotation angle (in degrees) for x-axis tick labels. Default is |
yangle |
Optional. Numeric. Rotation angle (in degrees) for y-axis tick labels. Default is |
xbreaks |
Optional. A numeric vector of break points for the x-axis. If |
ybreaks |
Optional. A numeric vector of break points for the y-axis. If |
legendOff |
Logical. If |
cex.legend |
Optional. Numeric scaling factor for the legend text size. Default is |
pre.label |
Character. Label for the pre-treatment period in the legend. Default is |
post.label |
Character. Label for the post-treatment period in the legend. Default is |
p |
A |
Licheng Liu, Yiqing Xu, Ziyi Liu, Zhongyu Yin, Rivka Lipkovitz
# Basic example with simulated data
set.seed(123)
event_data <- data.frame(
time = -5:5,
ATT = cumsum(rnorm(11, 0, 0.2)) + c(rep(0,5), 0, 0.5, 1, 1.2, 1.5, 1.3),
SE = runif(11, 0.1, 0.3)
)
event_data$CI.lower <- event_data$ATT - 1.96 * event_data$SE
event_data$CI.upper <- event_data$ATT + 1.96 * event_data$SE
event_data$count <- sample(50:150, 11, replace = TRUE)
event_data$count[event_data$time == -5 | event_data$time == 5] <- 20 # for proportion demo
# Default plot (point-range)
esplot(event_data, Period = "time", Estimate = "ATT",
CI.lower = "CI.lower", CI.upper = "CI.upper")
# # Connected plot with ribbon
# esplot(event_data, Period = "time", Estimate = "ATT",
# CI.lower = "CI.lower", CI.upper = "CI.upper",
# connected = TRUE, show.points = TRUE)
# # Connected plot using SE for CI calculation
# event_data_no_ci <- event_data[, c("time", "ATT", "SE", "count")]
# esplot(event_data_no_ci, Period = "time", Estimate = "ATT", SE = "SE",
# connected = TRUE, ci.outline = TRUE, color = "blue")
# # Show count bars and stats
# esplot(event_data, Period = "time", Estimate = "ATT",
# CI.lower = "CI.lower", CI.upper = "CI.upper", Count = "count",
# show.count = TRUE, stats = c(0.03, 0.12), stats.labs = c("P-val Pre", "P-val Post"),
# main = "Event Study with Counts and Stats", proportion = 0.2)
# # Highlight specific periods (connected)
# esplot(event_data, Period = "time", Estimate = "ATT", SE = "SE",
# connected = TRUE, highlight.periods = c(-1, 2),
# highlight.colors = c("orange", "green"),
# main = "Highlighted Periods (Connected)")
# # Highlight specific periods (point-range)
# esplot(event_data, Period = "time", Estimate = "ATT", SE = "SE",
# connected = FALSE, highlight.periods = c(-1, 2),
# highlight.colors = c("orange", "green"),
# main = "Highlighted Periods (Point-Range)")
# # Only post-treatment period, custom labels
# esplot(event_data, Period = "time", Estimate = "ATT", SE = "SE",
# only.post = TRUE, xlab = "Years Post-Intervention", ylab = "Impact Metric",
# start0 = TRUE, color = "darkred", est.lwidth = 1.5)
# Using did_wrapper object (conceptual example, requires `did` package and setup)
# if (requireNamespace("did", quietly = TRUE)) {
# # Assume `did_out` is an output from `did::att_gt` or similar
# # and `did_wrapper_obj` is created, e.g.,
# # did_wrapper_obj <- list(est.att = event_data) # Simplified for example
# # class(did_wrapper_obj) <- "did_wrapper"
# # esplot(did_wrapper_obj) # Would use defaults: Period="time", Estimate="ATT"
# }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.