Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidsjoberg/fa6c742b4a536781dac08c36fcc6322a to your computer and use it in GitHub Desktop.
Save davidsjoberg/fa6c742b4a536781dac08c36fcc6322a to your computer and use it in GitHub Desktop.
Sankey in rows
library(tidyverse)
library(ggsankey)
library(gapminder)
df <- gapminder %>%
group_by(continent, year) %>%
summarise(gdp = (sum(pop * gdpPercap)/1e9) %>% round(0), .groups = "keep") %>%
ungroup()
ggplot(df, aes(x = year,
node = continent,
fill = continent,
value = gdp,
shift = shift)) +
geom_sankey_bump(data = df %>% mutate(shift = 0), space = 1500, type = "sankey", color = "transparent", smooth = 6) +
geom_sankey_bump(data = df %>% mutate(shift = 70000), space = 1500, type = "sankey", color = "transparent", smooth = 6) +
geom_sankey_bump(data = df %>% mutate(shift = 70000*2), space = 1500, type = "sankey", color = "transparent", smooth = 6) +
geom_sankey_bump(data = df %>% mutate(shift = 0), space = 1500, type = "sankey", color = "transparent", smooth = 6) +
scale_fill_viridis_d(option = "A", alpha = .8) +
theme_sankey_bump(base_size = 16) +
labs(x = NULL,
y = "GDP ($ bn)",
fill = NULL,
color = NULL) +
theme(legend.position = "bottom") +
labs(title = "GDP development per continent")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment