As far as possible, the ggfixest plotting
functions try to mimic the behaviour of their base compatriots. However,
they also offer additional functionality thanks to the
ggplot2 API. This vignette will walk you through the
key differences and correspondences, specifically with regards to
ggiplot
versus the original iplot
.1
Start by loading ggfixest. This will automatically load ggplot2 and and fixest too, as both of these packages are required for this one to work.
In the examples that follow, I’ll be drawing on the
fixest introductory
vignette, as well as the iplot
help documentation.
Let’s compare the (base) iplot
and ggiplot
default plots.
There are some small differences, but they are certainly producing
the same basic plot. To get even closer to the original, we could
specify the use of errorbar(s) rather than (ggiplot
’s
default of) pointrange(s).
Many of the arguments for iplot
carry over to
ggiplot
too. This is deliberate, since we want to reduce
the cognitive overhead of switching between the two plotting methods.
For example, we can join points using the same
pt.join = TRUE
argument.
The ggiplot
defaults are slightly different in some
cases, but may require less arguments depending on what you want to do.
For example,
# iplot(est_did, pt.join = TRUE, ci.lty = 0, ci.width = 0, ci.fill = TRUE)
iplot(
est_did, pt.join = TRUE, ci.lty = 0, ci.width = 0, ci.fill = TRUE,
ci.fill.par = list(col = 'black', alpha = 0.3)
)
ggiplot(est_did, geom_style = 'ribbon', pt.pch = NA, col = 'orange')
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.
Unlike base iplot
, multiple confidence interval levels
are supported. This works for ribbons too.
Another new feature (i.e. unsupported in base iplot
) is
adding aggregated post- and/or pre-treatment effects to your plots.
Here’s an example that builds on the previous plot, by adding the mean
post-treatment effect.
ggiplot(
est_did, ci_level = c(.8, .95),
aggr_eff = "post", aggr_eff.par = list(col = "orange") # default col is grey
)
We’ll demonstrate multiple estimation functionality using the staggered treatment example (comparing vanilla TWFE with the Sun-Abraham estimator) from the fixest introductory vignette.
data(base_stagg)
est_twfe = feols(
y ~ x1 + i(time_to_treatment, treated, ref = c(-1, -1000)) | id + year,
data = base_stagg
)
est_sa20 = feols(
y ~ x1 + sunab(year_treated, year) | id + year,
data = base_stagg
)
Again, for comparison, here the base iplot
original.
Note that we add the legend manually.
iplot(
list('TWFE' = est_twfe, 'Sun & Abraham (2020)' = est_sa20),
main = 'Staggered treatment', ref.line = -1, pt.join = TRUE
)
legend(
'topleft', col = c(1, 2), pch = c(20, 17),
legend = c('TWFE', 'Sun & Abraham (2020)')
)
Here’s the ggiplot
version.
ggiplot(
list('TWFE' = est_twfe, 'Sun & Abraham (2020)' = est_sa20),
main = 'Staggered treatment', ref.line = -1, pt.join = TRUE
)
If we don’t name out list of models then it defaults to something sensible.