'manual ordering of lodes and stratum in ggalluvial
I feel like I have gone a bit mad. I am trying use ggalluvial to plot the distribution of organisms across taxonomic groups. I want to control the lode orderings to make things at a lower taxonomic group (aka class) be aligned with the higher taxonomic group (aka phylum).
I wrote code to do this and am now at the position where I think I can plot it. The lodes are calculated as the biggest on top within each taxonomic group. This is easy for phylum, but then for class it is within each phylum the class with the largest number of members is on top.
I have calculated my manual orders and the data in long format and have a column for lode to control the ordering within each value of x and stratum. However when I add in the order = lode (as recommended by the vignette nothing seems to change. And all my lodes are still mixing together and Firmicutes are still in the middle even though they are the biggest group and their lode number is 1 in the dataframe.
library(ggplot2)
library(ggalluvial)
example <- tibble::tribble(
~n, ~pathogen, ~x, ~stratum, ~lode, ~x_order,
1L, 97L, "ncbi_phylum", "Firmicutes", 1L, 1L,
14L, 98L, "ncbi_phylum", "Firmicutes", 1L, 1L,
2L, 99L, "ncbi_phylum", "Firmicutes", 1L, 1L,
2L, 100L, "ncbi_phylum", "Firmicutes", 1L, 1L,
6L, 101L, "ncbi_phylum", "Firmicutes", 1L, 1L,
3L, 102L, "ncbi_phylum", "Firmicutes", 1L, 1L,
18L, 103L, "ncbi_phylum", "Firmicutes", 1L, 1L,
7L, 104L, "ncbi_phylum", "Firmicutes", 1L, 1L,
31L, 105L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 106L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 107L, "ncbi_phylum", "Firmicutes", 1L, 1L,
2L, 108L, "ncbi_phylum", "Firmicutes", 1L, 1L,
4L, 109L, "ncbi_phylum", "Firmicutes", 1L, 1L,
4L, 110L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 111L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 112L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 113L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 114L, "ncbi_phylum", "Firmicutes", 1L, 1L,
2L, 115L, "ncbi_phylum", "Firmicutes", 1L, 1L,
13L, 116L, "ncbi_phylum", "Firmicutes", 1L, 1L,
3L, 117L, "ncbi_phylum", "Firmicutes", 1L, 1L,
6L, 118L, "ncbi_phylum", "Firmicutes", 1L, 1L,
3L, 119L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 120L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 121L, "ncbi_phylum", "Firmicutes", 1L, 1L,
46L, 122L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 123L, "ncbi_phylum", "Firmicutes", 1L, 1L,
30L, 124L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 125L, "ncbi_phylum", "Firmicutes", 1L, 1L,
2L, 126L, "ncbi_phylum", "Firmicutes", 1L, 1L,
2L, 127L, "ncbi_phylum", "Firmicutes", 1L, 1L,
15L, 128L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 129L, "ncbi_phylum", "Firmicutes", 1L, 1L,
5L, 130L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 131L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 132L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 133L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 134L, "ncbi_phylum", "Firmicutes", 1L, 1L,
2L, 135L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 136L, "ncbi_phylum", "Firmicutes", 1L, 1L,
3L, 137L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 138L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 139L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 140L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 141L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 142L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 143L, "ncbi_phylum", "Firmicutes", 1L, 1L,
2L, 144L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 145L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 146L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 147L, "ncbi_phylum", "Firmicutes", 1L, 1L,
8L, 148L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 149L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 150L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 151L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 152L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 153L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 154L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 155L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 156L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 157L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 158L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 159L, "ncbi_phylum", "Firmicutes", 1L, 1L,
6L, 160L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 161L, "ncbi_phylum", "Firmicutes", 1L, 1L,
4L, 162L, "ncbi_phylum", "Firmicutes", 1L, 1L,
3L, 163L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 164L, "ncbi_phylum", "Firmicutes", 1L, 1L,
6L, 165L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 166L, "ncbi_phylum", "Firmicutes", 1L, 1L,
7L, 167L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 168L, "ncbi_phylum", "Firmicutes", 1L, 1L,
2L, 169L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 170L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 171L, "ncbi_phylum", "Firmicutes", 1L, 1L,
10L, 172L, "ncbi_phylum", "Firmicutes", 1L, 1L,
2L, 173L, "ncbi_phylum", "Firmicutes", 1L, 1L,
1L, 321L, "ncbi_phylum", "Tenericutes", 2L, 1L,
1L, 322L, "ncbi_phylum", "Tenericutes", 2L, 1L,
12L, 323L, "ncbi_phylum", "Tenericutes", 2L, 1L,
1L, 324L, "ncbi_phylum", "Tenericutes", 2L, 1L,
1L, 325L, "ncbi_phylum", "Tenericutes", 2L, 1L,
2L, 326L, "ncbi_phylum", "Tenericutes", 2L, 1L,
2L, 327L, "ncbi_phylum", "Tenericutes", 2L, 1L,
5L, 92L, "ncbi_phylum", "Chlamydiae", 3L, 1L,
2L, 93L, "ncbi_phylum", "Chlamydiae", 3L, 1L,
1L, 94L, "ncbi_phylum", "Chlamydiae", 3L, 1L,
1L, 95L, "ncbi_phylum", "Chlamydiae", 3L, 1L,
1L, 123L, "ncbi_class", "Clostridia", 1L, 2L,
30L, 124L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 125L, "ncbi_class", "Clostridia", 1L, 2L,
2L, 126L, "ncbi_class", "Clostridia", 1L, 2L,
2L, 127L, "ncbi_class", "Clostridia", 1L, 2L,
15L, 128L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 129L, "ncbi_class", "Clostridia", 1L, 2L,
5L, 130L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 131L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 132L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 133L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 134L, "ncbi_class", "Clostridia", 1L, 2L,
2L, 135L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 136L, "ncbi_class", "Clostridia", 1L, 2L,
3L, 137L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 138L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 139L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 140L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 141L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 142L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 143L, "ncbi_class", "Clostridia", 1L, 2L,
2L, 144L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 145L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 146L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 147L, "ncbi_class", "Clostridia", 1L, 2L,
8L, 148L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 149L, "ncbi_class", "Clostridia", 1L, 2L,
1L, 97L, "ncbi_class", "Bacilli", 2L, 2L,
14L, 98L, "ncbi_class", "Bacilli", 2L, 2L,
2L, 99L, "ncbi_class", "Bacilli", 2L, 2L,
2L, 100L, "ncbi_class", "Bacilli", 2L, 2L,
6L, 101L, "ncbi_class", "Bacilli", 2L, 2L,
3L, 102L, "ncbi_class", "Bacilli", 2L, 2L,
18L, 103L, "ncbi_class", "Bacilli", 2L, 2L,
7L, 104L, "ncbi_class", "Bacilli", 2L, 2L,
31L, 105L, "ncbi_class", "Bacilli", 2L, 2L,
1L, 106L, "ncbi_class", "Bacilli", 2L, 2L,
1L, 107L, "ncbi_class", "Bacilli", 2L, 2L,
2L, 108L, "ncbi_class", "Bacilli", 2L, 2L,
4L, 109L, "ncbi_class", "Bacilli", 2L, 2L,
4L, 110L, "ncbi_class", "Bacilli", 2L, 2L,
1L, 111L, "ncbi_class", "Bacilli", 2L, 2L,
1L, 112L, "ncbi_class", "Bacilli", 2L, 2L,
1L, 113L, "ncbi_class", "Bacilli", 2L, 2L,
1L, 114L, "ncbi_class", "Bacilli", 2L, 2L,
2L, 115L, "ncbi_class", "Bacilli", 2L, 2L,
13L, 116L, "ncbi_class", "Bacilli", 2L, 2L,
3L, 117L, "ncbi_class", "Bacilli", 2L, 2L,
6L, 118L, "ncbi_class", "Bacilli", 2L, 2L,
3L, 119L, "ncbi_class", "Bacilli", 2L, 2L,
1L, 120L, "ncbi_class", "Bacilli", 2L, 2L,
1L, 121L, "ncbi_class", "Bacilli", 2L, 2L,
46L, 122L, "ncbi_class", "Bacilli", 2L, 2L,
1L, 157L, "ncbi_class", "Negativicutes", 3L, 2L,
1L, 158L, "ncbi_class", "Negativicutes", 3L, 2L,
1L, 159L, "ncbi_class", "Negativicutes", 3L, 2L,
6L, 160L, "ncbi_class", "Negativicutes", 3L, 2L,
1L, 161L, "ncbi_class", "Negativicutes", 3L, 2L,
4L, 162L, "ncbi_class", "Negativicutes", 3L, 2L,
3L, 163L, "ncbi_class", "Negativicutes", 3L, 2L,
1L, 164L, "ncbi_class", "Negativicutes", 3L, 2L,
6L, 165L, "ncbi_class", "Negativicutes", 3L, 2L,
1L, 166L, "ncbi_class", "Tissierellia", 4L, 2L,
7L, 167L, "ncbi_class", "Tissierellia", 4L, 2L,
1L, 168L, "ncbi_class", "Tissierellia", 4L, 2L,
2L, 169L, "ncbi_class", "Tissierellia", 4L, 2L,
1L, 170L, "ncbi_class", "Tissierellia", 4L, 2L,
1L, 171L, "ncbi_class", "Tissierellia", 4L, 2L,
10L, 172L, "ncbi_class", "Tissierellia", 4L, 2L,
2L, 173L, "ncbi_class", "Tissierellia", 4L, 2L,
1L, 150L, "ncbi_class", "Erysipelotrichia", 5L, 2L,
1L, 151L, "ncbi_class", "Erysipelotrichia", 5L, 2L,
1L, 152L, "ncbi_class", "Erysipelotrichia", 5L, 2L,
1L, 153L, "ncbi_class", "Erysipelotrichia", 5L, 2L,
1L, 154L, "ncbi_class", "Erysipelotrichia", 5L, 2L,
1L, 155L, "ncbi_class", "Erysipelotrichia", 5L, 2L,
1L, 156L, "ncbi_class", "Erysipelotrichia", 5L, 2L,
1L, 321L, "ncbi_class", "Mollicutes", 6L, 2L,
1L, 322L, "ncbi_class", "Mollicutes", 6L, 2L,
12L, 323L, "ncbi_class", "Mollicutes", 6L, 2L,
1L, 324L, "ncbi_class", "Mollicutes", 6L, 2L,
1L, 325L, "ncbi_class", "Mollicutes", 6L, 2L,
2L, 326L, "ncbi_class", "Mollicutes", 6L, 2L,
2L, 327L, "ncbi_class", "Mollicutes", 6L, 2L,
5L, 92L, "ncbi_class", "Chlamydiia", 7L, 2L,
2L, 93L, "ncbi_class", "Chlamydiia", 7L, 2L,
1L, 94L, "ncbi_class", "Chlamydiia", 7L, 2L,
1L, 95L, "ncbi_class", "Chlamydiia", 7L, 2L
)
ggplot(data = example,
aes(x = x_order, y = n, stratum = stratum, alluvium = pathogen)) +
geom_alluvium(aes(order = lode)) +
geom_stratum(aes(order = lode)) +
geom_text(stat = "stratum", aes(label = after_stat(stratum), order = lode)) +
theme_minimal()
#> Warning: Ignoring unknown aesthetics: order
#> Ignoring unknown aesthetics: order
#> Ignoring unknown aesthetics: order

Created on 2022-05-23 by the reprex package (v2.0.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
