Creation of barplot with excel

Hi everyone,

I exported my taxa-bar-plot data as a CSV file to my computer to create a barplot by groups rather than by sample ID. I am having trouble creating the same type of barplots that we get in QIIME2 on excel. Does anyone had tried to do this before?

My goal is to create a relative-frequency barplot by groups.

I look forward to your answer.

Be safe out there :smiley:

Hi!
As alternative, you can group your feature table: click
And then create a taxa barplot with grouped table

Hi @cortegas,

If I understand your question correctly, you still want to include all samples, but grouped by a certain column in the metadata. If this is the case and if you don’t mind a Python solution instead of Excel, check out the taxa_abundance_bar_plot method in the Dokdo package I wrote (https://github.com/sbslee/dokdo). Nice thing about the method is that it works directly with the visualization file from QIIME 2 (e.g. taxa-bar-plots.qzv). Below I will attach a working example using the taxa-bar-plots.qzv file from the “Moving Pictures” tutorial.

Hope this helps.

import sys
sys.path.append('/Users/seungbeenlee/Desktop/dokdo')
import api

import matplotlib.pyplot as plt
%matplotlib inline

fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(15, 15))
kwargs = {'level': 6, 'count': 8, 'show_legend': True, 'legend_short': True, 'label_columns': ['sample-id']}
qzv_file = 'moving-pictures-tutorial/taxa-bar-plots.qzv'
api.taxa_abundance_bar_plot(qzv_file, include_samples={'body-site': ['gut']}, title='gut', ax=ax1, **kwargs)
api.taxa_abundance_bar_plot(qzv_file, include_samples={'body-site': ['tongue']}, title='tongue', ax=ax2, **kwargs)
api.taxa_abundance_bar_plot(qzv_file, include_samples={'body-site': ['left palm']}, title='left palm', ax=ax3, **kwargs)
api.taxa_abundance_bar_plot(qzv_file, include_samples={'body-site': ['right palm']}, title='right palm', ax=ax4, **kwargs)

plt.tight_layout()
plt.savefig('taxa-bar-plots.png')

2 Likes

Hi Carmen,

I’ve not done it in excel, but you can also customize your taxa barplot in R. You can import your metadata, feature table and taxonomy into R following the qiime2R tutorial. There’s another R package, MicrobeR, developed by the same author, which allows you to make grouped taxa barplot using the imported data.

I also wrote an R function based on the MicrobeR pakcage, which allows you to quickly make publication-quality taxa barplots. If you’re interested, check the code I used for my project.

3 Likes

Thank you very much, both of you, for the input and the detailed responses. I do not dominate phyton, but I will try it with the R code.

I tried to do this, but I get stuck on the step going from .qza to .qzv. Any suggestions?

Hi,

Making taxa barplot in R does not require converting .qza files to .qzv files. I’m not sure which step you were stuck on. Below is an example using metadata, feature table and taxonomy file from the “Moving Picture” tutorial.

