Skip to content

Instantly share code, notes, and snippets.

@tonyelhabr
Last active October 26, 2021 23:00
Show Gist options
  • Save tonyelhabr/b2d43672ac96af369f96b01f83852211 to your computer and use it in GitHub Desktop.
Save tonyelhabr/b2d43672ac96af369f96b01f83852211 to your computer and use it in GitHub Desktop.
example sankey diagram
library(tibble)
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggsankey)
df <-
tibble(
id = c(
letters[1:13],
letters[c(1:3, 5, 7, 9, 11:12)],
letters[c(1:2, 5, 7, 9, 11)]
),
node = c(
rep('Multiple', 4), rep('Single - Associational', 2), rep('Single - Community', 2), rep('Single - Occupational', 2), rep('State', 3),
rep('Multiple', 3), rep('Single - Associational', 1), rep('Single - Community', 1), rep('Single - Occupational', 1), rep('State', 2),
rep('Multiple', 2), rep('Single - Associational', 1), rep('Single - Community', 1), rep('Single - Occupational', 1), rep('State', 1)
),
year = c(
rep(2002, 13),
rep(2010, 8),
rep(2020, 6)
)
)
# i think your data looks like this?
df_w_nas <- df %>%
expand(id, year) %>%
left_join(df) %>%
arrange(id, year)
network_w_nas <- df_w_nas %>%
arrange(id, year) %>%
group_by(id) %>%
mutate(
next_year = lead(year)
) %>%
ungroup()
network_w_nas %>%
ggplot() +
aes(x = year, next_x = next_year, node = node, next_node = node) +
geom_sankey(
flow.alpha = .6,
# node.color = 'gray30', # this is the issue, assuming you're using some of the code in the package README here: https://github.com/davidsjoberg/ggsankey/blob/main/README.Rmd#L313-L322
aes(fill = node)
) +
geom_sankey_label(
aes(label = node),
# color = 'white',
# fill = 'gray40',
size = 3
) +
scale_fill_viridis_d() +
theme_sankey() +
labs(x = NULL) +
theme(
legend.position = "none",
plot.title = element_text(hjust = .5)
)
@tonyelhabr
Copy link
Author

plot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment