'Fail to plot by group on a phyloseq object generated by R package Divnet
I intend to plot an alpha diversity plot of my samples, which consist of three groups. Alpha diversity is calculated by R package Divnet. It involve using phyloseq as a dependency.
The group of each sample is specified in a sam_data, column "type" in phyloseq object "df_family" "dv" is list composed of diversity estimates and standard errors.
Here is the code I used:
dv$shannon %>% plot(df_family, color = "type", group = df_family@sam_data$type) +
xlab("sample type") +
ylab("Shannon diversity estimate (family level)") +
coord_cartesian(ylim = c(0,5))`
Here is what I get:
The samples were shown independently, instead of clustered as a group

Here is what I intend to get:

Solution 1:[1]
Once you already have your phyloseq object df_family, you can use the function estimate_richness from phyloseq.
You can then join the sample meta data to this data frame of alpha diversities.
Finally, you can use ggplot2 directly to customize your plot accordingly, e.g. to put different sample groups (here SampleType) at your x axis:
library(tidyverse)
library(phyloseq)
# get example phy object
otufile <- system.file("extdata", "GP_otu_table_rand_short.txt.gz", package = "phyloseq")
mapfile <- system.file("extdata", "master_map.txt", package = "phyloseq")
df_family <- import_qiime(otufile, mapfile, trefile, showProgress = FALSE)
alphadiv <-
df_family %>%
estimate_richness() %>%
as_tibble(rownames = "sample_id") %>%
left_join(df_family@sam_data %>% as_tibble(rownames = "sample_id"))
alphadiv
alphadiv %>%
ggplot(aes(x = SampleType, y = Shannon)) +
geom_boxplot()
You can also add additional sample properties in aes e.g. aes(x = SampleType, y = Shannon, color = my_group) if my_group is a sample property column with cells containing f, li, or si.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | danlooo |

