I'm trying to illustrate an alluvial chart where different persons (identified with id numbers) test different products over time. In each node, they can choose to stay (next_node = NA) or continue towards another node. The alluvial chart itself looks fine. However, the node labels are sometimes duplicated multiple times for each fill category.
library(tidyverse)
library(ggsankey)
df_plot <- structure(list(id = c(193, 193, 276, 276, 276, 927, 927,
1104, 1104, 1630, 1630, 1630, 2688, 2688, 2765, 2765, 3856, 3856,
4727, 4727, 5312, 5312, 5312, 5707, 5707, 5707, 5707, 7603, 7603,
8724, 8724, 8724, 8724, 8724, 9974, 9974, 9974, 10432, 10432,
10904, 10904, 10904, 10904, 10904, 11898, 11898, 12936, 12936,
12936, 13249, 13249, 13661, 13661, 13661, 15185, 15185, 15810,
15810, 15810, 15810, 17698, 17698, 17698, 18680, 18680, 18680,
19355, 19355, 19440, 19440, 19532, 19532, 19532, 19532, 20293,
20293, 20549, 20549, 20549, 23221, 23221, 26554, 26554, 27931,
27931, 28089, 28089, 28089, 28164, 28164, 30122, 30122, 30654,
30654, 30654, 30757, 30757, 31347, 31347, 31347, 31393, 31393,
34250, 34250, 37554, 37554, 38095, 38095, 38095, 38095, 38422,
38422, 38622, 38622, 38622, 38622, 39838, 39838, 40748, 40748,
40748, 40838, 40838, 42743, 42743, 42966, 42966, 42966, 44095,
44095, 44095, 44095, 45931, 45931, 45931, 45931, 45980, 45980,
49392, 49392, 52116, 52116, 52116, 52116, 52344, 52344, 54019,
54019, 54019, 54019, 54019, 54142, 54142, 54142, 54142, 54142,
54250, 54250, 54746, 54746, 56370, 56370, 56370, 56864, 56864,
57655, 57655, 57655, 57655, 57655, 57655, 57655, 58879, 58879,
58879, 59249, 59249, 59738, 59738, 59738, 61452, 61452, 62804,
62804, 63377, 63377, 64282, 64282, 64282, 64433, 64433, 64433,
64433, 64732, 64732, 65185, 65185, 65185, 65611, 65611, 66511,
66511, 67220, 67220, 67458, 67458, 67660, 67660, 67746, 67746,
68600, 68600, 68600, 68811, 68811, 68811, 69137, 69137, 69137,
69137, 69137, 71391, 71391, 71391, 71391, 71391, 71417, 71417,
71417, 72029, 72029, 72029, 72488, 72488, 72488, 72545, 72545,
72699, 72699, 73339, 73339, 73339, 73365, 73365, 74991, 74991,
75026, 75026, 75522, 75522, 75522, 75522, 76539, 76539, 77033,
77033, 77033, 77191, 77191, 77191, 77211, 77211, 77211, 77321,
77321, 77321), produktnamn = c("product2", "product1", "product1",
"product4", "product1", "product1", "product4", "product4", "product2",
"product1", "product4", "product1", "product1", "product8", "product2",
"product5", "product4", "product7", "product8", "product3", "product8",
"product1", "product4", "product2", "product8", "product2", "product9",
"product1", "product4", "product1", "product2", "product4", "product1",
"product2", "product1", "product8", "product1", "product6", "product7",
"product1", "product4", "product1", "product4", "product1", "product1",
"product4", "product2", "product6", "product8", "product2", "product3",
"product1", "product2", "product3", "product4", "product5", "product1",
"product2", "product2", "product3", "product1", "product4", "product1",
"product4", "product3", "product6", "product2", "product4", "product2",
"product9", "product2", "product1", "product4", "product1", "product2",
"product5", "product4", "product8", "product7", "product1", "product4",
"product2", "product3", "product1", "product4", "product1", "product7",
"product6", "product2", "product5", "product1", "product4", "product2",
"product7", "product2", "product1", "product2", "product2", "product8",
"product4", "product2", "product5", "product1", "product7", "product2",
"product3", "product1", "product2", "product8", "product2", "product2",
"product4", "product1", "product4", "product1", "product4", "product2",
"product5", "product2", "product1", "product8", "product4", "product7",
"product1", "product4", "product1", "product4", "product6", "product3",
"product4", "product3", "product4", "product1", "product4", "product4",
"product1", "product2", "product5", "product1", "product4", "product1",
"product8", "product2", "product8", "product1", "product3", "product1",
"product4", "product1", "product4", "product1", "product1", "product4",
"product1", "product2", "product5", "product4", "product2", "product2",
"product5", "product1", "product4", "product1", "product1", "product4",
"product1", "product4", "product1", "product4", "product1", "product4",
"product1", "product1", "product4", "product8", "product1", "product4",
"product1", "product4", "product1", "product2", "product4", "product2",
"product3", "product8", "product3", "product1", "product4", "product3",
"product1", "product4", "product2", "product4", "product2", "product3",
"product2", "product1", "product4", "product1", "product4", "product2",
"product7", "product8", "product3", "product2", "product5", "product8",
"product2", "product1", "product8", "product1", "product2", "product1",
"product4", "product2", "product3", "product2", "product1", "product6",
"product8", "product5", "product1", "product4", "product4", "product1",
"product4", "product8", "product2", "product7", "product2", "product1",
"product4", "product2", "product5", "product2", "product2", "product5",
"product1", "product4", "product1", "product2", "product5", "product1",
"product4", "product1", "product4", "product2", "product1", "product2",
"product1", "product2", "product4", "product1", "product4", "product1",
"product4", "product4", "product2", "product1", "product4", "product1",
"product2", "product6", "product1", "product2", "product1"),
switch_nr = c(0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 0, 0, 1, 2, 2,
0, 0, 1, 0, 1, 0, 1, 2, 2, 3, 0, 1, 0, 1, 2, 0, 1, 0, 1,
1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1,
2, 3, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0,
1, 0, 0, 1, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 0, 0, 1, 2,
0, 1, 0, 1, 2, 3, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 2, 0,
1, 1, 2, 0, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2, 3, 0, 1, 0, 1,
2, 2, 3, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0,
1, 1, 2, 2, 3, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1,
0, 1, 1, 0, 1, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1,
1, 2, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0,
1, 0, 1, 0, 1, 0, 1, 2, 2, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1,
2, 0, 0, 1), switch_to = c("product1", NA, "product4", "product1",
NA, "product4", NA, "product2", NA, "product4", "product1",
NA, "product8", NA, "product5", NA, "product7", NA, "product3",
NA, "product1", "product4", NA, "product8", "product2", "product9",
NA, "product4", NA, "product2", "product4", "product1", "product2",
NA, "product8", "product1", NA, "product7", NA, "product4",
"product1", "product4", "product1", NA, "product4", NA, "product6",
"product8", NA, "product3", NA, "product2", "product3", NA,
"product5", NA, "product2", "product2", "product3", NA, "product4",
"product1", NA, "product3", "product6", NA, "product4", NA,
"product9", NA, "product1", "product4", "product1", NA, "product5",
NA, "product8", "product7", NA, "product4", NA, "product3",
NA, "product4", NA, "product7", "product6", NA, "product5",
NA, "product4", NA, "product7", "product2", NA, "product2",
NA, "product8", "product4", NA, "product5", NA, "product7",
NA, "product3", NA, "product2", "product8", "product2", NA,
"product4", NA, "product4", "product1", "product4", NA, "product5",
NA, "product1", "product8", NA, "product7", NA, "product4",
NA, "product4", "product6", NA, "product4", "product3", "product4",
NA, "product4", "product4", "product1", NA, "product5", NA,
"product4", NA, "product8", "product2", "product8", NA, "product3",
NA, "product4", "product1", "product4", "product1", NA, "product4",
"product1", "product2", "product5", NA, "product2", NA, "product5",
NA, "product4", "product1", NA, "product4", NA, "product4",
"product1", "product4", "product1", "product4", "product1",
NA, "product4", "product8", NA, "product4", NA, "product4",
"product1", NA, "product4", NA, "product3", NA, "product3",
NA, "product4", "product3", NA, "product4", "product2", "product4",
NA, "product3", NA, "product1", "product4", NA, "product4",
NA, "product7", NA, "product3", NA, "product5", NA, "product2",
NA, "product8", NA, "product2", "product1", NA, "product2",
"product3", NA, "product1", "product6", "product8", "product5",
NA, "product4", "product4", "product1", "product4", NA, "product2",
"product7", NA, "product1", "product4", NA, "product5", "product2",
NA, "product5", NA, "product4", NA, "product2", "product5",
NA, "product4", NA, "product4", NA, "product1", NA, "product1",
"product2", "product4", NA, "product4", NA, "product4", "product4",
NA, "product1", "product4", NA, "product2", "product6", NA,
"product2", "product1", NA), next_x = c(1, NA, 1, 2, NA,
1, NA, 1, NA, 1, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1, 2,
NA, 1, 2, 3, NA, 1, NA, 1, 1, 2, NA, NA, 1, 1, NA, 1, NA,
1, 2, 3, 3, NA, 1, NA, 1, 2, NA, 1, NA, 1, NA, NA, 1, NA,
1, 1, NA, NA, 1, 1, NA, 1, 1, NA, 1, NA, 1, NA, 1, 2, 3,
NA, 1, NA, 1, 2, NA, 1, NA, 1, NA, 1, NA, 1, 1, NA, 1, NA,
1, NA, 1, 1, NA, 1, NA, 1, 2, NA, 1, NA, 1, NA, 1, NA, 1,
1, 2, NA, 1, NA, 1, 2, 3, NA, 1, NA, 1, NA, NA, 1, NA, 1,
NA, 1, 2, NA, 1, 2, 2, NA, 1, 1, 2, NA, 1, NA, 1, NA, 1,
2, 3, NA, 1, NA, 1, 2, 3, 3, NA, 1, 1, NA, NA, NA, 1, NA,
1, NA, 1, 1, NA, 1, NA, 1, 1, 2, 2, 3, 3, NA, 1, 1, NA, 1,
NA, 1, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, NA, 1, 2, 2, NA,
1, NA, 1, 2, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA,
1, 1, NA, 1, NA, NA, 1, 1, NA, NA, NA, 1, 1, 2, 2, NA, 1,
1, NA, 1, NA, NA, 1, NA, NA, 1, NA, 1, NA, 1, NA, NA, 1,
NA, 1, NA, 1, NA, 1, 2, NA, NA, 1, NA, 1, 1, NA, 1, NA, NA,
1, 2, NA, 1, 1, NA)), row.names = c(NA, -266L), class = c("tbl_df",
"tbl", "data.frame"))
df_plot %>%
ggplot(aes(x = switch_nr, next_x = next_x, node = produktnamn, next_node = switch_to, fill = factor(produktnamn), label = produktnamn)) +
geom_alluvial(flow.alpha = .5) +
geom_alluvial_text(size = 3, color = "white") +
scale_fill_viridis_d() +
theme_alluvial(base_size = 18)