Income Inequality before and after taxes

ridges
social
economy
Author

Ana Luisa Bodevan

Published

August 5, 2025

Code
# TIDYTUESDAY WEEK 31
# INCOME INEQUALITY BEFORE AND AFTER TAXES 

# 1. SETUP
 
library(pacman)

pacman :: p_load(tidyverse, ggtext, showtext, ggrepel, 
                 dplyr, janitor, scales, glue, ggridges)

income_inequality_processed <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-08-05/income_inequality_processed.csv')

# 2. DATA ANALYSIS AND TIDYING 

df <- income_inequality_processed %>%
  mutate(
    redistribution = gini_mi_eq - gini_dhi_eq
  ) %>%
  filter(!is.na(redistribution)) # create the redistribution variable 

country_counts <- df %>%
  count(Entity) %>%
  filter(n >= 5) # select only countries with 5 or more reports 

country_order <- df %>%
  group_by(Entity) %>%
  summarise(median_redistribution = median(redistribution, na.rm = TRUE)) %>%
  arrange(median_redistribution) %>%
  pull(Entity) # order countries by median redistribution 

df <- df %>% 
  filter(Entity %in% country_counts$Entity) %>% 
  mutate(Entity = factor(Entity, levels = country_order))

ggplot(df, aes(x = redistribution, y = Entity, fill = ..x..)) +
  geom_density_ridges_gradient(
    scale = 3,
    rel_min_height = 0.01,
    color = "black",
    size = 0.3
  ) +
  scale_fill_viridis_c(option = "magma", direction = -1, name = "Redistribution") +
  labs(
    title = "Distribution of Post-Tax Inequality",
    x = NULL,
    y = NULL,
    caption = "TidyTuesday 2025 W31 | anabodevan.github.io",
  ) +
  theme_minimal(base_family = "Georgia") +
  theme(
    panel.grid.major = element_line(color = "gray90"),
    panel.grid.minor = element_blank(),
    axis.text.x = element_blank(),
    axis.text.y = element_text(size = 8, face = "bold", color = "#444444"),
    axis.title = element_text(size = 10, face = "bold"),
    plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 5, color = "#444444"),
    legend.position = "right"
  )

ggsave(
  filename = file.path("tidytuesday", "2025", "2025-08-05", paste0("20250805", ".png")),
  height = 7,
  width = 5,
  bg = "white",
  units = "in",
  dpi = 300
)