Stacked bar chart with OTUs identified to different taxonomic levels

492 views Asked by At

I have microbiome data with OTUs identified to the lowest taxonomic level possible. Some of the OTUs are identified to genus, others to family, order, etc. I need to create a stacked bar chart.

My problem:

  1. If I choose "genus" for my bar chart, anything that is not identified to the genus level is shown as "Unknown" on the chart.
  2. If I choose a higher level, I lose specificity.

How can I create a bar chart with the best taxonomic resolution for my data?

Thank you!

1

There are 1 answers

0
vmc333 On

You could put all genera, families etc. that are less than 5% of the relative abundance into one category. That way any low count OTUs are combined into one category, making your legend cleaner. Or you could remove all your unidentified genera, but it would be wise to make a note on the percentage or number or reads/ taxa that are removed because of this.

Here is some example code for combining otus that are less than whatever percentage that you want.

plot<-transform_sample_counts(phyloseq.object, function(x) 100 * x/sum(x))
glom <- tax_glom(plot, taxrank = 'class')
glom # should list taxa as phyla
data_glom<- psmelt(glom) # create dataframe from phyloseq object

data_glom$class <- as.character(data_glom$class) #convert to character

#simple way to rename phyla with < 5% abundance
data_glom$class[data_glom$Abundance < 0.05] <- "< 5% abundance"

#Count phyla to set color palette
Count = length(unique(data_glom$class)); Count
unique(data_glom$class) #add into line below
data_glom$class <- factor(data_glom$class, levels = c("class", "class", "class",  "class", "< 5% abundance")) 

spatial_plot <- ggplot(data_glom, aes(x=Sample, y=Abundance, fill=class)) + facet_grid(~variable, scales = "free")

spatial_plot + geom_bar(aes(), stat="identity", position="stack") + labs (title = "title, x="label", y= "Relative Abundance (%)", ) + scale_fill_manual(values =c("color","color","color","color","etc.")+ theme (legend.position="bottom", plot.title = element_text(hjust=0.5, size=15),axis.text = element_text(size = 15),axis.title = element_text(size = 15), legend.text=element_text(size=12),legend.title = element_text(size=15)) + guides(fill=guide_legend(nrow=4))