#' @title Taxonomic Composition Plot boxplot
#' @description Plot taxon abundance for samples.
#' @param x \code{\link{phyloseq-class}} object
#' @param taxonomic.level Merge the OTUs (for phyloseq object) into a higher taxonomic level. This has to be one from colnames(tax_table(x)).
#' @param top.otu Top number of taxa to plot.
#' @param VariableA Specify main variable of interest. This should be one of the variables in sample_variables(x).
#' @param title title for the plot
#' @param color any of the palette supported by ggpubr/RColorBrewer packages or user specified as c("red", "blue").
#' @return A \code{\link{ggplot}} plot object.
#' @export
#' @examples \dontrun{
#' # Example data
#' library(microbiomeutilities)
#' library(RColorBrewer)
#' data("biogeogut")
#' ps1 <- biogeogut
#' pn <- plot_taxa_boxplot(ps1,
#' taxonomic.level = "Phylum",
#' top.otu = 3, VariableA = "SampleType",
#' title = "Rel plot", color = "Set2")
#' print(pn)
#'
#' }
#' @keywords utilities
plot_taxa_boxplot <- function(x, taxonomic.level, top.otu, VariableA, title, color = NULL){
Abundance <- NULL
if (!is.null(x@phy_tree)){
message("For plotting purpuses the phy_tree will be removed")
x@phy_tree = NULL
}
else {
message("The phy_tree slot is empty, easy to make the plot")
}
taxic <- as.data.frame(x@tax_table);
otudf <- as.data.frame(otu_table(x));
taxic$OTU <- row.names(otudf);
taxmat <- as.matrix(taxic);
new.tax <- tax_table(taxmat);
tax_table(x) <- new.tax;
# Merge the taxa at a higher taxonomic level
if (!taxonomic.level == "OTU") {
x <- aggregate_taxa(x, taxonomic.level, top = top.otu)
}
x1 <- transform(x, "compositional")
x.df0 <- suppressWarnings(suppressMessages(phy_to_ldf(x1, transform.counts = NULL)))
p <- ggplot(x.df0, aes(x =x.df0[,taxonomic.level],
y=Abundance,
fill = x.df0[,VariableA]))
p <- p + geom_boxplot(position = position_dodge(1)) +
geom_point(position = position_dodge(width = 0.75),
aes(group = x.df0[, VariableA]))
p <- p + ggtitle(title) + theme_bw() +
theme(axis.text.x = element_text(face ="italic",
angle = 90))
p <- p + ylab("Relative abundance") + xlab(taxonomic.level) +
scale_fill_brewer(VariableA,
palette = color)
return(p)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.