Skip to content

Commit bfb8e3a

Browse files
committed
Merge pull request #65 from vsimko/master
refactoring and fixing issue #64
2 parents adf809e + 0d65d39 commit bfb8e3a

6 files changed

Lines changed: 75 additions & 23 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: corrplot
22
Type: Package
33
Title: Visualization of a Correlation Matrix
4-
Version: 0.78
4+
Version: 0.79
55
Author: Taiyun Wei, Viliam Simko
66
Suggests:
77
seriation,

R/colorlegend.R

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' @param ratio.colbar The width ratio of colorbar to the total colorlegend
1111
#' (including colorbar, segments and labels).
1212
#' @param lim.segment Vector (quantile) of length 2, the elements should be in
13-
#' [-1,1], giving segments coordinates ranges.
13+
#' [0,1], giving segments coordinates ranges.
1414
#' @param align Character, alignment type of labels, \code{"l"} means left,
1515
#' \code{"c"} means center and \code{"r"} right.
1616
#' @param addlabels Logical, whether add text label or not.
@@ -20,24 +20,36 @@
2020
#' @keywords hplot
2121
#' @author Taiyun Wei
2222
#' @export
23-
colorlegend <- function(colbar, labels, at = NULL,
24-
xlim = c(0, 1), ylim = c(0, 1), vertical = TRUE, ratio.colbar = 0.4,
25-
lim.segment = NULL, align = c("c", "l", "r"), addlabels = TRUE,
26-
...) {
27-
28-
if (is.null(at) & addlabels) {
23+
colorlegend <- function(
24+
colbar,
25+
labels,
26+
at = NULL,
27+
xlim = c(0, 1),
28+
ylim = c(0, 1),
29+
vertical = TRUE,
30+
ratio.colbar = 0.4,
31+
lim.segment = NULL,
32+
align = c("c", "l", "r"),
33+
addlabels = TRUE,
34+
...)
35+
{
36+
if (is.null(at) && addlabels) {
2937
at <- seq(0L, 1L, length = length(labels))
3038
}
3139

3240
if (is.null(lim.segment)) {
33-
lim.segment <- ratio.colbar + c(0, ratio.colbar / 5)
41+
lim.segment <- ratio.colbar + c(0, ratio.colbar * .2)
3442
}
3543

36-
if (any(at < 0L) | any(at > 1L)) {
44+
if (any(at < 0L) || any(at > 1L)) {
3745
stop("at should be between 0 and 1")
3846
}
3947

40-
if (any(lim.segment < 0L) | any(lim.segment > 1L)) {
48+
if (length(lim.segment) != 2) {
49+
stop("lim.segment should be a vector of length 2")
50+
}
51+
52+
if (any(lim.segment < 0L) || any(lim.segment > 1L)) {
4153
stop("lim.segment should be between 0 and 1")
4254
}
4355

@@ -57,30 +69,30 @@ colorlegend <- function(colbar, labels, at = NULL,
5769
rep(xlim[1] + xgap * rat1, len), yyy[-1],
5870
col = colbar, border = colbar)
5971
rect(xlim[1], ylim[1], xlim[1] + xgap * rat1, ylim[2], border = "black")
60-
61-
pos.xlabel <- rep(xlim[1] + xgap * max(rat2, rat1), length(at))
6272
segments(xlim[1] + xgap * rat2[1], at, xlim[1] + xgap * rat2[2], at)
6373

6474
if (addlabels) {
75+
pos.xlabel <- rep(xlim[1] + xgap * max(rat2, rat1), length(at))
6576
switch(align,
6677
l = text(pos.xlabel, y = at, labels = labels, pos = 4, ...),
6778
r = text(xlim[2], y = at, labels = labels, pos = 2, ...),
6879
c = text((pos.xlabel + xlim[2]) / 2, y = at, labels = labels, ...),
6980
stop("programming error - should not have reached this line!")
7081
)
7182
}
72-
}
83+
} else {
7384

74-
if (!vertical) {
7585
at <- at * xgap + xlim[1]
7686
xxx <- seq(xlim[1], xlim[2], length = len + 1)
87+
7788
rect(xxx[1:len], rep(ylim[2] - rat1 * ygap, len),
78-
xxx[-1], rep(ylim[2], len), col = colbar, border = colbar)
89+
xxx[-1], rep(ylim[2], len),
90+
col = colbar, border = colbar)
7991
rect(xlim[1], ylim[2] - rat1 * ygap, xlim[2], ylim[2], border = "black")
80-
pos.ylabel <- rep(ylim[2] - ygap * max(rat2, rat1), length(at))
8192
segments(at, ylim[2] - ygap * rat2[1], at, ylim[2] - ygap * rat2[2])
8293

8394
if (addlabels) {
95+
pos.ylabel <- rep(ylim[2] - ygap * max(rat2, rat1), length(at))
8496
switch(align,
8597
l = text(x = at, y = pos.ylabel, labels = labels, pos = 1, ...),
8698
r = text(x = at, y = ylim[1], labels = labels, pos = 2, ...),

R/corrRect.hclust.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
#' @keywords hplot
2323
#' @author Taiyun Wei
2424
#' @export
25-
corrRect.hclust <- function(corr, k = 2, col = "black", lwd = 2,
25+
corrRect.hclust <- function(
26+
corr,
27+
k = 2,
28+
col = "black",
29+
lwd = 2,
2630
method = c("complete", "ward", "ward.D", "ward.D2", "single", "average",
2731
"mcquitty", "median", "centroid") )
2832
{
@@ -32,7 +36,10 @@ corrRect.hclust <- function(corr, k = 2, col = "black", lwd = 2,
3236
hc <- cutree(tree, k = k)
3337
clustab <- table(hc)[unique(hc[tree$order])]
3438
cu <- c(0, cumsum(clustab))
35-
mat <- cbind(cu[-(k + 1)] + 0.5, n - cu[-(k + 1)] + 0.5,
36-
cu[-1] + 0.5, n - cu[-1] + 0.5)
37-
rect(mat[,1], mat[,2], mat[,3], mat[,4], border = col, lwd = lwd)
39+
40+
rect(cu[-(k + 1)] + 0.5,
41+
n - cu[-(k + 1)] + 0.5,
42+
cu[-1] + 0.5,
43+
n - cu[-1] + 0.5,
44+
border = col, lwd = lwd)
3845
}

man/colorlegend.Rd

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

tests/testthat/test-colorlegend.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pdf(NULL)
66
test_that("Basic usage of colorlegend", {
77
plot(0, type = "n")
88
expect_silent(colorlegend(rainbow(100), 0:9))
9+
expect_silent(colorlegend(rainbow(100), 0:9, vertical = FALSE))
910
})
1011

1112
test_that("Calling colorlegend without first calling plot should fail", {
@@ -16,3 +17,35 @@ test_that("Calling colorlegend without first calling plot should fail", {
1617
expect_error(colorlegend(rainbow(100), 0:9),
1718
regexp = "plot.new has not been called yet")
1819
})
20+
21+
test_that("Issue #64: lim.segment in function colorlegend()", {
22+
plot(0, type = "n")
23+
24+
expect_error(colorlegend(rainbow(100), 0:9, lim.segment = 1),
25+
regexp = "should be a vector of length 2")
26+
27+
expect_error(colorlegend(rainbow(100), 0:9, lim.segment = c(1,2,3)),
28+
regexp = "should be a vector of length 2")
29+
30+
# lim.segment[1] >= 0
31+
expect_error(colorlegend(rainbow(100), 0:9, lim.segment = c(-0.1, 0)),
32+
regexp = "should be between 0 and 1")
33+
34+
# lim.segment[2] <= 1
35+
expect_error(colorlegend(rainbow(100), 0:9, lim.segment = c(0, 1.1)),
36+
regexp = "should be between 0 and 1")
37+
38+
# automatic lim.segment
39+
expect_silent(colorlegend(rainbow(100), 0:9, lim.segment = NULL))
40+
41+
expect_silent(colorlegend(rainbow(100), 0:9, lim.segment = c(0,1)))
42+
})
43+
44+
test_that("Parameter `at` should be between 0 and 1", {
45+
plot(0, type = "n")
46+
47+
expect_error(colorlegend(rainbow(100), 0:2, at = c(-1,.5,.8)),
48+
regexp = "should be between 0 and 1")
49+
50+
expect_silent(colorlegend(rainbow(100), 0:2, at = c(0,.5,.8)))
51+
})

tests/testthat/test-corrplot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test_that("Issue #20: plotmath expressions in rownames / colnames", {
3434
corrplot(M)
3535
})
3636

37-
test_that("Issues #21: plotCI=rect incompatible with some methods", {
37+
test_that("Issue #21: plotCI=rect incompatible with some methods", {
3838
M <- cor(mtcars)
3939
L <- M - 0.1
4040
U <- M + 0.1

0 commit comments

Comments
 (0)