R/plotCUS_function.R

Defines functions plotCUS

Documented in plotCUS

plotCUS <- function(samp, tax, var, abu, taxon = NULL, ncol = NULL, xmin = NULL, xmax = NULL, log = FALSE, psize = 1, labels = TRUE, linetype = "dashed", xlab = "Gradient", ylab = "Scaled cumulative sum"){

  if(is.numeric(var) != T)
  {stop("var must be numeric.")}
  if(is.numeric(abu) != T)
  {stop("abu must be numeric.")}

  if(log == T)
  {log <- ggplot2::scale_x_log10()}
  else if(log == F)
  {log <- NULL}
  else{stop("Argument for log has to be either TRUE or FALSE.")}

  if(is.null(xmin) == T && is.null(xmax) == T)
  {xrange <- NULL}
  else if(is.null(xmin) == F && is.null(xmax) == T)
  {xrange <- ggplot2::xlim(xmin, max(df$var))}
  else if(is.null(xmin) == T && is.null(xmax) == F)
  {xrange <- ggplot2::xlim(min(df$var), xmax)}
  else if(is.null(xmin) == F && is.null(xmax) == F)
  {xrange <- ggplot2::xlim(xmin, xmax)}
  else{stop("Argument for xmin or xmax need to be nummeric.")}

  if(labels == T){theme <- ggplot2::theme(axis.line = ggplot2::element_line(colour = "black"),
                                     axis.text.y = ggplot2::element_text(colour = "black"),
                                     axis.title.y = ggplot2::element_text(colour = "black"),
                                     legend.position = "none",
                                     legend.key = ggplot2::element_blank(),
                                     panel.background = ggplot2::element_blank(),
                                     panel.grid.minor = ggplot2::element_blank(),
                                     strip.background = ggplot2::element_rect(
                                       colour="black", fill="white",
                                       linetype="solid"))}
  else if(labels == F){theme <-  ggplot2::theme(axis.line = ggplot2::element_line(colour = "black"),
                   axis.text.y = ggplot2::element_text(colour = "black"),
                   axis.title.y = ggplot2::element_text(colour = "black"),
                   legend.position = "none",
                   strip.text.x = ggplot2::element_blank(),
                   legend.key = ggplot2::element_blank(),
                   panel.background = ggplot2::element_blank(),
                   panel.grid.minor = ggplot2::element_blank(),
                   strip.background = ggplot2::element_rect(
                     colour="", fill="",
                     linetype=""))}
  else{stop("Argument for labels has to be either TRUE or FALSE.")}

  df1 <- setNames(cbind.data.frame(samp, tax, var, abu),c("samp", "tax", "var", "abu"))
  df1 <- df1[df1$tax %in%  names(table(df1$tax))[table(df1$tax) >= 10],]

  tot       <- aggregate(data=df1, tax~samp*var, length)
  tot$tax   <- 1
  tot$custax<- cumsum(tot$tax)/max(cumsum(tot$tax))

  totabu       <- aggregate(data=df1, abu~samp*var, sum)
  totabu$cusabu<- cumsum(totabu$abu)/max(cumsum(totabu$abu))

  tot        <- cbind(tot, totabu)[-c(5,6)]
  tot$sumcus <- tot$custax+tot$cusabu
  tot$custot <- (tot$sumcus-min(tot$sumcus))/(max(tot$sumcus)-min(tot$sumcus))
  tot        <- tot[-c(7)]

  cuslist   <- list()

  for(i in unique(df1$tax)){
    tax              <- df1[df1$tax == i,]
    tax$tax          <- 1

    taxsub           <- tot[tot$samp %in% tax$samp,]
    taxsub$taxsum    <- cumsum(taxsub$tax)/max(cumsum(taxsub$tax))
    taxsub$abusum    <- cumsum(taxsub$abu)/max(cumsum(taxsub$abu))
    taxsub$taxtot    <- taxsub$taxsum+taxsub$abusum
    taxsub$taxtot    <- (taxsub$taxtot-min(taxsub$taxtot))/(max(taxsub$taxtot)-min(taxsub$taxtot))
    taxsub$taxon     <- i
    rownames(taxsub) <- NULL
    cuslist[[i]]     <- setNames(taxsub[,c("taxon", "var", "custot", "taxtot")], c("taxon", "var", "custot", "taxtot"))}

  df                 <- do.call(rbind.data.frame, cuslist)
  rownames(df)       <- NULL
  df$taxon           <- as.factor(df$taxon)

  colint            <- setNames(as.data.frame(matrix(ncol = 3, nrow = 0)), c("taxon", "colour", "location"))
  for(x in df$taxon){
    tax              <- df[df$taxon == x,]
    col              <- ifelse((tax$custot-tax$taxtot)[which.max((tax$custot-tax$taxtot)^2)] > 0, "green", "red")
    loc              <- tax$var[which.max((tax$custot-tax$taxtot)^2)]
    colint[x,]       <- cbind(x, col, loc)}
  rownames(colint)   <- NULL
  colint$taxon       <- as.factor(colint$taxon)
  colint$location    <- as.numeric(colint$location)

  df <- merge(df, colint, by.x = "taxon", by.y = "taxon")
  if(is.character(taxon) == T){
    df <- df[df$taxon %in% taxon,]
    if(nrow(df) == 0){stop("Not the right taxon name(s) selected?")}}
  else{
    df <- df}

  ggplot2::ggplot(df)+
    ggplot2::xlab(xlab)+log+xrange+
    ggplot2::ylab(ylab)+
    ggplot2::geom_point(ggplot2::aes(df$var, df$custot), size = psize, colour = "blue")+
    ggplot2::geom_point(ggplot2::aes(df$var, df$taxtot), size = psize, colour =  df$colour)+
    ggplot2::geom_vline(ggplot2::aes(xintercept = df$location), linetype = linetype)+
    theme+facet_wrap(~ taxon, ncol = ncol)}
snwikaij/GRASS documentation built on July 29, 2020, 1:54 p.m.