Skip to content

Commit

Permalink
Merge pull request #81 from vsimko/master
Browse files Browse the repository at this point in the history
fixed issues #10 and #76
  • Loading branch information
vsimko authored Feb 17, 2017
2 parents c336f9d + 3f5537e commit c36ae27
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 87 deletions.
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
Package: corrplot
Type: Package
Title: Visualization of a Correlation Matrix
Version: 0.81
Date: 2017-02-07
Version: 0.82
Date: 2017-02-09
Author: Taiyun Wei, Viliam Simko
Suggests:
knitr,
testthat
Maintainer: Taiyun Wei <weitaiyun@gmail.com>
Description: A graphical display of a correlation matrix or general matrix.
It also contains some algorithms to do matrix reordering.
It also contains some algorithms to do matrix reordering. In addition,
corrplot is good at details, including choosing color, text labels,
color labels, layout, etc.
License: GPL
URL: https://github.com/taiyun/corrplot
BugReports: https://github.com/taiyun/corrplot/issues
Expand Down
43 changes: 43 additions & 0 deletions R/corrplot-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' @docType package
#' @name corrplot-package
#'
#' @title
#' Visualization of a correlation matrix
#'
#' @description
#' The corrplot package is a graphical display of a correlation matrix,
#' confidence interval or general matrix. It also contains some algorithms to do
#' matrix reordering. In addition, corrplot is good at details, including
#' choosing color, text labels, color labels, layout, etc.
#'
#' @author Taiyun Wei (weitaiyun@@gmail.com)
#' @author Viliam Simko (viliam.simko@@gmail.com)
#'
#' Maintainer: Taiyun Wei (weitaiyun@@gmail.com)
#'
#' @references
#' Michael Friendly (2002).
#' \emph{Corrgrams: Exploratory displays for correlation matrices}.
#' The American Statistician, 56, 316--324.
#'
#' D.J. Murdoch, E.D. Chow (1996).
#' \emph{A graphical display of large correlation matrices}.
#' The American Statistician, 50, 178--180.
#'
#' @seealso
#' The \code{plotcorr} function in the \code{ellipse} package and
#' \code{corrgram} function in the \code{corrgram} package has some
#' similarities.
#'
#' @keywords hplot
#' @keywords correlation
#' @keywords correlogram
#' @keywords feature selection
#' @keywords dimensionality reduction
NULL

