You have a barplot, but your samples are out of order; perhaps you want to reorder them by an experimental condition BUT you donβt want to merge samples. This code will conserve the original sample data by individual and rearrange samples on the x-axis.
It uses both melt and glom (which we have seen in some other code recipes here), which alters the original dataset so it should be used on its own and not carried over to a different analysis. One advantage of this is that the object will become much more lightweight - creating a barplot using a phyloseq object that has a lot of ASVs or samples will be much more computationally taxing than using the glommed/melted dataframe.
As you can see, you will also need to provide the exact order you want the samples in. I did this by reordering the samples using the sort function in a spreadsheet software (Excel in this case). @gauriipaul you have this file in your dataset folder on the drive already (Terrazas.xlsx, under the samdf tab). I sorted by multiple levels in this case, to group samples by treatment, then within treatment by day, and within the day category by the subject.
You then have to take this list and make a vector out of it. Itβs a little tedious, but easy:
#Rearranging samples in barplotdesiredOrder β c(β701Cβ, β702Cβ, β705Cβ, β706Cβ, β916Cβ, β917Cβ, β918Cβ, β919Cβ, β920Cβ,β921Cβ, β922Cβ, β923Cβ, β924Cβ, β925Cβ, β926Cβ, β927Cβ, β928Cβ, β929Cβ,β930Cβ, β701C2β, β702C2β, β705C2β, β706C2β, β918C2β, β919C2β, β921C2β,β922C2β, β923C2β, β924C2β, β925C2β, β926C2β, β927C2β, β928C2β, β929C2β,β732Nβ, β734Nβ, β735Pβ, β736Nβ, β737Pβ, β739Nβ, β740Pβ, β741Pβ, β742Pβ,β743Pβ, β744Pβ, β947Nβ, β948Nβ, β949Nβ, β950Nβ, β951Nβ, β952Pβ, β953Pβ,β954Pβ, β955Nβ, β956Nβ, β957Pβ, β958Pβ, β960Nβ, β962Pβ, β963Pβ, β966Nβ,β967Nβ, β970Pβ, β971Pβ, β972Pβ, β973Pβ, β975Nβ, β978Pβ, β980Nβ, β981Pβ,β982Nβ, β983Nβ, β984Nβ, β985Pβ, β986Nβ, β987Pβ, β988Pβ, β989Nβ, β990Pβ,β732N2β, β735P2β, β736N2β, β737P2β, β739N2β, β741P2β, β742P2β, β947N2β,β948N2β, β949N2β, β950N2β, β951N2β, β952P2β, β953P2β, β954P2β, β955N2β,β956N2β, β957P2β, β958P2β, β960N2β, β962P2β, β963P2β, β970P2β, β971P2β,β972P2β, β973P2β, β975N2β, β978P2β, β979N2β, β981P2β, β984N2β, β985P2β,β986N2β, β988P2β, β989N2β, β990P2β)
glom β tax_glom(oc, taxrank = βPhylumβ)glom # Squish all ASVs of the same Phylum together
melt β psmelt(glom) # create dataframe from phyloseq object
melt$sample_id β factor(melt$sample_id, levels = desiredOrder) #Reorder samples (by reordering factors) of the melt dataframe
p β ggplot(data=melt, aes(x=sample_id, y=Abundance, fill=Phylum))
p + geom_bar(aes(), stat=βidentityβ, position=βstackβ) +theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
Here is the final output and the original example, using Class. Youβll notice that in the glom/melted version the NA category gets turned into empty space.
Original
Made with glom/melt