> # Load packages
> library(tidyverse) 
> library(RColorBrewer)
> library(cowplot)
> library(qiime2R) 
> library(MicrobeR) 
> 
> # Load function
> source("make_taxa_barplot.R")
> 
> # Import data ##################################################################
> # Metadata
> metadata <- read_tsv("metadata.tsv", comment = "#q2") 
> 
> metadata <- metadata %>% 
>   rename_at(vars(contains("-")), ~gsub("-", "_", .x)) %>% # replace dash with underscore 
>   rename(SampleID = "sample_id") # use "SampleID" as sample identifier 
> 
> # Feature table
> table <- read_qza("table.qza")
> 
> count_tab <- table$data %>% as.data.frame() 
> 
> # Taxonomy
> taxonomy <- read_qza("taxonomy.qza")
> 
> tax_tab <- taxonomy$data %>% 
>   as.data.frame() %>%
>   separate(Taxon, sep = ";", c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")) %>% 
>   column_to_rownames("Feature.ID") %>%
>   select(-Confidence)
> 
> # Taxa barplot #################################################################
> # Phylum-level ----------------------------------------------------------------- 
> # Collapse feature table at phylum level
> tab_phy <- Summarize.Taxa(count_tab, tax_tab)$Phylum %>%
>   # the following 4 lines of code prune the taxonomy to contain phylum names only
>   rownames_to_column("tax") %>%
>   filter(!grepl("NA", tax)) %>% # remove taxa without kingdom or phylum level annotation
>   mutate(tax = gsub("\\[|\\]", "", tax), # remove square brackets
>          tax = gsub("k__.*p__", "", tax)) %>%
>   column_to_rownames("tax")
> 
> # Plot taxa on individual basis
> make_taxa_barplot(table = tab_phy, 
>                   metadata = metadata, 
>                   group_by = body_site, 
>                   ntaxa = 10, # number of taxa to display
>                   nrow = 1, # nrow for facetting
>                   plot_mean = FALSE, # plot mean relative abundance within each group
>                   cluster_sample = FALSE, # cluster samples based on similarity
>                   sample_label = SampleID,
>                   italize_taxa_name = TRUE)
> 
> ggsave("taxa_barplot_moving_picture_individual.png")

> # Plot taxa using group means
> make_taxa_barplot(table = tab_phy, 
>                   metadata = metadata, 
>                   group_by = body_site, 
>                   ntaxa = 10,
>                   nrow = 1,
>                   plot_mean = TRUE, 
>                   cluster_sample = FALSE,
>                   sample_label = body_site,
>                   italize_taxa_name = TRUE)
> 
> ggsave("taxa_barplot_moving_picture_mean.png")

You can download the demo files and play with the code. You may need to modify the code to make it work for your project. demo.zip (235.8 KB)

5 Likes

Thank you!!!

I am stuck at the portion where I need to separate the columns by taxonomy level.

#Taxonomy
taxonomy = read_qza(“taxonomy.qza”)
tax_tab = taxonomy$data %>% as.data.frame()
separate(Taxon, sep = “;”, c(“Kingdom”, “Phylum”, “Class”, “Order”,“Family”, “Genus”, “Species”)) %>% column_to_rownames(“Feature.ID”) %>% select(-Confidence)

Error in separate(taxon, sep = “;”, c(“Kingdom”, “Phylum”, “Class”, “Order”, :
object ‘taxon’ not found

But, when I check the name of the column in my taxonomy object, appears Taxon:

head(tax_tab)

                   Feature.ID

1 fbbeae3dceada6928b2cdc472f5d9901
2 2f65298490e0fe48b880145471ec54eb
3 eca7ea65dbc52bdbc9ade7f98e109a1e
4 a1b0b267e7b0a74b30da31a1dbfc5ff2
5 c5921056b376e8b1f075a6d1f18b6ee4
6 a9cce45b3721fc88e16c3bde3a75c1aa
Taxon
1 k__Bacteria; p__Verrucomicrobia; c__Verrucomicrobiae; o__Verrucomicrobiales; f__Verrucomicrobiaceae; g__Akkermansia; s__muciniphila
2 k__Bacteria; p__Firmicutes; c__Bacilli; o__Lactobacillales; f__Lactobacillaceae; g__Lactobacillus; s__salivarius
3 k__Bacteria; p__Bacteroidetes; c__Bacteroidia; o__Bacteroidales; f__Porphyromonadaceae; g__Parabacteroides
4 k__Bacteria; p__Firmicutes; c__Erysipelotrichi; o__Erysipelotrichales; f__Erysipelotrichaceae; g__Clostridium; s__cocleatum
5 k__Bacteria; p__Firmicutes; c__Bacilli; o__Lactobacillales; f__Lactobacillaceae; g__Lactobacillus; s__salivarius
6 k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__; g__; s__
Confidence
1 1.0000000
2 0.9060246
3 1.0000000
4 0.9785728
5 0.8086647
6 0.9863715

I know this is not a complex code, but is being a pain!!!

2 Likes

It can be frustrating to use a new tool. Hang in there. You’ll make it! :blush:

You missed a pipe (%>%) after this line of code.

Try this:

tax_tab = taxonomy$data %>%
as.data.frame() %>%
separate(Taxon, sep = “;”, c(“Kingdom”, “Phylum”, “Class”, “Order”,“Family”, “Genus”, “Species”)) %>%
column_to_rownames(“Feature.ID”) %>%
select(-Confidence)

3 Likes

It worked! Thank you. For sure, getting used to R still.

Which package is the function “make_taxa_barplot()”
I see that you loaded at the beginning, but I assume that’s something that you wrote or that’s from one of the packages?

Thanks for all the feedback! :smiley: :nerd_face:

make_taxa_barplot is a function I wrote based on the MicrobeR package. It has not been fully tested yet, but it does the work most of the time.

I was able to run the taxa_barplot as an alternative to your function. However, I still can see the sample ID at the bottom of the figure.

taxa_barplot(features = tab_phy, metadata = metadata, category = “group”, normalize = “percent”, ntoplot = 7)

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.