R/functions.R

Defines functions plotRanges colorize

## custom functions

## 1. easy color text ####
# can be used as inline code, eg: `r colorize("Additional Ideas:", "red")`
colorize <- function(x, color) {
  if (knitr::is_latex_output()) {
    sprintf("\\textcolor{%s}{%s}", color, x)
  } else if (knitr::is_html_output()) {
    sprintf("<span style='color: %s;'>%s</span>", color, 
            x)
  } else x
}

## 2.granges plot ####
# ref https://github.com/genomicsclass/ph525x/blob/master/R/plotRanges.R  
plotRanges <- function(x, xlim = x, main = deparse(substitute(x)),
                       col = "black", sep = 0.25, ...)
{
  height <- 0.5
  if (is(xlim, "Ranges"))
    xlim <- c(min(start(xlim)), max(end(xlim)))
  bins <- disjointBins(IRanges(start(x), end(x) + 1))
  plot.new()
  plot.window(xlim, c(0, max(bins)*(height + sep)))
  ybottom <- bins * (sep + height) - height
  rect(start(x)-0.5, ybottom, end(x)+0.5, ybottom + height, col = col, ...)
  title(main)
  axis(1)
}
vanheeringen-lab/learnr.proto documentation built on March 1, 2021, 11:10 p.m.