taiyun / corrplot

A visual exploratory tool on correlation matrix
https://github.com/taiyun/corrplot
Other
316 stars 86 forks source link

bg color not working in corrplot.mixed #119

Closed ptoche closed 3 years ago

ptoche commented 6 years ago

Setting bg in corrplot.mixed doesn't add background color, as would have been expected from the description:

corrplot.mixed(cor(mtcars), bg = "black", cl.pos = "n") 

cor2

Now to be fair, cl.pos is not an argument of corrplot.mixed, I added it to mention a feature request: adding it 👍

ptoche commented 6 years ago

In the below modified version of corrplot.mixed, I have added a col option to go with the existing lower.col and upper.col. And likewise a bg option together with lower.bg and upper.bg.

I have not tested extensively. It has worked for my purpose.

cor3

Here's a quick fix to add cl.pos = "n"

  1. add cl.pos = c("n"), to the list of arguments (line 31)
  2. add cl.pos <- match.arg(cl.pos) just below tl.pos (line 42)
  3. add cl.pos = cl.pos to the first call of corrplot (the second call has cl.pos = "n", which is correct) (line 63)

I was only interested in adding the "n" option, so not sure if the other options are taken care of properly.

ptoche commented 6 years ago

For convenience, here is the modified corrplot.mixed.r I haven't documented my edits, as you're supposed to...

#' Using mixed methods to visualize a correlation matrix.
#'
#' @param corr Matrix, the correlation matrix to visualize.
#' @param lower Character, the visualization method for the lower triangular
#'   correlation matrix.
#' @param upper Character, the visualization method for the upper triangular
#'   correlation matrix.
#' @param tl.pos Character, \code{"lt"}, \code{"d"} or \code{"n"}, giving
#'   position of text labels, \code{"lt"} means left and top,  \code{"d"} means
#'   diagonal. If \code{"n"},  add no textlabel.
#' @param diag Character, for specifying the glyph on the principal diagonal. It
#'   is one of \code{"n"} (default,  draw nothing), \code{"l"} (draw the glyphs
#'   of lower triangular) or \code{"u"} (draw the glyphs of upper triangular).
#' @param bg The background color.
#' @param addgrid.col See the \code{addgrid.col} parameter in the function
#'   \code{\link{corrplot}}
#' @param lower.col Passed as \code{col} parameter to the lower matrix.
#' @param upper.col Passed as \code{col} parameter to the upper matrix.
#' @param plotCI See the \code{plotCI} parameter in the function
#'   \code{\link{corrplot}}
#' @param mar See \code{\link{par}}.
#' @param \dots Additional arguments for corrplot's wrappers
#'
#' @author Taiyun Wei
#' @example vignettes/example-corrplot.mixed.R
#' @export
corrplot.mixed <- function(
  corr,
  lower = "number",
  upper = "circle",
  tl.pos = c("d", "lt", "n"),
  cl.pos = c("n"),
  diag = c("n", "l", "u"),
  bg = NULL,
  lower.bg = NULL,
  upper.bg = NULL,
  addgrid.col = "grey",
  col = NULL,
  lower.col = NULL,
  upper.col = NULL,
  plotCI = c("n", "square", "circle", "rect"),
  mar = c(0, 0, 0, 0),
  ...)
{
  tl.pos <- match.arg(tl.pos)
  cl.pos <- match.arg(cl.pos)
  diag <- match.arg(diag)
  n <- nrow(corr)

  # fixes issue #21
  # some methods are not compatible with plotCI="rect"
  adjust_plotCI <- function(plotCI, method) {
    if (plotCI != "rect" || method %in% c("circle", "square")) {
      return(plotCI)
    }
    return("n")
  }

  plotCI_lower <- adjust_plotCI(plotCI, lower)
  plotCI_upper <- adjust_plotCI(plotCI, upper)

  if (!(is.null(col))) {
    lower.col <- col
    upper.col <- col
  }

  if (!(is.null(bg))) {
    lower.bg <- bg
    upper.bg <- bg
  }

  # fixed issue #102
  # restore this parameter when exiting the corrplot.mixed function in any way
  oldpar <- par(mar = mar, bg = "white")
  on.exit(par(oldpar), add = TRUE)

  corrplot(corr, type = "upper", method = upper, diag = TRUE,
           tl.pos = tl.pos, cl.pos = cl.pos, plotCI = plotCI_upper,
           col = upper.col, mar = mar, bg = upper.bg, ...)

  corrplot(corr, add = TRUE, type = "lower", method = lower,
           diag = (diag == "l"),
           tl.pos = "n", cl.pos = "n", plotCI = plotCI_lower,
           col = lower.col, mar = mar, bg = lower.bg, ...)

  if (diag == "n" && tl.pos != "d") {
    # draw empty rectangles over the diagonal to "clean" it graphically
    symbols(1:n, n:1, add = TRUE, bg = bg, fg = addgrid.col,
            inches = FALSE, squares = rep(1, n))
  }

  # fixes issue #43
  # return value should be the same as in the corrplot function
  invisible(corr)
}