R Econ Visual Library

R code for data visualization in economics, created and maintained by DIME Analytics.

# 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))
## Picking joint bandwidth of 18.3