R/plot_fun.R

#' Display basic information of regions of genome data group by Sample or Chromosome
#'
#' @param region a data frame contains information of any experiments that result in regions of
#'               the genome showing "enrichment" which means above p-value threshold.
#' @param group a character specify group option, current only support 'Sample' and 'Chromosome'.
#' @param statistic a character specify basic statistics option, current only support 'Mean' and 'Median'.
#'
#' @return ggplot graphs.
#' @examples
#' \dontrun{
#' library(ACME)
#' datdir <- system.file('extdata',package='ACME')
#' fnames <- dir(datdir)
#' example.agff <- read.resultsGFF(fnames,path=datdir)
#' calc <- do.aGFF.calc(example.agff,window=1000,thresh=0.95)
#' Regions <- findRegions(calc)
#' #example 1 mean p values of regions above p value threshhold grouped by samples
#' RegionsInfoPlot(Regions)
#'
#' #example 2 mean p values of regions above p value threshhold grouped by Chromosomes
#' RegionsInfoPlot(Regions,group = "Chromosome", statatisic = "Mean")
#'
#'
#' #example 2 mean p values of regions above p value threshhold grouped by Chromosomes
#' RegionsInfoPlot(Regions,group = "Sample", statatisic = "Mean")
#' }
#'
#' @export
RegionsInfoPlot <- function(Regions, group = "Sample", statatisic = "Mean") {

  #checking

  #if not data frame, stop
  stopifnot(is.data.frame(Regions))

  #specify column names must be given.

  stopifnot(all(c("Sample","Chromosome","Mean","Median") %in%  colnames(Regions)))

  #options must be supported
  stopifnot( group %in% c("Sample","Chromosome"))

  stopifnot( statatisic %in% c("Mean","Median"))

  #convert to factors
  regs <- Regions

  regs$Chromosome <- factor(regs$Chromosome, levels = c(paste0("chr",1:22), "chrX"))

  #graphs
  p1 <- ggplot2::ggplot(regs, ggplot2::aes(Sample,  Mean, fill = Sample)) +
    ggplot2::geom_violin() +  ggplot2::stat_summary(fun.y=mean, geom="point", size=2, color="red")  +
    ggplot2::labs(title = "Mean Values of Regions above p value threshold group by sample") +
    ggplot2::scale_fill_brewer(palette = "YlOrRd") +
    ggplot2::theme_bw() +  ggplot2::guides(fill=FALSE)  + ggplot2::xlab("Mean of p values") +
    ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45))

  p2 <-  ggplot2::ggplot(regs, ggplot2::aes(Sample,  Mean, fill = Sample)) +
    ggplot2::geom_violin() +  ggplot2::stat_summary(fun.y=mean, geom="point", size=2, color="red")  +
    ggplot2::labs(title = "Median Values of Regions above p value threshold group by sample") +
    ggplot2::scale_fill_brewer(palette = "YlOrRd") +
    ggplot2::theme_bw() +  ggplot2::guides(fill=FALSE)  + ggplot2::xlab("Median of p values") +
    ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45))

  p3 <- ggplot2::ggplot(regs, ggplot2::aes(Chromosome,  Mean, fill = Chromosome)) +
    ggplot2::geom_violin() +  ggplot2::stat_summary(fun.y=mean, geom="point", size=2, color="red")  +
    ggplot2::labs(title = "Mean Values of Regions above p value threshold group by Chromosome") +
    ggplot2::scale_fill_brewer(palette = "YlOrRd") +
    ggplot2::theme_bw() +  ggplot2::guides(fill=FALSE)  + ggplot2::xlab("Mean of p values") +
    ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45))

  p4 <- ggplot2::ggplot(regs, ggplot2::aes(Chromosome,  Median, fill = Chromosome)) +
    ggplot2::geom_violin() +  ggplot2::stat_summary(fun.y=mean, geom="point", size=2, color="red")  +
    ggplot2::labs(title = "Median Values of Regions above p value threshold group by Chromosome") +
    ggplot2::scale_fill_brewer(palette = "YlOrRd") +
    ggplot2::theme_bw() +  ggplot2::guides(fill=FALSE)  + ggplot2::xlab("Meadin of p values") +
    ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45))

  #swith confidtions, which plot to display
  if(group == "Sample" & statatisic == "Mean") {

    print(p1)

  } else if (group == "Sample" & statatisic == "Median") {

    print(p2)

  } else if (group == "Chromosome" & statatisic == "Mean") {

    print(p3)

  } else  if (group == "Chromosome" & statatisic == "Median") {

    print(p4)
  }

}
#Ends
yiqiutang/genomeRegionsInfo documentation built on May 14, 2019, 4:04 a.m.