Closed padpadpadpad closed 2 years ago
HI @padpadpadpad and thanks for raising the issue. I'm about to be offline for a couple of days but i have copied your code and will try to resolve this later this week!
I think i understand what you're trying to do, but please correct me if not: Every class is connected to exactly one phylum, and you want the classes to be ordered in such a way that, for example, if Tenericutes is the bottom-most phylum at the left axis, then Mollicutes is the bottom-most class, since it is the only one associated with Tenericutes; and so on.
The way you've constructed your diagram, the lodes are actually the pathogens, while the strata are the phyla (at the left axis) and the classes (at the right axis). So what you actually want to do is control the ordering of the strata. This should be done by making the stratum variable a factor rather than a character. The following code produces what i think you want, though the alluvia are not clear because they are all the same color. I also convert the axis variable (x
) to a factor to ensure that everything is correctly sorted.
example$x <- factor(example$x, levels = unique(example$x))
stratum_lev <- unique(example[order(example$x, example$lode), ]$stratum)
example$stratum <- factor(example$stratum, levels = stratum_lev)
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()
Please let me know if this resolves the issue (and, if so, feel free to close the issue)!
I will check this soon and get back to you. Lost track of this update. Thanks for responding so fast.
This worked a treat. Still playing with it but your code was amazing. Many thanks.
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.
Created on 2022-05-23 by the reprex package (v2.0.1)