Skip to content

Commit c36ae27

Browse files
authored
Merge pull request #81 from vsimko/master
fixed issues #10 and #76
2 parents c336f9d + 3f5537e commit c36ae27

11 files changed

Lines changed: 264 additions & 87 deletions

File tree

DESCRIPTION

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
Package: corrplot
22
Type: Package
33
Title: Visualization of a Correlation Matrix
4-
Version: 0.81
5-
Date: 2017-02-07
4+
Version: 0.82
5+
Date: 2017-02-09
66
Author: Taiyun Wei, Viliam Simko
77
Suggests:
88
knitr,
99
testthat
1010
Maintainer: Taiyun Wei <weitaiyun@gmail.com>
1111
Description: A graphical display of a correlation matrix or general matrix.
12-
It also contains some algorithms to do matrix reordering.
12+
It also contains some algorithms to do matrix reordering. In addition,
13+
corrplot is good at details, including choosing color, text labels,
14+
color labels, layout, etc.
1315
License: GPL
1416
URL: https://github.com/taiyun/corrplot
1517
BugReports: https://github.com/taiyun/corrplot/issues

R/corrplot-package.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#' @docType package
2+
#' @name corrplot-package
3+
#'
4+
#' @title
5+
#' Visualization of a correlation matrix
6+
#'
7+
#' @description
8+
#' The corrplot package is a graphical display of a correlation matrix,
9+
#' confidence interval or general matrix. It also contains some algorithms to do
10+
#' matrix reordering. In addition, corrplot is good at details, including
11+
#' choosing color, text labels, color labels, layout, etc.
12+
#'
13+
#' @author Taiyun Wei (weitaiyun@@gmail.com)
14+
#' @author Viliam Simko (viliam.simko@@gmail.com)
15+
#'
16+
#' Maintainer: Taiyun Wei (weitaiyun@@gmail.com)
17+
#'
18+
#' @references
19+
#' Michael Friendly (2002).
20+
#' \emph{Corrgrams: Exploratory displays for correlation matrices}.
21+
#' The American Statistician, 56, 316--324.
22+
#'
23+
#' D.J. Murdoch, E.D. Chow (1996).
24+
#' \emph{A graphical display of large correlation matrices}.
25+
#' The American Statistician, 50, 178--180.
26+
#'
27+
#' @seealso
28+
#' The \code{plotcorr} function in the \code{ellipse} package and
29+
#' \code{corrgram} function in the \code{corrgram} package has some
30+
#' similarities.
31+
#'
32+
#' @keywords hplot
33+
#' @keywords correlation
34+
#' @keywords correlogram
35+
#' @keywords feature selection
36+
#' @keywords dimensionality reduction
37+
NULL
38+
39+
.onAttach <- function(libname, pkgname) {
40+
# just to show a startup message
41+
message <- paste("corrplot", utils::packageVersion("corrplot"), "loaded")
42+
packageStartupMessage(message, appendLF = TRUE)
43+
}

