#' plotCorrMatrix
#'
#' This function generates a plot of a correlation matrix with various options. It is a wrapper function for
#' the corrplot function from the {corrplot} package.
#' @param X a correlation matrix, typically generated by a call to calcCorrMatrix or the base fn cor()
#' @param order either "original" or "clustered" (for now, other options to be explored)
#' @param label plot title, default is NULL
#' @param n.groups number of groups to mark (applies only if order = "clustered"). NA produces default of round(n/3).
#' @param plot.type layout options in corrplot package. Default is "circle". Other good options are "color" and "number"
#' @keywords correlation matrix, plot
#' @export
#' @examples
#' M <- cor(mtcars)
#' plotCorrMatrix(M)
plotCorrMatrix <- function(X, order = "original", n.groups = NA ,label = NULL, plot.type="circle"){
# layout options
# https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html
# full or half matrix
if(order == "clustered"){type <- "full"; order.in <- "hclust"}
if(order == "original"){type <- "upper"; order.in <- "original"}
# prep for margin argument down the road
margins <- c(1, 1, 1, 1)
# do not display values in the principal diagonal
diag <- FALSE
if(is.na(n.groups)){n.groups <- round(dim(X)[1]/3)}
corrplot(X, type= type,diag = diag, method = plot.type,
order = order.in,addrect = n.groups,
mar = margins)
} # end plotCorrMatrix
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.