I´m trying to alter a basic alluvial plot in order to fit my data and I have two separate issues:
- two of the categories (A1 & A2) within my main variable (index) are much much larger in relation to the others which makes the rest of the outcomes to look squeezed in.
- the second variable I´m using has also many categories in it which makes the legend too big to fit withing the margin of the graph
I use the following code_
ggplot(data = sni_long,
aes(axis1 = index, axis2 = Avdelning, y = freq)) +
geom_alluvium(aes(fill = Avdelning)) +
geom_stratum() +
geom_text(stat = "stratum",
aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Index", "Avdelning"),
expand = c(0.15, 0.05)) +
theme_void()
when I tried the following code hoping to split my axis1 variable (index) in half:
ggplot(data = sni_long,
aes(axis1 = index,
axis2 = Avdelning,
axis3 = index,
y = freq)) +
geom_alluvium(aes(fill = Avdelning)) +
geom_stratum() +
geom_text(stat = "stratum",
aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("Index", "Avdelning"),
expand = c(0.15, 0.05)) +
theme_void()
I only managed to mirror the graph:
and as you see in both cases, the legend falls outside the scope of the graph :S
Edit: dataset here:
sni_long<- structure(list(Avdelning = c("A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "L", "M", "N", "P", "Q", "R", "S", "A", "B", "C",
"D", "E", "F", "G", "H", "I", "J", "L", "M", "N", "P", "Q", "R",
"S", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "L", "M",
"N", "P", "Q", "R", "S", "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "L", "M", "N", "P", "Q", "R", "S", "A", "B", "C", "D",
"E", "F", "G", "H", "I", "J", "L", "M", "N", "P", "Q", "R", "S",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "L", "M", "N",
"P", "Q", "R", "S", "A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "L", "M", "N", "P", "Q", "R", "S", "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "L", "M", "N", "P", "Q", "R", "S", "A",
"B", "C", "D", "E", "F", "G", "H", "I", "J", "L", "M", "N", "P",
"Q", "R", "S", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"L", "M", "N", "P", "Q", "R", "S"), index = c("A1", "A1", "A1",
"A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1",
"A1", "A1", "A1", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2",
"A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A3", "A3",
"A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3",
"A3", "A3", "A3", "A3", "B1", "B1", "B1", "B1", "B1", "B1", "B1",
"B1", "B1", "B1", "B1", "B1", "B1", "B1", "B1", "B1", "B1", "B2",
"B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2",
"B2", "B2", "B2", "B2", "B2", "B3", "B3", "B3", "B3", "B3", "B3",
"B3", "B3", "B3", "B3", "B3", "B3", "B3", "B3", "B3", "B3", "B3",
"C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1",
"C1", "C1", "C1", "C1", "C1", "C1", "C2", "C2", "C2", "C2", "C2",
"C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2", "C2",
"C2", "D1", "D1", "D1", "D1", "D1", "D1", "D1", "D1", "D1", "D1",
"D1", "D1", "D1", "D1", "D1", "D1", "D1", "D2", "D2", "D2", "D2",
"D2", "D2", "D2", "D2", "D2", "D2", "D2", "D2", "D2", "D2", "D2",
"D2", "D2"), freq = c(91, 15, 958, 82, 74, 1338, 1296, 591, 295,
905, 286, 1074, 751, 237, 602, 104, 101, 3896, 96, 6585, 274,
328, 12885, 14708, 4308, 4647, 5685, 3808, 13722, 4656, 2066,
3466, 1592, 2425, 16, 7, 430, 44, 28, 305, 386, 154, 8, 268,
79, 295, 129, 71, 160, 12, 16, 29, 5, 264, 22, 20, 410, 405,
192, 108, 348, 78, 400, 324, 67, 258, 33, 32, 10, 4, 172, 14,
13, 214, 270, 112, 45, 191, 38, 236, 187, 52, 167, 11, 24, 18,
7, 229, 20, 14, 161, 215, 79, 6, 176, 45, 174, 89, 38, 93, 6,
9, 72, 3, 138, 8, 9, 464, 310, 178, 190, 235, 81, 385, 252, 61,
147, 40, 47, 39, 2, 44, 2, 2, 145, 104, 37, 23, 54, 41, 101,
36, 11, 25, 8, 13, 18, 1, 137, 12, 16, 268, 212, 123, 39, 218,
39, 219, 178, 48, 133, 22, 13, 13, 0, 67, 7, 12, 206, 159, 96,
38, 160, 26, 160, 160, 36, 120, 22, 9)), row.names = c(NA, -170L
), class = c("tbl_df", "tbl", "data.frame"))