R/corrplot.R

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@
229229
#' ARSA, BBURCG, BBWRCG, MDS, TSP, Chen and so forth.
230230
#'
231231
#' @example vignettes/example-corrplot.R
232-
#' @keywords hplot
233232
#' @import graphics grDevices stats
234233
#' @export
235234
corrplot <- function(corr,
@@ -394,8 +393,8 @@ corrplot <- function(corr,
394393
apply_mat_filter <- function(mat) {
395394
x <- matrix(1:n * m, n, m)
396395
switch(type,
397-
upper = mat[row(x) > col(x)] <- Inf,
398-
lower = mat[row(x) < col(x)] <- Inf
396+
upper = mat[row(x) > col(x)] <- Inf,
397+
lower = mat[row(x) < col(x)] <- Inf
399398
)
400399

401400
if (!diag) {
@@ -419,14 +418,14 @@ corrplot <- function(corr,
419418
# we use this for rending NA cells differently
420419
getPos.NAs <- function(mat) {
421420
tmp <- apply_mat_filter(mat)
422-
ind <- which(is.na(tmp), arr.ind = TRUE)
421+
ind <- which(is.na(tmp), arr.ind = TRUE)
423422
Pos <- ind
424423
Pos[,1] <- ind[,2]
425424
Pos[,2] <- -ind[,1] + 1 + n
426425
return(Pos)
427426
}
428427

429-
Pos <- getPos.Dat(corr)[[1]]
428+
Pos <- getPos.Dat(corr)[[1]]
430429

431430
# rows
432431
n2 <- max(Pos[,2])
@@ -460,11 +459,11 @@ corrplot <- function(corr,
460459

461460
## assign colors
462461
assign.color <- function(dat = DAT, color = col){
463-
newcorr <- (dat + 1) / 2
464-
newcorr[newcorr <= 0] <- 0
465-
newcorr[newcorr >= 1] <- 1 - 1e-16
462+
newcorr <- (dat + 1) / 2
463+
newcorr[newcorr <= 0] <- 0
464+
newcorr[newcorr >= 1] <- 1 - 1e-16
466465

467-
color[floor(newcorr * length(color)) + 1] # new color returned
466+
color[floor(newcorr * length(color)) + 1] # new color returned
468467
}
469468

470469
col.fill <- assign.color()
@@ -503,39 +502,54 @@ corrplot <- function(corr,
503502
## calculate label-text width approximately
504503
if (!add) {
505504
plot.new()
506-
xlabwidth <- ylabwidth <- 0
507505

506+
# Issue #10: code from Sébastien Rochette (github user @statnmap)
507+
xlabwidth <- max(strwidth(newrownames, cex = tl.cex))
508+
ylabwidth <- max(strwidth(newcolnames, cex = tl.cex))
509+
laboffset <- strwidth("W", cex = tl.cex) * tl.offset
510+
511+
# Issue #10
508512
for (i in 1:50) {
509-
xlim <- c(m1 - 0.5 - xlabwidth,
510-
m2 + 0.5 + mm * cl.ratio * (cl.pos == "r"))
511-
ylim <- c(n1 - 0.5 - nn * cl.ratio * (cl.pos == "b"),
512-
n2 + 0.5 + ylabwidth)
513-
plot.window(xlim + c(-0.2, 0.2), ylim + c(-0.2, 0.2), asp = 1,
514-
xaxs = "i", yaxs = "i")
513+
xlim <- c(
514+
m1 - 0.5 - laboffset -
515+
xlabwidth * (grepl("l", tl.pos) | grepl("d", tl.pos)),
516+
m2 + 0.5 + mm * cl.ratio * (cl.pos == "r") +
517+
xlabwidth * abs(cos(tl.srt * pi / 180)) * grepl("d", tl.pos)
518+
) + c(-0.35, 0.15) +
519+
c(-1,0) * grepl("l", tl.pos) # margin between text and grid
520+
521+
ylim <- c(
522+
n1 - 0.5 - nn * cl.ratio * (cl.pos == "b"),
523+
n2 + 0.5 + laboffset +
524+
ylabwidth * abs(sin(tl.srt * pi / 180)) * grepl("t", tl.pos)
525+
) +
526+
c(-0.15, 0) +
527+
c(0, -1) * (type == "upper") + # nasty hack
528+
c(0,1) * grepl("d", tl.pos) # margin between text and grid
529+
530+
plot.window(xlim, ylim, asp = 1, xaxs = "i", yaxs = "i")
531+
515532
x.tmp <- max(strwidth(newrownames, cex = tl.cex))
516533
y.tmp <- max(strwidth(newcolnames, cex = tl.cex))
517534

518-
if (min(x.tmp - xlabwidth, y.tmp - ylabwidth) < 0.0001) {
535+
laboffset.tmp <- strwidth("W", cex = tl.cex) * tl.offset
536+
if (max(x.tmp - xlabwidth,
537+
y.tmp - ylabwidth,
538+
laboffset.tmp - laboffset) < 1e-03) {
519539
break
520540
}
521541

522542
xlabwidth <- x.tmp
523543
ylabwidth <- y.tmp
524-
}
525544

526-
if (tl.pos == "n" || tl.pos == "d") {
527-
xlabwidth <- ylabwidth <- 0
528-
}
545+
laboffset <- laboffset.tmp
529546

530-
if (tl.pos == "td") ylabwidth <- 0
531-
if (tl.pos == "ld") xlabwidth <- 0
532-
533-
laboffset <- strwidth("W", cex = tl.cex) * tl.offset
534-
xlim <- c(m1 - 0.5 - xlabwidth - laboffset,
535-
m2 + 0.5 + mm * cl.ratio * (cl.pos == "r")) + c(-0.35, 0.15)
536-
ylim <- c(n1 - 0.5 - nn * cl.ratio * (cl.pos == "b"),
537-
n2 + 0.5 + ylabwidth * abs(sin(tl.srt * pi / 180)) + laboffset)
538-
+ c(-0.15, 0.35)
547+
if (i == 50) {
548+
warning(c("Not been able to calculate text margin, ",
549+
"please try again with a clean new empty window using ",
550+
"{plot.new(); dev.off()} or reduce tl.cex"))
551+
}
552+
}
539553

540554
if (.Platform$OS.type == "windows") {
541555
grDevices::windows.options(width = 7,

R/corrplot.mixed.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#' \code{\link{corrplot}}
1717
#' @param plotCI See the \code{plotCI} parameter in the function
1818
#' \code{\link{corrplot}}
19+
#' @param lower.col Passed as \code{col} parameter to the lower matrix.
20+
#' @param upper.col Passed as \code{col} parameter to the upper matrix.
1921
#' @param \dots Additional arguments for corrplot's wrappers
2022
#'
2123
#' @author Taiyun Wei
@@ -29,6 +31,8 @@ corrplot.mixed <- function(
2931
diag = c("n", "l", "u"),
3032
bg = "white",
3133
addgrid.col = "grey",
34+
lower.col = NULL,
35+
upper.col = NULL,
3236
plotCI = c("n", "square", "circle", "rect"),
3337
...)
3438
{
@@ -49,11 +53,13 @@ corrplot.mixed <- function(
4953
plotCI_upper <- adjust_plotCI(plotCI, upper)
5054

5155
corrplot(corr, type = "upper", method = upper, diag = TRUE,
52-
tl.pos = tl.pos, plotCI = plotCI_upper, ...)
56+
tl.pos = tl.pos, plotCI = plotCI_upper,
57+
col = upper.col, ...)
5358

5459
corrplot(corr, add = TRUE, type = "lower", method = lower,
5560
diag = (diag == "l"),
56-
tl.pos = "n", cl.pos = "n", plotCI = plotCI_lower, ...)
61+
tl.pos = "n", cl.pos = "n", plotCI = plotCI_lower,
62+
col = lower.col, ...)
5763

5864
if (diag == "n" && tl.pos != "d") {
5965
symbols(1:n, n:1, add = TRUE, bg = bg, fg = addgrid.col,

inst/CITATION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ bibentry("Manual",
22
title="R package 'corrplot': Visualization of a correlation matrix",
33
author="Taiyun Wei and Viliam Simko",
44
year="2017",
5-
note="(Version 0.81)",
5+
note="(Version 0.82)",
66
url="https://github.com/taiyun/corrplot",
77

88
textVersion =
99
paste("T. Wei and V. Simko (2016). ",
10-
"R package 'corrplot': Visualization of a correlation matrix (Version 0.81). ",
10+
"R package 'corrplot': Visualization of a correlation matrix (Version 0.82). ",
1111
"Available from https://github.com/taiyun/corrplot",
1212
sep=""),
1313

inst/NEWS

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
CHANGES IN corrplot VERSION 0.81
2+
3+
CHANGES
4+
* Fixed #79: added parameter `win.asp` to control aspect ratio
5+
* Fixed #18: parameter `outline` combined with `method="pie"` controls
6+
the color of the otline circle of each pie.
7+
* updated vignette
8+
9+
110
CHANGES IN corrplot VERSION 0.80
211

312
CHANGES
4-
* Fixed #70, Enable to plot a matrix with NA when 'is.corr = F'
13+
* Fixed #70: Enable to plot a matrix with NA when 'is.corr = F'
514

615

716
CHANGES IN corrplot VERSION 0.77
817

918
CHANGES
10-
* Fixed #58, make sure the margin is correct in corrplot.mixed().
19+
* Fixed #58: make sure the margin is correct in corrplot.mixed().
1120
* Revised document.
1221

1322

man/corrplot-package.Rd

Lines changed: 26 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/corrplot.Rd

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/corrplot.mixed.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-corrplot.R

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ test_that("Issue #21: plotCI=rect incompatible with some methods", {
8686
L <- M - 0.1
8787
U <- M + 0.1
8888
expect_equal(corrplot.mixed(M, lower = "circle", upper = "number",
89-
low = L, upp = U, plotCI = "rect"), M)
89+
lowCI = L, uppCI = U, plotCI = "rect"), M)
9090
expect_equal(corrplot.mixed(M, lower = "number", upper = "circle",
91-
low = L, upp = U, plotCI = "rect"), M)
91+
lowCI = L, uppCI = U, plotCI = "rect"), M)
9292
expect_equal(corrplot.mixed(M, lower = "circle", upper = "square",
93-
low = L, upp = U, plotCI = "rect"), M)
93+
lowCI = L, uppCI = U, plotCI = "rect"), M)
9494
expect_equal(corrplot.mixed(M, lower = "ellipse", upper = "square",
95-
low = L, upp = U, plotCI = "rect"), M)
95+
lowCI = L, uppCI = U, plotCI = "rect"), M)
9696
expect_equal(corrplot.mixed(M, lower = "pie", upper = "square",
97-
low = L, upp = U, plotCI = "rect"), M)
97+
lowCI = L, uppCI = U, plotCI = "rect"), M)
9898
})
9999

100100
test_that("Issue #43: Return value should be the same as corrplot function", {
@@ -208,3 +208,10 @@ test_that("Issue #18", {
208208
expect_silent(corrplot(M, method = "pie", outline = TRUE))
209209
expect_silent(corrplot(M, method = "pie", outline = "white"))
210210
})
211+
212+
test_that("Issue #76: separate `col` parameters corrplot.mixed", {
213+
M <- cor(mtcars)
214+
expect_silent(corrplot.mixed(M, lower.col = "black"))
215+
expect_silent(corrplot.mixed(M, lower = "circle",
216+
upper = "number", upper.col = "black"))
217+
})

0 commit comments

Comments
 (0)