R/cor.heat.R

#' Produce a correlation matrix as a heatmap.
#'
#' \code{cor.heat(Mat, coloramp, textsize, scaletext, abs.val=TRUE, ...)}
#'
#' @param Mat is a matrix resulting from correlations
#' @param coloramp is a colorRampPalette() palette, e.g., coloramp = colorRampPalette(c('red','blue'))
#' @param textsize is the multiplier for text size of labels and value size
#' @param scaletext is the multiplier for the text size of legend labels
#' @param abs.val logical; if true, absolute values of correlation coefficient (x100) are displayed
#' @param Names can be supplied to replace the existing variable names
#' @param ... further arguments to be passed to color2D.matplot, in particular show.values=TRUE and border=NA
#'
#' @details axis labels are at top and left hand side, and scale is on
#' right hand side, so make suitable margin sizes.
#' Essentially, this function is a convenience wrapper for
#' color2D.matplot() from plotrix.
#' @examples
#' data(mtcars)
#' CorMT = cor(mtcars, method="spearman")
#' CLS = colorRampPalette(c("blue","white","darkred"))
#' x11()
#' par(mar=c(1,4,4,3))
#' cor.heat(CorMT, CLS, textsize=1.1, scaletext=1.3,show.values=TRUE, border=NA)


cor.heat = function(Mat, coloramp, textsize = 1, scaletext = 1,
                    abs.val = TRUE, Names = NULL,...){
  require(plotrix)
  if(is.null(Names)!=TRUE){
    rownames(Mat) = Names
    colnames(Mat) = Names
  }
  LVars = nrow(Mat)
  CLRS = coloramp(201)
  Cor100 = round(Mat*100)
  CellCol = CLRS[Cor100+101]
  # make diagonals black
  CellCol[seq(from=1,to=(LVars^2),length=LVars)] = "#000000"
  if(abs.val==TRUE) Cor100 = abs(Cor100)
  color2D.matplot(Cor100, cellcolors =CellCol,
                  axes = FALSE, vcex = textsize,
                  xlab="", ylab="",...)
  axis(3, at = 1:nrow(Mat)-0.5, labels = colnames(Mat),
       las=2,cex.axis=textsize)
  axis(2, at = 1:nrow(Mat)-0.5, labels = rev(rownames(Mat)),
       las=2,cex.axis=textsize)
  par(usr=c(0,100,0,100))
  colGradLegend(102,20,colramp = coloramp, colno = 25,frac = 40, width = 3,
            align = "v",labs = c(-100,-50,0,50,100),Round=1,xpd=TRUE,
            tsize = scaletext)
  text(105,65,expression(underline("Scale")),xpd=TRUE,cex=scaletext)
}
helophilus/ColsTools documentation built on May 30, 2019, 4:03 p.m.