R/plot_xBar.r

Defines functions xBar

#' annotate x ranges as labeled bars
#' 
#' @param x1,x2   vectors of start and endpoints, respectively
#' @param y       y value where to plot the markers (may be a vector)
#' @param text    labels (may be a vector)
#'
#' 
#' @return nothing
#' @examples
#' data(KVsweeps)
#' plot(KVsweeps)
#' xBar(.01,.02,1,'test')
#' xBar(c(.01,.026),c(.025,.05),12,c('baseline','depolarisation'))
#' 
#' @export
xBar <- function(x1, x2, y, text = "", pos = 3, cex = 0.7, col.text = "black", col.box = "grey80", 
    col.border = col.box, offset = 0, box = T, lines = F, layer = F, col.layer = "grey95", 
    col.lines = col.box, xpd = NA) {
    if (is.list(x1)) 
        x1 <- unlist(x1)
    if (is.list(x2)) 
        x2 <- unlist(x2)
    if (is.list(y)) 
        y <- unlist(y)
    if (!box) {
        segments(x1, y, x2, y, xpd = xpd)
    }
    if (box) {
        rect(x1, y, x2, y + par("cxy")[2] * cex, col = col.box, border = col.border, xpd = xpd)
    }
    
    if (lines) {
        abline(v = c(x1, x2), col = col.lines, lty = 3, xpd = xpd)
    }
    if (layer) {
        rect(x1, par("usr")[3], x2, par("usr")[4], col = col.layer, border = col.border, xpd = xpd)
        # box()
    }
    text(x1 + (x2 - x1)/2, y, text, pos = pos, cex = cex, col = col.text, offset = offset, 
        xpd = xpd)
    
}
tdanker/ephys2 documentation built on Aug. 11, 2019, 12:12 p.m.