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
101 stars 11 forks source link

Composition barplot sorting by 1 taxon #174

Closed magibc closed 1 month ago

magibc commented 1 month ago

First of all I would like to congratulate your R package.

I have been used your package in the past without any issues but now in another phyloseq object I cannot sort by 1 taxon at genus and species level. For phylum, Class, Order and Family no problem to plot it properly (ordered).

Always, for each taxa rank level I perform tax_glom() from phyloseq to the desired taxa before to plot comp_barplot.

I suspected that could be due to the space in the specie name:

S16_ps_specie %>% tax_fix() %>% tax_agg("Species") %>% tax_transform("compositional") %>% ps_arrange(desc("Mesomycoplasma dispar"), .target = "otu_table") %>% comp_barplot(tax_level = "Species", sample_order = "asis") + coord_flip()

But after I changed the phyloseq object for this specie to be Mesomycoplasma_dispar, but the problem persists.

I use tax_fix() because I have the Error in tax_agg(., "Species") : NAs in tax_table at rank: Species

I do not know what could be the problem to fix this. If it is necessary I can send you the phyloseq objects by private.

Thanks on advance,

Magi

david-barnett commented 1 month ago

Glad you're finding microViz useful, Magi 😄

Can you give an example of your code that is running correctly? E.g ordering by a family

Do I understand correctly that your issue is that you are able to make a bar plot but that the bars are not in the order that you expect? They do not appear to be ordered in descending relative abundance of your target species.

How abundant is the species of interest? Is it possible that many of the samples have zero abundance or otherwise equal abundances?

Cheers David

magibc commented 1 month ago

Thanks Dr. David Barnett for your rapid feedback.

Yes, I give you an example for family that works:

S16_ps_genus %>% tax_fix() %>% tax_agg("Family") %>% tax_transform("compositional") %>% ps_arrange(desc(Microbacteriaceae), .target = "otu_table") %>% comp_barplot(tax_level = "Family", sample_order = "asis") + coord_flip()

Exactly, my issue is that I am able to make the bar plot but the bars are not plotted in descending order. I can confirm that all the samples (20 samplees) shares the more abundant genus and specie.

Thanks again,

Magí

david-barnett commented 1 month ago

I think the issue is that the species name should not be a string for the arranging, i.e. something like this below should work. A tiny modification of your original code :)


S16_ps_specie %>%
tax_fix() %>%
tax_agg("Species") %>%
tax_transform("compositional") %>%
ps_arrange(desc(`Mesomycoplasma dispar`), .target = "otu_table") %>%
comp_barplot(tax_level = "Species", sample_order = "asis") +
coord_flip()
magibc commented 1 month ago

Thank you very much David.

The ' symbol insted of " works as desired to sort for 1 taxa!

I close the issue.

Thanks again!!!

Magí