YuLab-SMU / MicrobiotaProcess

:microbe: A comprehensive R package for deep mining microbiome
https://www.sciencedirect.com/science/article/pii/S2666675823000164
182 stars 37 forks source link

How to rearrange samples on barplot based on abundance? #72

Open Jawen-D opened 2 years ago

Jawen-D commented 2 years ago

Hi,

I need help in arranging my samples based on their abundance (highest to lowest). I followed this script:

classtaxa <- get_taxadf(obj=ps, taxlevel=3) classtaxa1 <- get_alltaxadf(obj=ps, taxlevel=3)

pclass <- ggbartax(obj=classtaxa, facetNames="Group") + xlab(NULL) + ylab("relative abundance (%)") + scale_fill_manual(values=c(colorRampPalette(RColorBrewer::brewer.pal(12,"Set3"))(31))) + guides(fill= guide_legend(keywidth = 0.5, keyheight = 0.5)) pclass

I was able to produce this bar plot: image

however the samples are arranged alphabetically by default. Is there a way to rearrange them according to their abundance(highest to lowest)?

xiangpin commented 2 years ago

We have updated the MicrobiotaProcess based on the tidy framework. We recommend using the newest released version (v.1.8.2) of MicrobiotaProcess to analyze the microbiome dataset.

  1. wanted to visualize the taxonomy based on a specific level and arrange the samples based on their abundance You can use mp_plot_abundance to visualize it.
    
    library(MicrobiotaProcess)
    MicrobiotaProcess v1.9.3.993 For help:
    https://github.com/YuLab-SMU/MicrobiotaProcess/issues

If you use MicrobiotaProcess in published research, please cite the paper:

S Xu, L Zhan, W Tang, Z Dai, L Zhou, T Feng, M Chen, S Liu, X Fu, T Wu, E Hu, G Yu. MicrobiotaProcess: A comprehensive R package for managing and analyzing microbiome and other ecological data within the tidy framework. 04 February 2022, PREPRINT (Version 1) available at Research Square [https://doi.org/10.21203/rs.3.rs-1284357/v1]

This message can be suppressed by: suppressPackageStartupMessages(library(MicrobiotaProcess))

data(mouse.time.mpse) mouse.time.mpse

A MPSE-tibble (MPSE object) abstraction: 4,142 × 11

OTU=218 | Samples=19 | Assays=Abundance | Taxonomy=Kingdom, Phylum, Class, Order, Family, Genus, Species

OTU Sample Abundance time Kingdom Phylum Class Order Family Genus Species

1 OTU_1 F3D0 579 Early k__Bac… p__Ba… c__B… o__B… f__Mu… g__u… s__un_… 2 OTU_2 F3D0 345 Early k__Bac… p__Ba… c__B… o__B… f__Mu… g__u… s__un_… 3 OTU_3 F3D0 449 Early k__Bac… p__Ba… c__B… o__B… f__Mu… g__u… s__un_… 4 OTU_4 F3D0 430 Early k__Bac… p__Ba… c__B… o__B… f__Mu… g__u… s__un_… 5 OTU_5 F3D0 154 Early k__Bac… p__Ba… c__B… o__B… f__Ba… g__B… s__un_… 6 OTU_6 F3D0 470 Early k__Bac… p__Ba… c__B… o__B… f__Mu… g__u… s__un_… 7 OTU_7 F3D0 282 Early k__Bac… p__Ba… c__B… o__B… f__Mu… g__u… s__un_… 8 OTU_8 F3D0 184 Early k__Bac… p__Ba… c__B… o__B… f__Ri… g__A… s__un_… 9 OTU_9 F3D0 45 Early k__Bac… p__Ba… c__B… o__B… f__Mu… g__u… s__un_… 10 OTU_10 F3D0 158 Early k__Bac… p__Ba… c__B… o__B… f__Mu… g__u… s__un_… # … with 4,132 more rows # ℹ Use `print(n = ...)` to see more rows ``` if you want to rarefy the community, you can use mp_rrarefy first (The column of specified .abundance should be integer). All samples will be rarefied to the smallest depth number in all samples. ``` p <- mouse.time.mpse %>% mp_rrarefy(.abundance=Abundance) %>% mp_plot_abundance(.abundance=RareAbundance, .group=time, taxa.class=Phylum, topn=30, width=3/5) p ``` ![XX1](https://user-images.githubusercontent.com/17870644/195612386-f40adea5-ea79-4d18-929f-575e2ce5bd1b.PNG)

if you want to adjust the order of sample names, you can re-factor the Sample column.

> yourwant <- sample(colnames(mouse.time.mpse))
> p$data %<>% dplyr::mutate(Sample=factor(Sample, levels=yourwant))
> p

XX

if you want to visualize the original abundance based on the .abundance column, you can add force=T and relative=F

>f <- mouse.time.mpse %>% mp_plot_abundance(.abundance=Abundance, .group=time, taxa.class=Phylum, topn=30, width=3/5, force=T, relative=F)
> f

XX2

  1. Also, I was able to create a PCA and PCoA plot. How can I add the sample names on each dot in plot?

you can use mp_cal_pca or mp_cal_pcoa and mp_plot_ord the visualize the result. you can specify the show.sample=TRUE in the mp_plot_ord to add the sample names. You can use ?mp_plot_ord to view the help information. Or you can run example('mp_plot_ord', run.dontrun=T) to do run the function quickly.

> f2 <- mouse.time.mpse %>% mp_cal_pcoa(.abundance=Abundance, distmethod='aitchison', pseudocount=1) %>% mp_plot_ord(.group=time, show.sample=T)
> f2

XX3

  1. Lastly, I was able to make violin plots for the alpha diversity matrices. How can I make same plot for the abundance between the two study groups?

you can use mp_cal_alpha and mp_plot_alpha to do this.

> f3 <- mouse.time.mpse %>% mp_rrarefy(.abundance=Abundance) %>% mp_cal_alpha(.abundance=RareAbundance) %>% mp_plot_alpha(.group=time, .alpha=c(Observe, Shannon, Pielou))
> library(ggplot2)
> f3 + scale_fill_manual(values=c('deepskyblue', 'orange')) + scale_color_manual(values=c('deepskyblue', 'orange'))

XX4

More vignettes can be found in this.

taekwonleo-yuhao commented 2 years ago

Is there any easy way to assign a universal color pattern for taxa between different MPSEs?

I need to make a consistent figure legend for different plots. 0000aa

Thanks!