library(tidyverse)

income_raw <- read_csv("https://raw.githubusercontent.com/jackblun/Globalinc/master/GCIPrawdata.csv", 
                       skip = 2)

income <- income_raw %>% 
  gather(decile, value, starts_with("Decile")) %>%
  mutate(decile = as.integer(str_remove_all(decile, "Decile|Income"))) %>% 
  mutate(decile = factor(decile))

income_2014 <- income %>% 
  filter(Year == 2014) %>% 
  arrange(`Mean Income`) %>% 
  mutate(Country = fct_inorder(Country)) %>% 
  drop_na(Country)

Giant unreadable heatmap

# Deciles on x-axis, countries on y, filled by deciles
plot_income_2014 <- ggplot(income_2014, 
                           aes(x = decile, y = Country, fill = value)) + 
  geom_tile() +
  scale_fill_viridis_c(option = "magma", 
                       labels = scales::dollar,
                       name = "2005 $ PPP") + 
  labs(x = "Decile", y = NULL, 
       title = "World income distribution in 2014") +
  theme_minimal(base_size = 6) +
  guides(fill = guide_colorbar(barwidth = 20, barheight = 0.5, 
                               title.position = "top")) +
  theme(legend.position = "bottom")

plot_income_2014

Interactive giant heatmap

library(plotly)
ggplotly(plot_income_2014)

PPP binned into quintiles

income_2014_binned <- income_2014 %>% 
  mutate(ppp_binned = ntile(value, 5)) %>% 
  rename(`2005 $ PPP` = value)

plot_income_2014_binned <- ggplot(income_2014_binned, 
                                  aes(x = decile, y = Country, 
                                      fill = factor(ppp_binned))) + 
  geom_tile(aes(label = `2005 $ PPP`)) +
  scale_fill_viridis_d(option = "magma", 
                       labels = scales::percent_format(accuracy = 1)(1:5 * .20),
                       name = "2005 $ PPP percentile") + 
  labs(x = "Decile", y = NULL, 
       title = "World income distribution in 2014") +
  theme_minimal(base_size = 6) +
  guides(fill = guide_legend(title.position = "top")) +
  theme(legend.position = "bottom")
plot_income_2014_binned

Interactive binned PPP

ggplotly(plot_income_2014_binned)