R/plotClad.R

Defines functions plotClad

Documented in plotClad

#' Plot Cladogram
#'
#' @param dataset MicroVis dataset. Defaults to the activedataset
#' @param factor Factor to analyze by. Defaults to the active factor
#'
#' @return Cladogram plot of LEfSe significant taxa
#' @export
#'
plotClad <- function(dataset=NULL, factor=NULL) {
  if(is.null(dataset)) dataset <- get('active_dataset',envir = mvEnv)

  if(is.null(dataset$name)) dataset_name <- 'active_dataset'
  else dataset_name <- dataset$name

  on.exit(cat('There is an incompatibility between ggtree and dplyr >=1.0.6 as of May 2021. If you can, please downgrade to dplyr 1.0.5 for cladograms to work'))

  factor <- factor[factor %in% names(dataset$factor)]
  if(is.null(factor)) factor <- dataset$active_factor
  factor <- setFVar(dataset,factor_name = factor)
  colors <- dataset$colors
  colors <- colors[names(colors) %in% factor$subset]

  if(is.null(dataset$stats[[factor$name]]$lefse)) dataset <- mvlefse(dataset,
                                                                     dataset_name)

  mm <- dataset$stats[[factor$name]]$lefse

  clad <- microbiomeMarker::plot_cladogram(mm, color = colors,
                                           clade_label_level = 5,
                                           annotation_shape = 22,
                                           annotation_shape_size = 3,
                                           node_size_offset = 2)

  on.exit()

  clad_legend <- ggpubr::as_ggplot(get_legend(clad))
  clad <- clad+theme(legend.position = 'none')

  show(clad)

  save_directory <- saveResults(dataset,foldername = 'Cladogram',
                                factors = dataset$factors,
                                active_factor = dataset$active_factor,
                                verbose = F)

  show(clad_legend)
  if(!is.null(save_directory)) {
    ggsave(file.path(save_directory,paste0(nameAnalysis(dataset$factors,dataset$active_factor),
                                           'legend.png')),clad_legend,device='png',
           width = 18,height = 12,units = 'in',dpi = 600)
    cat('\nFigures and any associated statistics saved to:\n ',save_directory)
  }

  activate(dataset)

  return(clad)
}
microresearcher/MicroVis documentation built on Feb. 8, 2024, 10:59 a.m.