-
Notifications
You must be signed in to change notification settings - Fork 6
/
ggiplot.Rmd
289 lines (226 loc) · 8.07 KB
/
ggiplot.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
---
title: "Comparing ggiplot with iplot"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Comparing ggiplot with iplot}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
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`.^[The `ggcoefplot` and `coefplot`
functions share a subset of the differences presented here, so you should be
aware of those too once you've read this vignette.]
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.
```{r setup}
library(ggfixest)
```
In the examples that follow, I'll be drawing on the **fixest**
[introductory vignette](https://lrberge.github.io/fixest/articles/fixest_walkthrough.html),
as well as the `iplot` help documentation.
### Example 1: Vanilla TWFE
```{r est_did}
data(base_did)
est_did = feols(y ~ x1 + i(period, treat, 5) | id + period, base_did)
```
Let's compare the (base) `iplot` and `ggiplot` default plots.
```{r est_did_plot_defaults}
iplot(est_did)
ggiplot(est_did)
```
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).
```{r est_did_ebar}
ggiplot(est_did, geom = 'errorbar')
```
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.
```{r est_did_pt_join}
iplot(est_did, pt.join = TRUE)
ggiplot(est_did, pt.join = TRUE, geom_style = 'errorbar')
```
The `ggiplot` defaults are slightly different in some cases, but may require
less arguments depending on what you want to do. For example,
```{r est_did_ribbon}
# 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')
ggiplot(est_did, geom_style = 'ribbon', pt.pch = NA, col = 'orange')
```
Unlike base `iplot`, multiple confidence interval levels are supported.
This works for ribbons too.
```{r est_did_ci_multi}
ggiplot(est_did, ci_level = c(.8, .95))
```
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.
```{r est_did_aggr_eff}
ggiplot(
est_did, ci_level = c(.8, .95),
aggr_eff = "post", aggr_eff.par = list(col = "orange") # default col is grey
)
```
### Example 2: Multiple estimation (i)
We'll demonstrate multiple estimation functionality using the staggered treatment example
(comparing vanilla TWFE with the Sun-Abraham estimator) from the **fixest**
introductory vignette.
```{r base_stagg}
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.
```{r stagg_iplot}
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.
```{r stagg_ggiplot}
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.
```{r stagg_ggiplot_noname}
ggiplot(
list(est_twfe, est_sa20),
main = 'Staggered treatment', ref.line = -1, pt.join = TRUE
)
```
One nice thing about the **ggplot2** API is that it makes changing multiplot
figures simple. For example, if you don't like the presentation of "dodged"
models in a single frame, then it's easy to facet them instead using
the `multi_style = 'facet'` argument.
```{r stagg_ggiplot_facet}
ggiplot(
list('TWFE' = est_twfe, 'Sun & Abraham (2020)' = est_sa20),
main = 'Staggered treatment', ref.line = -1, pt.join = TRUE,
multi_style = 'facet'
)
```
### Example 3: Multiple estimation (ii)
An area where `ggiplot` shines is in complex multiple estimation cases, such
as lists of `fixest_multi` objects. To illustrate, let's add a split variable
(group) to our staggered dataset.
```{r base_stagg_grp}
base_stagg_grp = base_stagg
base_stagg_grp$grp = ifelse(base_stagg_grp$id %% 2 == 0, 'Evens', 'Odds')
```
Now re-run our two regressions from earlier, but splitting the sample to
generate `fixest_multi` objects.
```{r stagg_grp}
est_twfe_grp = feols(
y ~ x1 + i(time_to_treatment, treated, ref = c(-1, -1000)) | id + year,
data = base_stagg_grp, split = ~ grp
)
est_sa20_grp = feols(
y ~ x1 + sunab(year_treated, year) | id + year,
base_stagg_grp, split = ~ grp
)
```
Both `iplot` and `ggiplot` do fine with a single `fixest_multi` object (although
remember that we have to manually add a legend for the former)
```{r stagg_grp_single}
iplot(est_twfe_grp, ref.line = -1, main = 'Staggered treatment: TWFE')
legend('topleft', col = c(1, 2), pch = c(20, 17), legend = c('Evens', 'Odds'))
ggiplot(est_twfe_grp, ref.line = -1, main = 'Staggered treatment: TWFE')
```
However, `iplot` complains if we combine a list of _several_ `fixest_multi`
objects.
```{r stagg_grp_multi_iplot, error = TRUE}
iplot(
list('TWFE' = est_twfe_grp, 'Sun & Abraham (2020)' = est_sa20_grp),
ref.line = -1, main = 'Staggered treatment: Split mutli-sample'
)
```
In contrast, `ggiplot` works...
```{r stagg_grp_multi_ggiplot}
ggiplot(
list('TWFE' = est_twfe_grp, 'Sun & Abraham (2020)' = est_sa20_grp),
ref.line = -1, main = 'Staggered treatment: Split mutli-sample'
)
```
... but is even better when we use faceting instead of dodged errorbars.
Let's use this as an opportunity to construct a fancy plot that invokes some
additional arguments and ggplot theming.
```{r stagg_grp_multi_ggiplot_fancy}
ggiplot(
list("TWFE" = est_twfe_grp, "Sun & Abraham (2020)" = est_sa20_grp),
ref.line = -1,
main = "Staggered treatment: Split mutli-sample",
xlab = "Time to treatment",
multi_style = "facet",
geom_style = "ribbon",
facet_args = list(labeller = labeller(id = \(x) gsub(".*: ", "", x))),
theme = theme_minimal() +
theme(
text = element_text(family = "HersheySans"),
plot.title = element_text(hjust = 0.5),
legend.position = "none"
)
)
```
### Asides
#### On theming and scale adjustments
Setting the theme inside the `ggiplot` call is optional and not strictly
necessary, since the ggplot2 API allows programmatic updating of existing
plots. E.g.
```{r theme_update}
last_plot() +
labs(caption = 'Note: Super fancy plot brought to you by ggiplot')
last_plot() +
theme_grey() +
theme(legend.position = 'none') +
scale_fill_brewer(palette = 'Set1', aesthetics = c('colour', 'fill'))
```
etc.
#### On dictionaries
Dictionaries work similarly to `iplot`. Simple example:
```{r dict}
base_did$letter = letters[base_did$period]
est_letters = feols(y ~ x1 + i(letter, treat, 'e') | id+letter, base_did)
# Dictionary for capitalising the letters
dict = LETTERS[1:10]; names(dict) = letters[1:10]
ggiplot(est_letters) # No dictionary
```
You can either set the dictionary directly in the plot call...
```{r dict_direct}
ggiplot(est_letters, dict = dict)
```
... Or, set it globally using the `setFixest_dict` macro.
```{r dict_global}
setFixest_dict(dict)
ggiplot(est_letters)
setFixest_dict() # reset
```