Reordering phyloseq barplots by sample name

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

1 Like