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",
  "forcats",
  "sciplot"
)

# Change to install = TRUE to install the required packages
pacman::p_load(packages, character.only = TRUE, install = FALSE)

# Load an example dataset ---------------
data <- read_dta("https://github.com/worldbank/r-econ-visual-library/raw/master/Library/Data/ScatterFittedLine.dta")

data <- data %>% mutate(post = as.factor(post))

ggplot(data, aes(x = area_cult, y = revenue, colour = post)) +
  geom_point(aes(alpha = 0.2), show.legend = FALSE) +
  geom_smooth(aes(group = post, colour = post), method = "lm", se = FALSE) +
  theme_classic() +
  ylab("Agriculture revenue (BRL thousands)") +
  xlab("Cultivated area (ha)") +
  scale_color_discrete(name = "", labels = c("Pre-treatment", "Post-treatment")) +
  theme(
    axis.text = element_text(size = 12),
    axis.title = element_text(size = 14),
    legend.text = element_text(size = 12),
    legend.position = "bottom"
  )
## `geom_smooth()` using formula 'y ~ x'