diff --git a/R/corrplot.R b/R/corrplot.R index cb2b4a5..6194df0 100644 --- a/R/corrplot.R +++ b/R/corrplot.R @@ -269,7 +269,7 @@ corrplot = function(corr, method = c('circle', 'square', 'ellipse', 'number', 'shade', 'color', 'pie'), type = c('full', 'lower', 'upper'), col = NULL, col.lim = NULL, is.corr = TRUE, bg = 'white', title = '', add = FALSE, diag = TRUE, outline = FALSE, - mar = c(0, 0, 0, 0), + mar = c(0, 0, 0, 0), del.lead.zero = FALSE, addgrid.col = NULL, addCoef.col = NULL, addCoefasPercent = FALSE, @@ -323,6 +323,12 @@ corrplot = function(corr, stop('Need a matrix or data frame!') } + # logical of whether leading zero is dropped from numbers generated from 'method = "number"' and 'addCoef.col' + if (del.lead.zero == TRUE) { + del.lead.zero = function(val) {sub("^(-?)0.", "\\1.", sprintf("%.2f", val)) }} + else if (del.lead.zero == FALSE) { + del.lead.zero = function(val){val}} + # select grid color automatically if not specified if (is.null(addgrid.col)) { addgrid.col = switch(method, color = NA, shade = NA, 'grey') @@ -704,7 +710,7 @@ corrplot = function(corr, if (method == 'number' && plotCI == 'n') { x = (DAT - int) * ifelse(addCoefasPercent, 100, 1) / zoom text(Pos[, 1], Pos[, 2], font = number.font, col = col.fill, - labels = format(round(x, number.digits), nsmall = number.digits), + labels = format(del.lead.zero(round(x, number.digits)), nsmall = number.digits), cex = number.cex) } @@ -883,13 +889,14 @@ corrplot = function(corr, ## add numbers if (!is.null(addCoef.col) && method != 'number') { text(Pos[, 1], Pos[, 2], col = addCoef.col, - labels = format( - round( - (DAT - int) * ifelse(addCoefasPercent, 100, 1) / zoom, - number.digits - ), - nsmall = number.digits - ), + + + if (!is.null(addCoef.col) && method != 'number') { + text(Pos[, 1], Pos[, 2], col = addCoef.col, + labels = del.lead.zero(format( + round((DAT - int) * ifelse(addCoefasPercent, 100, 1) / zoom, number.digits), + nsmall = number.digits + )), cex = number.cex, font = number.font) }