R/graph_functions.R

Defines functions make.posenr.plot make.bubble.plot

Documented in make.bubble.plot make.posenr.plot

#' Make Bubble Plot
#'
#' Creates a ggplot2 bubble plot from a table obtained with \code{\link[mubeen]{multiple.bed.intersect}}
#'
#' With this type of representation, you can only plot a genomic position (a bed file, and not all the multiple intersects at the same time).
#'
#' @param mobeen_object A character vector with the path of the folder with the feature bed files.
#' @param title A character vector with the title of your graph
#' @param odds_conv A boolean indicating if the < 1 Odds Ratios should be converted to negative numbers to obtain a symmetric distribution (e.g. 0.5 would be -2)
#'
#' @return A ggplot2 object with the graph.
#' @export
make.bubble.plot = function(mobeen_object, title="", odds_conv = TRUE){

  if (!is.data.frame(mobeen_object)) {
    mobeen_object = mobeen_object[["join_list"]]
  }

  mobeen_object = mobeen_object[mobeen_object$sum == 0,]
  mobeen_object$FDR[mobeen_object$FDR==0] = .Machine$double.xmin

  if (odds_conv){
    mobeen_object$estimate[mobeen_object$estimate<1] = -1 * 1/ mobeen_object$estimate[mobeen_object$estimate<1]
  }

  plot = ggplot2::ggplot(mobeen_object, ggplot2::aes(y=factor(feature_name), x=factor(cell_type), color = estimate)) +
    ggplot2::geom_point(ggplot2::aes(fill=estimate, size=-log10(FDR)), colour = "black", shape = 21) +
    ggplot2::scale_size_continuous(range = c(1,12)) +
    ggplot2::scale_fill_gradient2(low="darkblue", high="red", mid ='white', midpoint = 0, na.value='white') +
    ggplot2::theme_bw() +
    ggplot2::ylab('') +
    ggplot2::xlab('') +
    ggplot2::scale_y_discrete(limits=unique(mobeen_object$feature_name)) +
    ggplot2::labs(fill = "Odds Ratio") +
    ggplot2::labs(size = "-log10(FDR)") +
    ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, vjust = 0.9, hjust = 1))+
    ggplot2::ggtitle(title)+
    ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))
  
  if (length(unique(mobeen_object$target)) > 1)  {
      plot = plot + ggplot2::facet_wrap(~ target) +
          ggplot2::ggtitle(title)}


  return(plot)
}


#' Make Positional Enrichment Plot
#'
#' Creates a ggplot2 positional enrichment plot from a table obtained with \code{\link[mubeen]{multiple.bed.intersect}}
#'
#' With this type of representation, you can visualize the enrichment of the features in a position in comparision with their genomic context. The result is a graph that mimics a signal plot from only bed files data.
#'
#' @param mobeen_object A character vector with the path of the folder with the feature bed files.
#' @param scales The scales parameter from \code{\link[ggplot2]{facet_wrap}}. It can be "fixed" or "free_y".
#' @param title A character vector with the title of your graph
#'
#' @return A ggplot2 object with the graph.
#' @export
#'
make.posenr.plot = function(mobeen_object, scales="fixed", title=""){

  if (!is.data.frame(mobeen_object)) {
    mobeen_object = mobeen_object[["join_list"]]
  }

  plot = ggplot2::ggplot(mobeen_object, ggplot2::aes(x=sum, y=estimate, color = cell_type)) +
    ggplot2::geom_line(size=1)+ ggplot2::theme_bw() +
    ggplot2::theme(panel.grid.major = ggplot2::element_blank(), panel.grid.minor = ggplot2::element_blank()) +
    ggplot2::labs(x="Distance(bp)", y = "Odds Ratio") +
    ggplot2::geom_vline(xintercept=0, linetype="dashed") +
    ggplot2::scale_color_discrete(name="Cell Type")

    if (length(unique(mobeen_object$target)) > 1)  {
      plot = plot + ggplot2::facet_grid(rows = ggplot2::vars(feature_name), cols = ggplot2::vars(target), scales=scales) +
      ggplot2::ggtitle(title)
    }
    else{
      plot = plot + ggplot2::facet_wrap(~ feature_name, ncol=2, scales=scales) +
      ggplot2::ggtitle(title)}

  return(plot)
}
omorante/mubeen documentation built on June 4, 2020, 9:19 p.m.