-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathREADME.Rmd
162 lines (127 loc) · 5.41 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
dpi = 300,
out.width = "100%"
)
```
# ggfixest
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/ggfixest)](https://CRAN.R-project.org/package=ggfixest)
[![R-universe status badge](https://grantmcdermott.r-universe.dev/badges/ggfixest)](https://grantmcdermott.r-universe.dev)
[![CRAN checks](https://badges.cranchecks.info/worst/ggfixest.svg)](https://cran.r-project.org/web/checks/check_results_ggfixest.html)
[![R-CMD-check](https://github.com/grantmcdermott/ggfixest/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/grantmcdermott/ggfixest/actions/workflows/R-CMD-check.yaml)
[![Docs](https://img.shields.io/badge/docs-stable-blue.svg)](https://grantmcdermott.com/ggfixest/index.html)
[![Docs](https://img.shields.io/badge/docs-dev-blue.svg)](https://grantmcdermott.com/ggfixest/dev/index.html)
<!-- badges: end -->
The **ggfixest** package provides dedicated **ggplot2** plotting methods for
**fixest** objects. Specifically, it provides drop-in "gg" equivalents of the
latter's [`coefplot`](https://lrberge.github.io/fixest/reference/coefplot.html)
and [`iplot`](https://lrberge.github.io/fixest/reference/coefplot.html)
base plotting functions.
The goal of **ggfixest** is to produce nice looking coefficient plots and
interaction plots---including
[event study](https://theeffectbook.net/ch-EventStudies.html)
plots---with minimal effort and scope for further customization.
## Installation
The stable version of **ggfixest** is available on CRAN.
``` r
install.packages("ggfixest")
```
Or, you can grab the latest development version from R-universe.
``` r
install.packages("ggfixest", repos = "https://grantmcdermott.r-universe.dev")
```
## Quickstart
The [package website](https://grantmcdermott.com/ggfixest/)
provides a number of examples in the help documentation. (Also available by
typing `?ggcoefplot` or `?ggiplot` in your R console.) But here are a few
quickstart examples to whet your appetite.
Start by loading the **ggfixest** package.
```{r pkgload}
library(ggfixest)
```
Note that this automatically loads **ggplot2** and **fixest** as required
dependencies too. As the package name suggests, **ggfixest** _only_ supports
**fixest** model objects.^[For other model classes, a more generic visualization
package/tool like
[**see**](https://easystats.github.io/see/articles/parameters.html) or
[**modelsummary**](https://modelsummary.com/vignettes/modelplot.html)
would be more appropriate.]
### Coefficient plots
Use `ggcoefplot` to draw basic coefficient plots.
```{r coefplot1, message=FALSE}
est = feols(
Petal.Length ~ Petal.Width + Sepal.Length + Sepal.Width + Species,
data = iris
)
# coefplot(est) ## base version
ggcoefplot(est) ## this package
```
The above plot call and output should look very familiar to regular **fixest**
users. Like its base equivalent, `ggcoefplot` can be heavily customized and
contains various shortcuts for common operations. For example, we can use regex
to control the coefficient grouping logic.
```{r coefplot2, message=FALSE}
ggcoefplot(est, group = list(Sepal = "^^Sepal.", Species = "^^Species"))
```
### Event study plots
The `ggiplot` function is a special case of `ggocoefplot` that only plots
coefficients with factor levels or interactions (specifically, those created
with the [`i`](https://lrberge.github.io/fixest/reference/i.html)
operator). This is especially useful for producing event study plots in a
difference-in-differences (DiD) setup.
```{r es1, message=FALSE}
est_did = feols(y ~ x1 + i(period, treat, 5) | id+period, base_did)
# iplot(est_did) ## base version
ggiplot(est_did) ## this package
```
Again, the above plot call and output should look very familiar to regular
**fixest** users. But note that `ggiplot` supports several features that are
not available in the base `iplot` version. For example, plotting multiple
confidence intervals and aggregate treatments effects.
```{r es2}
ggiplot(
est_did,
ci_level = c(.8, .95),
aggr_eff = "post", aggr_eff.par = list(col = "orange")
)
```
And you can get quite fancy, combining lists of complex multiple estimation
objects with custom themes, and so on.
```{r es3}
base_stagg_grp = base_stagg
base_stagg_grp$grp = ifelse(base_stagg_grp$id %% 2 == 0, 'Evens', 'Odds')
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,
data = base_stagg_grp, split = ~grp
)
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"
)
)
```
For more `ggiplot` examples and comparisons with its base counterpart, see the
detailed [vignette](http://grantmcdermott.com/ggfixest/articles/ggiplot.html) on
the package homepage (or, by typing `vignette("ggiplot")` in your R console).