R/16_Correlation_plot.R

Defines functions romicsCorrelation

Documented in romicsCorrelation

#' romicsCorrelation()
#' @description generate one or multiple volcano plots from t.test or wilcox.test run using the functions romicsTtest() or romicsWilcoxTest() respectively
#' @param romics_object  an object of type romics_object generated with the
#' @param by_level Boolean indicates if the correlation plot has to be displayed for each level of the factor set with the parameter <factor = >, if multiple levels are present, one correlation plot will be generated by factor, if FALSE the global correlation plot will be generated.
#' @param factor Indicates what factor to use for the <by_level> plotting, by default ("main")
#' @param imputed Indicates if the imputed values should be used for the correlation (FALSE by default) if TRUE, the data employed will utilize both imputed and non imputed values.
#' @param corr_type has to be either 'pearson', 'kendall',or 'spearman', indicates the type of correlation calculated.
#' @param mode has to be either 'scatter' or 'colors' to indicate the type of plot to display
#' @param ... passes argument to the function ggpairs from the package GGally, type ?ggpairs() for documentation
#' @details generate one or multiple correlation plots using the function ggpairs from the package GGally (scatter mode) or a simple color heatmap (color mode).
#' @return This function will print different plot requested
#' @author Geremy Clair
#' @examples romicsCorrelation(Example_processed_romics_object,by_level=FALSE,factor="main",imputed=T,corr_type="pearson",mode="color")
#' @export
romicsCorrelation<-function(romics_object,by_level=FALSE,factor="main",imputed=FALSE,corr_type="pearson",mode="scatter",...){
  if(!is.romics_object(romics_object) | missing(romics_object)) {stop("romics_object is missing or is not in the appropriate format")}
  if(missing(by_level)){by_level=FALSE}
  if(!by_level %in% c(TRUE,FALSE)){stop("<by_levels> has to be either TRUE or FALSE")}
  if(missing(imputed)){imputed=TRUE}
  if(!imputed %in% c(TRUE,FALSE)){stop("<imputed> has to be either TRUE or FALSE")}
  if(missing(corr_type)){corr_type="pearson"}
  if(!corr_type %in% c("pearson", "kendall","spearman")){stop("<corr_type> has to be either 'pearson', 'kendall',or 'spearman'")}
  if(missing(factor)){factor="main"}
  if(missing(mode)){mode="scatter"}
  if(!mode %in% c("scatter", "color")){stop("<mode> has to be either 'scatter' or 'color'")}
  if(imputed == FALSE){romics_object<-romicsRestoreMissing(romics_object)}

  data<-romics_object$data
  myplots<-list()
  if(by_level==FALSE){
  if(mode=="scatter"){
    myplots[[1]]<- ggpairs(data,upper = list(continous = wrap("cor",method=corr_type)),)+theme_ROP()}
    if(mode=="color"){
      melt_cor=melt(cor(data,method = corr_type))
      round_val=round(melt_cor$value,2)
      myplots[[1]]<-ggplot(melt_cor,aes(x=Var1, y=Var2, fill=value))+
        geom_tile()+
        scale_fill_viridis_c()+
        geom_text(aes(Var2, Var1, label = round_val))+
        theme_ROP()
    }
    }else{
  if(factor=="main"){factor<-romics_object$main_factor}
  if(!factor %in% romics_object$main_factor){stop("the <factor> was not in the list of factors comprised in this romics_object, use the function romicsFactorNames() to get the full list.")}
  factor<-as.factor(t(romics_object$metadata[rownames(romics_object$metadata)==factor,]))
  lv<-levels(factor)
  factor<-as.character(factor)

    for (i in 1:length(lv)){
      dt<-data[,factor==lv[i]]
      if(mode=="scatter"){myplots[[i]]<-ggpairs(dt,upper = list(continous = wrap("cor",method=corr_type)),...)}
      if(mode=="color"){
          melt_dt<-melt(cor(dt,method = corr_type))
          round_val=round(melt_dt$value,2)
          myplots[[i]]<-ggplot(melt_dt,aes(x=Var1, y=Var2, fill=value))+
                        geom_tile()+
                        scale_fill_viridis_c()+
                        geom_text(aes(Var2, Var1, label = round_val))+
                        theme_ROP()
    }
    }
    }
   myplots
}
PNNL-Comp-Mass-Spec/RomicsProcessor documentation built on March 18, 2023, 5:14 a.m.