# ggplot2: Relative Abundances Whisker Plot for Individual Phyla across samples

**URL:** <https://forum.qiime2.org/t/ggplot2-relative-abundances-whisker-plot-for-individual-phyla-across-samples/32177>\
**Category:** Other Bioinformatics Tools\
**Tags:** r\
**Created:** [December 22, 2024, 2:07am UTC](https://forum.qiime2.org/t/ggplot2-relative-abundances-whisker-plot-for-individual-phyla-across-samples/32177 "2024-12-22T02:07:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gordon\_Zheng](https://forum.qiime2.org/user_avatar/forum.qiime2.org/gordon_zheng/32/19875_2.png) [@Gordon\_Zheng](https://forum.qiime2.org/u/Gordon_Zheng)\
**Post date:** [December 22, 2024, 2:07am UTC](https://forum.qiime2.org/t/ggplot2-relative-abundances-whisker-plot-for-individual-phyla-across-samples/32177/1 "2024-12-22T02:07:44Z")

</div>

Hi all,

I am currently using QZA files and R to produce Relative Abundances plots. Following is the code for a sample:

> library(ggplot2)  
> library(reshape2)  
> library(dplyr)  
> library(cowplot)  
> library(grid)  
> taxa\_data \<- read.csv("phylum.data.csv")  
> head(taxa\_data)  
> colnames(taxa\_data)  
> source \<- c("Normal", "Normal", "Normal", "LDLrKO", "LDLrKO", "LDLrKO", "LDLr HD 0", "LDLr HD 0", "LDLr HD 0")  
> if (length(source) != nrow(taxa\_data)) {  
> stop("The length of the source column does not match the number of rows in taxa\_data.")  
> }  
> taxa\_data$source \<- source  
> taxa\_long \<- melt(taxa\_data, id.vars = c("index", "source"))  
> taxa\_long$value \<- as.numeric(as.character(taxa\_long$value))  
> taxa\_long \<- taxa\_long %\>% filter(!is.na(value))  
> taxa\_long \<- taxa\_long %\>%  
> group\_by(source) %\>%  
> mutate(total = sum(value)) %\>%  
> ungroup() %\>%  
> mutate(percentage = (value / total) \* 100)  
> head(taxa\_long)  
> taxa\_long$variable \<- gsub(" .\*", "", taxa\_long$variable)  
> p \<- ggplot(taxa\_long, aes(x = source, y = percentage, fill = variable)) +  
> geom\_bar(stat = "identity", show.legend = FALSE) +  
> theme\_minimal() +  
> labs(x = "Treatment", y = "Relative Abundance (%)") +  
> theme(axis.text.x = element\_text(angle = 90, hjust = 1))  
> print(p)  
> get\_legend \<- function(myggplot) {  
> tmp \<- ggplot\_gtable(ggplot\_build(myggplot))  
> leg \<- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")  
> legend \<- tmp$grobs[[leg]]  
> return(legend)  
> }  
> [image]

I want to produce something more like Figure B shown below: [image]

Any suggestions?

---

<div class="post-metadata">

**Author:** ![Gordon\_Zheng](https://forum.qiime2.org/user_avatar/forum.qiime2.org/gordon_zheng/32/19875_2.png) [@Gordon\_Zheng](https://forum.qiime2.org/u/Gordon_Zheng)\
**Post date:** [December 22, 2024, 5:28am UTC](https://forum.qiime2.org/t/ggplot2-relative-abundances-whisker-plot-for-individual-phyla-across-samples/32177/2 "2024-12-22T05:28:26Z")

</div>

Mine:

 ![Rplot01](https://forum-qiime2-org.s3.dualstack.us-west-2.amazonaws.com/original/3X/5/a/5a810a3807ec4d3a9f633562ac3a3a31b3a110f7.png)

Want:

 ![Comparison-relative-abundance-of-microbiota-at-the-genus-level-among-the-4-groups_W640](https://forum-qiime2-org.s3.dualstack.us-west-2.amazonaws.com/original/3X/0/7/07cd33465c6ea76e5050e192ee4ca6f2bd23d4b3.jpeg)

---

<div class="post-metadata">

**Author:** ![salias](https://forum.qiime2.org/user_avatar/forum.qiime2.org/salias/32/18594_2.png) [@salias](https://forum.qiime2.org/u/salias)\
**Post date:** [December 22, 2024, 2:21pm UTC](https://forum.qiime2.org/t/ggplot2-relative-abundances-whisker-plot-for-individual-phyla-across-samples/32177/3 "2024-12-22T14:21:53Z")

</div>

Hi @Gordon_Zheng and welcome to the :qiime2\_square: Forum!

What do you want exactly from Figure B? The theme, separating the conditions, or something else?

For the theme, you can check all [ggplot pre-built themes](https://ggplot2.tidyverse.org/reference/ggtheme.html) and see if any of them fit your needs. You can also [build one yourself](https://ggplot2.tidyverse.org/reference/theme.html).

For the grouped plots, you can generate one plot per condition and then use [patchwork](https://patchwork.data-imaginist.com/).

Best,

Sergio

---

<div class="post-metadata">

**Author:** ![Gordon\_Zheng](https://forum.qiime2.org/user_avatar/forum.qiime2.org/gordon_zheng/32/19875_2.png) [@Gordon\_Zheng](https://forum.qiime2.org/u/Gordon_Zheng)\
**Post date:** [December 22, 2024, 5:38pm UTC](https://forum.qiime2.org/t/ggplot2-relative-abundances-whisker-plot-for-individual-phyla-across-samples/32177/4 "2024-12-22T17:38:17Z")

</div>

I am looking for separate plots, like Figure B and the +-SE. I combined all the samples in my figure, but I am not sure how to separate them based on their phylum and calculate +-SE. I attempted an R-code to calculate standard error but my relative abundance goes up to 3000%

---

<div class="post-metadata">

**Author:** ![salias](https://forum.qiime2.org/user_avatar/forum.qiime2.org/salias/32/18594_2.png) [@salias](https://forum.qiime2.org/u/salias)\
**Post date:** [December 24, 2024, 11:04am UTC](https://forum.qiime2.org/t/ggplot2-relative-abundances-whisker-plot-for-individual-phyla-across-samples/32177/5 "2024-12-24T11:04:11Z")

</div>

Hi @Gordon_Zheng ,

> [@Gordon\_Zheng](#):
>
> phylum.data.csv

As far as I understood, this CSV file contains relative abundance of features annotated at the phylum level. Each row is a sample and each column is a phylum. I understand you want a plot for each phylum where the X-axis contains your "source" variable (i.e., "Normal", "LDLrKO" and "LDLr HD 0").

Figure B (as well as your figure) are boxplots, that show the distribution of data. You don't need to calculate SE in order to generate them.

What I would do is:

1. Convert your source column to factor using `as.factor(taxa_data$source)`

2. Then, for each phylum you want to plot:

3. Once you generate all the plots you want, use patchwork:

```R
combined_plot <- (plot_firmicutes | plot_actinobacteria) / 
                 (plot_proteobacteria | plot_bacteroidetes)

```

Sergio

---

<div class="post-metadata">

**Author:** ![colinbrislawn](https://forum.qiime2.org/user_avatar/forum.qiime2.org/colinbrislawn/32/6221_2.png) [@colinbrislawn](https://forum.qiime2.org/u/colinbrislawn)\
**Post date:** [December 25, 2024, 5:40am UTC](https://forum.qiime2.org/t/ggplot2-relative-abundances-whisker-plot-for-individual-phyla-across-samples/32177/6 "2024-12-25T05:40:29Z")

</div>

Welcome to the forums!

For separate panels, you can use the ggplot function `facet_wrap()`

> **[Wrap a 1d ribbon of panels into 2d — facet\_wrap](https://ggplot2.tidyverse.org/reference/facet_wrap.html)**
>
> facet\_wrap() wraps a 1d sequence of panels into 2d. This is generally
> a better use of screen space than facet\_grid() because most
> displays are roughly rectangular.

For the stat tests in the graph itself, SE, comparison of groups, etc., I use this package.

> **[ggplot2 Based Publication Ready Plots](https://rpkgs.datanovia.com/ggpubr/)**
>
> The ggplot2 package is excellent and flexible for elegant data
> visualization in R. However the default generated plots requires some formatting
> before we can send them for publication. Furthermore, to customize a ggplot,
> the syntax is...

Making a complex graph requires more complex code, but it can be done! Let us know if you have questions along the way.
