R/ps_refactor.R

Defines functions ps_refactor

Documented in ps_refactor

#' Relevel the Sample variable in a psmelted phyloseq object 
#' 
#' Relevel the Sample variable in a psmelted phyloseq object, 
#' such that similar samples are plotted together with ggplot barcharts 
#' 
#' @param psmelted A phyloseq object melted into a data.frame with psmelt
#' @param ... Arguments passed to hclust
#' @importFrom reshape2 dcast
#' @importFrom stats dist hclust
#' @export
ps_refactor <- function(psmelted, ...){
    if(!is(psmelted, "data.frame")){
        stop("Input should be a phyloseq object melted into a data.frame with psmelt")
    }
    mat <- reshape2::dcast(OTU ~ Sample, value.var = "Abundance", data = psmelted)   
    hc <- hclust(dist(t(mat[, -1])), ...)
    psmelted$Sample <- factor(psmelted$Sample, levels = hc$labels[hc$order])
    return(psmelted)
}    
Russel88/MicEco documentation built on Nov. 24, 2022, 2:33 a.m.