david-barnett / microViz

R package for microbiome data visualization and statistics. Uses phyloseq, vegan and the tidyverse. Docker image available.
https://david-barnett.github.io/microViz/
GNU General Public License v3.0
94 stars 10 forks source link

Sorting taxa by abundance per sample in comp_barplot #44

Closed rsak-384 closed 1 year ago

rsak-384 commented 2 years ago

Hi!

First of all, thank you for creating and sharing this great package, it makes plot generation a lot more easy and convenient.

I have a question regarding the comp_barplot function, more specifically the tax_order argument. Is there any way to sort the lets say 10 taxa by their abundance in each sample, by example, the most abundant taxa would be at the bottom, second most abundant taxa above that and so forth, with optionally the "Other" taxa being at the top, regardless of its abundance. Unfortunately i could not achieve this with the suggested sorting methods.

Sorry if i have missed or misunderstood something in the documentation.

david-barnett commented 2 years ago

Hi!

Firstly thanks for complement, glad you're finding microViz useful 😄

Just to check I understand what you are asking about: You would like the order of the taxa in each bar (each sample) to be dependent on the abundance of each taxon in only that sample. The fill colour of taxa should be consistent across samples, so they can share a single colour legend.

Is that right?

If so, that's quite tricky, I think. As within one ggplot, the order in which bars are stacked is consistent, as it depends on the underlying ordering of the grouping variable's values.

If you don't have that many samples, you could probably achieve this by making one plot per bar and sticking them together with patchwork or cowplot. Keep the colours consistent by specifying a fixed palette using tax_palette

david-barnett commented 2 years ago

Approximately how many samples do you have, and how variable are their compositions? would the top 10 taxa vary wildly between samples, or just their order? My suspicion is that if you have a lot of samples, allowing the bar stacking order to vary by sample might end up looking chaotic.

Interesting question though, thank you 🙂

estebannieto93 commented 1 year ago

Hi

I have a related question, I want to plot the all the taxa that have higher relative abundance than 3% in at least one gruop of samples,. How can I do that?

david-barnett commented 1 year ago

One way could be:

Hope this helps, wrote it on my phone on the train so some details might not be exactly right

david-barnett commented 1 year ago

now more info added on this topic to https://david-barnett.github.io/microViz/articles/web-only/compositions.html

estebannieto93 commented 1 year ago

Thanks a lot for the answer! the package is very useful