joey711 / phyloseq

phyloseq is a set of classes, wrappers, and tools (in R) to make it easier to import, store, and analyze phylogenetic sequencing data; and to reproducibly share that data and analysis with others. See the phyloseq front page:
http://joey711.github.io/phyloseq/
582 stars 187 forks source link

Need help in reordering samples for ggplot #1676

Closed science-chump closed 1 year ago

science-chump commented 1 year ago

Hey all,

Huge phyloseq fan

So I have this plot which I have posted below. The problem is the order in which the samples appear. I know ggplot puts things in alphabetical order, and I want the order to be the row names like in the meta data. I tried adding a column with sample # and telling R the factor orders before making a phyloseq object but it did not help.

This is the bit code I am using to reorder the factors and it appears to work! meta_ECM$ASV_ID <- fct_reorder(meta_ECM$ASV_ID, meta_ECM$Sample.no)

I do a bit of data wrangling from that step. Removed low read samples, merged taxa with fungal database, normalized and finally made a phyloseq object. Next I subset that phyloseq object into the data set I am trying to plot. Only problem is that the order seems to have been lost! (I want aR20 next to bR20 next to eR20, not aR20 next to aR28, bR12...etc)

I tried using the solutions posted #518 and one from stack overflow and neither has solved the problem. Feel like I am missing something little here!

Anyways thanks in advance and for building such a great resource!

Ecm genera FINAL

ycl6 commented 1 year ago

Hi @science-chump

Same as a normal ggplot, you can change the order of the factor that you are using as x in plot_bar() before ploting. In the GlobalPatterns example below, I use sample() to reorder the sample ID column X.SampleID that I am showing in the x-axis. The order will be retained in the resulting plot, in the SampleType panels.

library(phyloseq)
library(ggplot2)
data(GlobalPatterns)

ps = subset_taxa(GlobalPatterns, Phylum == "Verrucomicrobia")
rp = transform_sample_counts(ps, function(OTU) OTU/sum(OTU))

levels(sample_data(rp)$X.SampleID)
#>  [1] "AQC1cm"   "AQC4cm"   "AQC7cm"   "CC1"      "CL3"      "Even1"   
#>  [7] "Even2"    "Even3"    "F21Plmr"  "LMEpi24M" "M11Fcsw"  "M11Plmr" 
#> [13] "M11Tong"  "M31Fcsw"  "M31Plmr"  "M31Tong"  "NP2"      "NP3"     
#> [19] "NP5"      "SLEpi20M" "SV1"      "TRRsed1"  "TRRsed2"  "TRRsed3" 
#> [25] "TS28"     "TS29"

plot_bar(rp, x = "X.SampleID", fill = "Family") + facet_grid(~SampleType, scales = "free_x", space = "free_x")


# Change factor order
set.seed(1)
sample_data(rp)$X.SampleID = factor(sample_data(rp)$X.SampleID, 
                                    levels = sample(levels(sample_data(rp)$X.SampleID), nsamples(rp)))

levels(sample_data(rp)$X.SampleID)
#>  [1] "TS28"     "CC1"      "Even2"    "AQC1cm"   "AQC4cm"   "M11Fcsw" 
#>  [7] "M31Fcsw"  "NP3"      "TRRsed2"  "LMEpi24M" "Even1"    "SLEpi20M"
#> [13] "NP2"      "TRRsed3"  "F21Plmr"  "CL3"      "SV1"      "M11Plmr" 
#> [19] "NP5"      "M31Tong"  "M31Plmr"  "M11Tong"  "TRRsed1"  "TS29"    
#> [25] "AQC7cm"   "Even3"

plot_bar(rp, x = "X.SampleID", fill = "Family") + facet_grid(~SampleType, scales = "free_x", space = "free_x")

Created on 2023-04-17 by the reprex package (v2.0.1)

science-chump commented 1 year ago

Thank you for helping me with this, being able to see the levels within a phyloseq object was a huge help.

My solution was to essentially duplicate my ASV_ID column in the metadata which I called Sample_ID. I think what was happening is that when the phyloseq object gets made, the ASV_ID gets dropped and the sample_names take on what was ASV_ID.

By duplicating this into a new column and ordering it based on my Sample.no column (ordered numerically 1-140), I was able to have all of the levels nicely before the data enters phyloseq!

meta_ECM$Sample.no <- as.numeric(meta_ECM$Sample.no)
meta_ECM$Sample_ID <- fct_reorder(meta_ECM$Sample_ID, meta_ECM$Sample.no)
#meta_ECM <- meta_ECM[order(levels(meta_ECM$Sample.no)),]
str(meta_ECM)
lapply(meta_ECM, levels)