.onAttach <- function(libname, pkgname) {
# just to show a startup message
message <- paste("corrplot", utils::packageVersion("corrplot"), "loaded")
packageStartupMessage(message, appendLF = TRUE)
}
74 changes: 44 additions & 30 deletions R/corrplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@
#' ARSA, BBURCG, BBWRCG, MDS, TSP, Chen and so forth.
#'
#' @example vignettes/example-corrplot.R
#' @keywords hplot
#' @import graphics grDevices stats
#' @export
corrplot <- function(corr,
Expand Down Expand Up @@ -394,8 +393,8 @@ corrplot <- function(corr,
apply_mat_filter <- function(mat) {
x <- matrix(1:n * m, n, m)
switch(type,
upper = mat[row(x) > col(x)] <- Inf,
lower = mat[row(x) < col(x)] <- Inf
upper = mat[row(x) > col(x)] <- Inf,
lower = mat[row(x) < col(x)] <- Inf
)

if (!diag) {
Expand All @@ -419,14 +418,14 @@ corrplot <- function(corr,
# we use this for rending NA cells differently
getPos.NAs <- function(mat) {
tmp <- apply_mat_filter(mat)
ind <- which(is.na(tmp), arr.ind = TRUE)
ind <- which(is.na(tmp), arr.ind = TRUE)
Pos <- ind
Pos[,1] <- ind[,2]
Pos[,2] <- -ind[,1] + 1 + n
return(Pos)
}

Pos <- getPos.Dat(corr)[[1]]
Pos <- getPos.Dat(corr)[[1]]

# rows
n2 <- max(Pos[,2])
Expand Down Expand Up @@ -460,11 +459,11 @@ corrplot <- function(corr,

## assign colors
assign.color <- function(dat = DAT, color = col){
newcorr <- (dat + 1) / 2
newcorr[newcorr <= 0] <- 0
newcorr[newcorr >= 1] <- 1 - 1e-16
newcorr <- (dat + 1) / 2
newcorr[newcorr <= 0] <- 0
newcorr[newcorr >= 1] <- 1 - 1e-16

color[floor(newcorr * length(color)) + 1] # new color returned
color[floor(newcorr * length(color)) + 1] # new color returned
}

col.fill <- assign.color()
Expand Down Expand Up @@ -503,39 +502,54 @@ corrplot <- function(corr,
## calculate label-text width approximately
if (!add) {
plot.new()
xlabwidth <- ylabwidth <- 0

# Issue #10: code from Sébastien Rochette (github user @statnmap)
xlabwidth <- max(strwidth(newrownames, cex = tl.cex))
ylabwidth <- max(strwidth(newcolnames, cex = tl.cex))
laboffset <- strwidth("W", cex = tl.cex) * tl.offset

# Issue #10
for (i in 1:50) {
xlim <- c(m1 - 0.5 - xlabwidth,
m2 + 0.5 + mm * cl.ratio * (cl.pos == "r"))
ylim <- c(n1 - 0.5 - nn * cl.ratio * (cl.pos == "b"),
n2 + 0.5 + ylabwidth)
plot.window(xlim + c(-0.2, 0.2), ylim + c(-0.2, 0.2), asp = 1,
xaxs = "i", yaxs = "i")
xlim <- c(
m1 - 0.5 - laboffset -
xlabwidth * (grepl("l", tl.pos) | grepl("d", tl.pos)),
m2 + 0.5 + mm * cl.ratio * (cl.pos == "r") +
xlabwidth * abs(cos(tl.srt * pi / 180)) * grepl("d", tl.pos)
) + c(-0.35, 0.15) +
c(-1,0) * grepl("l", tl.pos) # margin between text and grid

ylim <- c(
n1 - 0.5 - nn * cl.ratio * (cl.pos == "b"),
n2 + 0.5 + laboffset +
ylabwidth * abs(sin(tl.srt * pi / 180)) * grepl("t", tl.pos)
) +
c(-0.15, 0) +
c(0, -1) * (type == "upper") + # nasty hack
c(0,1) * grepl("d", tl.pos) # margin between text and grid

plot.window(xlim, ylim, asp = 1, xaxs = "i", yaxs = "i")

x.tmp <- max(strwidth(newrownames, cex = tl.cex))
y.tmp <- max(strwidth(newcolnames, cex = tl.cex))

if (min(x.tmp - xlabwidth, y.tmp - ylabwidth) < 0.0001) {
laboffset.tmp <- strwidth("W", cex = tl.cex) * tl.offset
if (max(x.tmp - xlabwidth,
y.tmp - ylabwidth,
laboffset.tmp - laboffset) < 1e-03) {
break
}

xlabwidth <- x.tmp
ylabwidth <- y.tmp
}

if (tl.pos == "n" || tl.pos == "d") {
xlabwidth <- ylabwidth <- 0
}
laboffset <- laboffset.tmp

if (tl.pos == "td") ylabwidth <- 0
if (tl.pos == "ld") xlabwidth <- 0

laboffset <- strwidth("W", cex = tl.cex) * tl.offset
xlim <- c(m1 - 0.5 - xlabwidth - laboffset,
m2 + 0.5 + mm * cl.ratio * (cl.pos == "r")) + c(-0.35, 0.15)
ylim <- c(n1 - 0.5 - nn * cl.ratio * (cl.pos == "b"),
n2 + 0.5 + ylabwidth * abs(sin(tl.srt * pi / 180)) + laboffset)
+ c(-0.15, 0.35)
if (i == 50) {
warning(c("Not been able to calculate text margin, ",
"please try again with a clean new empty window using ",
"{plot.new(); dev.off()} or reduce tl.cex"))
}
}

if (.Platform$OS.type == "windows") {
grDevices::windows.options(width = 7,
Expand Down
10 changes: 8 additions & 2 deletions R/corrplot.mixed.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#' \code{\link{corrplot}}
#' @param plotCI See the \code{plotCI} 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 \dots Additional arguments for corrplot's wrappers
#'
#' @author Taiyun Wei
Expand All @@ -29,6 +31,8 @@ corrplot.mixed <- function(
diag = c("n", "l", "u"),
bg = "white",
addgrid.col = "grey",
lower.col = NULL,
upper.col = NULL,
plotCI = c("n", "square", "circle", "rect"),
...)
{
Expand All @@ -49,11 +53,13 @@ corrplot.mixed <- function(
plotCI_upper <- adjust_plotCI(plotCI, upper)

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

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

if (diag == "n" && tl.pos != "d") {
symbols(1:n, n:1, add = TRUE, bg = bg, fg = addgrid.col,
Expand Down
4 changes: 2 additions & 2 deletions inst/CITATION
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ bibentry("Manual",
title="R package 'corrplot': Visualization of a correlation matrix",
author="Taiyun Wei and Viliam Simko",
year="2017",
note="(Version 0.81)",
note="(Version 0.82)",
url="https://github.com/taiyun/corrplot",

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

Expand Down
13 changes: 11 additions & 2 deletions inst/NEWS
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
CHANGES IN corrplot VERSION 0.81

CHANGES
* Fixed #79: added parameter `win.asp` to control aspect ratio
* Fixed #18: parameter `outline` combined with `method="pie"` controls
the color of the otline circle of each pie.
* updated vignette


CHANGES IN corrplot VERSION 0.80

CHANGES
* Fixed #70, Enable to plot a matrix with NA when 'is.corr = F'
* Fixed #70: Enable to plot a matrix with NA when 'is.corr = F'


CHANGES IN corrplot VERSION 0.77

CHANGES
* Fixed #58, make sure the margin is correct in corrplot.mixed().
* Fixed #58: make sure the margin is correct in corrplot.mixed().
* Revised document.


Expand Down
44 changes: 26 additions & 18 deletions man/corrplot-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/corrplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/corrplot.mixed.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions tests/testthat/test-corrplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ test_that("Issue #21: plotCI=rect incompatible with some methods", {
L <- M - 0.1
U <- M + 0.1
expect_equal(corrplot.mixed(M, lower = "circle", upper = "number",
low = L, upp = U, plotCI = "rect"), M)
lowCI = L, uppCI = U, plotCI = "rect"), M)
expect_equal(corrplot.mixed(M, lower = "number", upper = "circle",
low = L, upp = U, plotCI = "rect"), M)
lowCI = L, uppCI = U, plotCI = "rect"), M)
expect_equal(corrplot.mixed(M, lower = "circle", upper = "square",
low = L, upp = U, plotCI = "rect"), M)
lowCI = L, uppCI = U, plotCI = "rect"), M)
expect_equal(corrplot.mixed(M, lower = "ellipse", upper = "square",
low = L, upp = U, plotCI = "rect"), M)
lowCI = L, uppCI = U, plotCI = "rect"), M)
expect_equal(corrplot.mixed(M, lower = "pie", upper = "square",
low = L, upp = U, plotCI = "rect"), M)
lowCI = L, uppCI = U, plotCI = "rect"), M)
})

test_that("Issue #43: Return value should be the same as corrplot function", {
Expand Down Expand Up @@ -208,3 +208,10 @@ test_that("Issue #18", {
expect_silent(corrplot(M, method = "pie", outline = TRUE))
expect_silent(corrplot(M, method = "pie", outline = "white"))
})

test_that("Issue #76: separate `col` parameters corrplot.mixed", {
M <- cor(mtcars)
expect_silent(corrplot.mixed(M, lower.col = "black"))
expect_silent(corrplot.mixed(M, lower = "circle",
upper = "number", upper.col = "black"))
})
Loading

0 comments on commit c36ae27

Please sign in to comment.