forked from worldbank/r-econ-visual-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontribution-template.Rmd
More file actions
74 lines (59 loc) · 2.37 KB
/
contribution-template.Rmd
File metadata and controls
74 lines (59 loc) · 2.37 KB
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
---
# Use a description of your plot here to facilitate search
pagetitle: "Density Plots: Density by rounds and by group with ridgelines"
# Add your GitHub handle to get credit for your contribution
author: "@mizuhirosuzuki"
# Date is optional
date: ""
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r SameNameAsYourScript, fig.path = '../docs/figure/', warning = FALSE}
# Install and load packages ===========================================================
packages <-
c("tidyverse",
"haven",
"ggridges")
pacman::p_load(packages,
character.only = TRUE,
install = FALSE) # Change to install = TRUE to install the required packages
# Data wrangling =====================================================================
# Original data is available at https://microdata.worldbank.org/index.php/catalog/2249.
# Only relevant variables are kept in the dataset used here.
data <-
read_dta("https://github.com/worldbank/r-econ-visual-library/raw/master/Library/Data/ReplicationDataGhanaJDE_short.dta")
# For simpliticity, we will includes only those who received treatment
# between 2nd and 3rd waves in the treatment group.
analysis_data <-
data %>%
filter(wave >= 2) %>%
group_by(sheno) %>%
mutate(treatment = max((wave == 3) & (timetreat == 1)),
control = all(control == 1)) %>%
filter(treatment == TRUE | control == TRUE) %>%
ungroup() %>%
mutate(treatment_group = ifelse(cashtreat == 1,
"Cash",
ifelse(equiptreat == 1,
"In-kind",
"Control")))
# Create graph ====================================================================
ggplot(analysis_data,
aes(x = realfinalprofit,
y = fct_rev(factor(wave)),
color = factor(treatment_group),
fill = factor(treatment_group))) +
geom_density_ridges(alpha = 0.1,
scale = 1) +
theme_ridges() +
xlab("3-month Real Profit (cedi)") +
ylab("Rounds") +
scale_color_brewer(palette = "Set2",
name = "Group",
breaks = c("Control", "Cash", "In-kind")) +
scale_fill_brewer(palette = "Set2",
name = "Group",
breaks = c("Control", "Cash", "In-kind")) +
xlim(c(0, 250))
```