R/plot-misc.R

Defines functions add.alpha errbar

Documented in add.alpha errbar

#' Add an alpha channel to a color
#' 
#' @param col   a color value or values.
#' @param alpha transparency intensity.
#' 
#' @references \url{https://gist.github.com/mages/5339689}
#' 
#' @importFrom grDevices rgb col2rgb
#' @export

add.alpha <- function(col, alpha = 1){
   out <- apply(sapply(col, col2rgb)/255, 2L,
                function(x)
                   rgb(x[1L], x[2L], x[3L], alpha=alpha))
   out[is.na(col)] <- NA
   out
}


#' Add error bars to the plot
#' 
#' @param pos    vector of locations of bars
#' @param low    vector of lower error margins
#' @param high   vector of higher error margins
#' @param length length of error bar whiskers
#' @param horiz  a logical value. If \code{FALSE}, the bars are drawn vertically
#'               with the first bar to the left. If \code{TRUE}, the bars are
#'               drawn horizontally with the first at the bottom. 
#' @param \dots  additional \code{arrows} parameters
#' 
#' @seealso \code{\link{arrows}}
#' 
#' @importFrom graphics lines arrows
#' @export

errbar <- function(low, high, pos=1:length(low), length=0.05, horiz=FALSE, add=TRUE, ...) {
   if (horiz) {
      if (!add) plot(c(low, high), rep(pos, 2), type="n", xlab="", ylab="", ...)
      arrows(low, pos, high, pos, length=length, angle=90, code=3, ...)
   } else {
      if (!add) plot(rep(pos, 2), c(low, high), type="n", xlab="", ylab="", ...)
      arrows(pos, low, pos, high, length=length, angle=90, code=3, ...)
   }
}
twolodzko/twextras documentation built on May 3, 2019, 1:52 p.m.