Subsetting Samples from a Phyloseq Object - meyermicrobiolab/Meyer_Lab_Resources GitHub Wiki
After you have run dada (ASV picking) and taxonomy assignment, sometimes you want to plot figures with only a subset of your samples without re-doing the ASV picking. You can create a new phyloseq object with only certain samples, based on their metadata.
For example, below I wanted to plot only MCAV samples instead of all coral species. My original phyloseq object is called "ps_ra" for phyloseq with relative abundances (to plot stacked bar charts). My new phyloseq object is "ps_ra_mcav" to just get the MCAV samples. "Coral" is the column name in the metadata - be sure this matches exactly to the metadata file. The same with the value "Montastraea cavernosa" - if it isn't spelled/capitalized exactly the same as the metadata file, the command will fail.
IMPORTANT: you can only have one matching pattern at a time. For example, I cannot pick "Montastraea cavernosa" and "Orbicella faveolata" at the same time. You can easily make a new column in your metadata with "keep" or "don't keep", etc. Remake your phyloseq object with the new metadata and then do your subsetting.
ps_ra_mcav = subset_samples(ps_ra, Coral == "Montastraea cavernosa") # == indicates the samples to KEEP; use != to indicate samples to EXCLUDE
This can be modified for any metadata column you have - such as location, collection date, etc. Just make sure your spelling and capitalization match correctly.