R/box_plot.R

Defines functions taxa_boxplot

Documented in taxa_boxplot

#' @title Function to make boxplots of taxa counts or relative abundance
#' @name taxa_boxplot
#' @description A function to make boxplots of one specified taxa relative abundance with the option to stratify by a factor variable
#' @param micro_set A tidy_micro data set
#' @param taxa A character string. The name of the taxa of interest
#' @param ... The factor variable you'd like to stratify by
#' @param y The taxa information
#' @param xlab x-axis label
#' @param ylab y-axis label
#' @param main Plot title
#' @param subtitle Subtitle for the plot
#' @return A ggplot that you can add geoms to if you'd like
#' @examples
#' data(phy); data(cla); data(ord); data(fam); data(met)
#' otu_tabs = list(Phylum = phy, Class = cla, Order = ord, Family = fam)
#' set <- tidy_micro(otu_tabs = otu_tabs, meta = met) %>%
#' filter(day == 7) ## Only including the first week
#'
#' set %>% taxa_boxplot("Firmi:Clost:Clostridiales", bpd1)
#' @export
taxa_boxplot <- function(micro_set, taxa, ..., y = ra, xlab = NULL, ylab = NULL,
                         main = NULL, subtitle = NULL, legend_title = NULL){

  if(!is.character(taxa)) stop("taxa must be a character string")
  if(taxa %nin% micro_set$Taxa) stop(paste(taxa, "not in supplied tidy_micro set. \n"))

  micro_set %>%
    dplyr::filter(Taxa == taxa) %>%
    dplyr::group_by(!!!quos(...)) %>%
    dplyr::mutate(box_groups = interaction(!!!quos(...), sep = ":")) %>%
    ggplot2::ggplot(aes(x = box_groups, y = !!enquo(y), fill = box_groups)) +
    ggplot2::geom_boxplot() +
    ggplot2::labs(x = xlab, y = ylab, title = main, subtitle = subtitle, fill = legend_title) +
    ggplot2::theme_bw()

}
CharlieCarpenter/tidy.micro documentation built on Jan. 19, 2020, 6:28 p.m.