R/plot_functions.R

Defines functions plotOligoFreqs

Documented in plotOligoFreqs

#' plotOligoFreqs
#' 
#' Plots oligo-nucleotide frequencies over a region as lines.
#' 
#' @export
#' @param freqs (numeric matrix) oligonucleotide frequencies
#' @param x_pos (numeric vector) x axis positions
#' @param ylim (numeric tuple) range of the y axis
#' @param xlab (character) x axis label, defaults to: \"distance to position of interest\"
#' @param ylab (character) y axis label, defaults to: \"frequencies\"
#' @param legend_nrow (integer) over how many rows should the legend be split
#' @param col (character vector) colours to use, if \code{NULL} (default) a palette will be generated by \code{\link[maRs]{distinctive_colors}}
#' @param ... further parameters for \code{\link[graphics]{plot}}
#' 
plotOligoFreqs <- function(freqs, x_pos=NULL, ylim=NULL, xlab="distance to position of interest", ylab="frequencies", legend_nrow=1, col=NULL, ...) {
  
  if(all(is.nan(freqs))) {
    freqs[] <- 0
  }
  
  if(length(ylim) == 0) {
    ylim <- c(min(freqs, na.rm =T),max(freqs, na.rm =T))
  }
  
  if(length(x_pos) == 0) {
    x_pos <- 1:ncol(freqs)
  }
  
  if(!is.null(col)) {
    colours <- col
    if(length(col) != nrow(freqs)) {
      warning("length of col doesn't match rows of freqs!")
    } 
  } else {
    colours <- maRs::distinctive_colors(nrow(freqs))
  }
  
  plot(x_pos, freqs[1,], typ='l', col=colours[1], ylim=ylim, ylab=ylab, xlab="", xaxt='n', ...)
  axis(side=1, pretty(x_pos, 10), mgp=c(3.5,1.5,0))
  title(xlab=xlab, mgp=c(3.5,1.5,0))
  for(i in 2:nrow(freqs)) {
    lines(x_pos ,freqs[i,],typ='l',col=colours[i], ...)
  }
  legend("top",legend=rownames(freqs),fill=colours, ncol=nrow(freqs)/legend_nrow, cex=min(par('cex'), 6/(nrow(freqs)^(0.25))^2)) #,horiz=TRUE)
}
markheron/pRon documentation built on Feb. 18, 2020, 12:33 a.m.