Alluvial plot
Category: other_not_ready

library(tidyverse)
library(ggalluvial)
states_all
# A tibble: 13,168,874 x 5
# bin `SD 3m` `SD 6m` `SD 12m` `CR 12m`
# <chr> <dbl> <dbl> <dbl> <dbl>
# 1 chr1:0-200 8 8 8 8
# 2 chr1:200-400 8 8 8 8
# 3 chr1:400-600 8 8 8 8
# 4 chr1:600-800 8 8 8 8
# 5 chr1:800-1000 8 8 8 8
# 6 chr1:1000-1200 8 8 8 8
# 7 chr1:1200-1400 8 8 8 8
# 8 chr1:1400-1600 8 8 8 8
# 9 chr1:1600-1800 8 8 8 8
# 10 chr1:1800-2000 8 8 8 8
# … with 13,168,864 more rows
state_transition_lodes <- states_all %>%
group_by(`SD 3m`, `SD 12m`, `CR 12m`) %>%
tally() %>%
ungroup() %>%
mutate(
`SD 3m` = factor(`SD 3m`, levels = c('1','2','3','4','5','6','7','8')),
`SD 12m` = factor(`SD 12m`, levels = c('1','2','3','4','5','6','7','8')),
`CR 12m` = factor(`CR 12m`, levels = c('1','2','3','4','5','6','7','8')),
alpha = ifelse((`SD 3m` == `SD 12m` & `SD 3m` == `CR 12m`), 0, 0.5)
) %>%
to_lodes_form(key = 'Demographic', axes = 1:3)
state_transition_lodes
# A tibble: 1,290 x 5
# n alpha alluvium Demographic stratum
# <int> <dbl> <int> <fct> <fct>
# 1 25324 0 1 SD 3m 1
# 2 2792 0.5 2 SD 3m 1
# 3 1482 0.5 3 SD 3m 1
# 4 3040 0.5 4 SD 3m 1
# 5 187 0.5 5 SD 3m 1
# 6 74 0.5 6 SD 3m 1
# 7 254 0.5 7 SD 3m 1
# 8 237 0.5 8 SD 3m 1
# 9 872 0.5 9 SD 3m 1
# 10 1053 0.5 10 SD 3m 1
# … with 1,280 more rows
fig_4C <-
state_transition_lodes %>%
ggplot(aes(x = Demographic, stratum = stratum, alluvium = alluvium, y = n,
label = stratum)) +
scale_x_discrete(labels = c('SD 3m', 'SD 12m', 'CR 12m'), position = 'top') +
geom_alluvium(aes(fill = stratum, alpha = alpha), width = 1/3) +
geom_stratum(aes(fill = stratum, alpha = 0.75), width = 1/3) +
geom_label(stat = 'stratum') +
scale_fill_brewer(palette = 'Dark2', direction = 1) +
theme_bw() +
labs(x = '', y = '', title = 'C') +
theme_bw() +
theme(
plot.title = element_text(face = 'bold', size = 20),
legend.position = 'none',
axis.title = element_blank(),
axis.text.x = element_text(face = 'bold', colour = 'black', size = 15),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank()
)
ggsave('figure_4/C.pdf', fig_4C)
p <- ggplot(
data = titanic_wide,
aes(
axis1 = Class,
axis2 = Sex,
axis3 = Age,
y = Freq
)
) +
scale_x_discrete(limits = c('Class', 'Sex', 'Age'), expand = c(0.1, 0), position = 'top') +
geom_alluvium(aes(fill = Survived)) +
geom_stratum() +
scale_fill_jcolors(palette = 'default') +
geom_text(stat = 'stratum', label.strata = TRUE) +
theme_bw() +
theme(
plot.title = element_text(face = 'bold', size = 20),
legend.position = 'none',
axis.title = element_blank(),
axis.text.x = element_text(face = 'bold', colour = 'black', size = 15),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank()
)