Skip to content

Commit 3dfc1f3

Browse files
authored
Merge pull request #93 from michaellevy/master
Add method to mark significant correlations
2 parents a8c7d09 + 473baa8 commit 3dfc1f3

3 files changed

Lines changed: 88 additions & 30 deletions

File tree

R/corrplot.R

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,19 @@
152152
#'
153153
#' @param sig.level Significant level, if the p-value in \code{p-mat} is bigger
154154
#' than \code{sig.level}, then the corresponding correlation coefficient is
155-
#' regarded as insignificant.
155+
#' regarded as insignificant. If \code{insig} is \code{"label_sig"}, this may
156+
#' be an increasing vector of significance levels, in which case \code{pch}
157+
#' will be used once for the highest p-value interval and multiple times
158+
#' (e.g. "*", "**", "***") for each lower p-value interval.
156159
#'
157160
#' @param insig Character, specialized insignificant correlation coefficients,
158-
#' \code{"pch"} (default), \code{"p-value"}, \code{"blank"} or \code{"n"}. If
159-
#' \code{"blank"}, wipe away the corresponding glyphs; if \code{"p-value"},
160-
#' add p-values the corresponding glyphs; if \code{"pch"}, add characters (see
161-
#' \code{pch} for details) on corresponding glyphs; if \code{"n"}, don't take
162-
#' any measures.
161+
#' \code{"pch"} (default), \code{"p-value"}, \code{"blank"}, \code{"n"}, or
162+
#' \code{"label_sig"}. If \code{"blank"}, wipe away the corresponding glyphs;
163+
#' if \code{"p-value"}, add p-values the corresponding glyphs;
164+
#' if \code{"pch"}, add characters (see \code{pch} for details) on
165+
#' corresponding glyphs; if \code{"n"}, don't take any measures; if
166+
#' \code{"label_sig"}, mark significant correlations with pch
167+
#' (see \code{sig.level}).
163168
#'
164169
#' @param pch Add character on the glyphs of insignificant correlation
165170
#' coefficients(only valid when \code{insig} is \code{"pch"}). See
@@ -256,7 +261,7 @@ corrplot <- function(corr,
256261
shade.lwd = 1, shade.col = "white",
257262

258263
p.mat = NULL, sig.level = 0.05,
259-
insig = c("pch", "p-value", "blank", "n"),
264+
insig = c("pch", "p-value", "blank", "n", "label_sig"),
260265
pch = 4, pch.col = "black", pch.cex = 3,
261266

262267
plotCI = c("n", "square", "circle", "rect"),
@@ -791,26 +796,62 @@ corrplot <- function(corr,
791796
pos.pNew <- getPos.Dat(p.mat)[[1]]
792797
pNew <- getPos.Dat(p.mat)[[2]]
793798

794-
ind.p <- which(pNew > sig.level)
795-
p_inSig <- length(ind.p) > 0
799+
if (insig == "label_sig") {
800+
801+
# Unless another character is specified, mark sig with *
802+
if(!is.character(pch))
803+
pch <- "*"
804+
805+
place_points <- function(sig.locs, point)
806+
text(pos.pNew[,1][sig.locs], pos.pNew[,2][sig.locs],
807+
labels = point, col = pch.col, cex = pch.cex, lwd = 2)
808+
809+
if (length(sig.level) == 1) {
810+
place_points(sig.locs = which(pNew < sig.level), point = pch)
811+
812+
} else {
813+
l <- length(sig.level)
814+
for (i in seq_along(sig.level)) {
815+
iter <- l + 1 - i
816+
pchTmp <- paste(rep(pch, i), collapse = "")
817+
if(i == length(sig.level)) {
818+
locs <- which(pNew < sig.level[iter])
819+
if (length(locs))
820+
place_points(sig.locs = locs, point = pchTmp)
821+
} else {
822+
locs <- which(pNew < sig.level[iter] & pNew > sig.level[iter - 1])
823+
if(length(locs))
824+
place_points(sig.locs = locs, point = pchTmp)
825+
}
826+
827+
}
828+
}
796829

797-
if (insig == "pch" && p_inSig) {
798-
points(pos.pNew[,1][ind.p], pos.pNew[,2][ind.p],
799-
pch = pch, col = pch.col, cex = pch.cex, lwd = 2)
800-
}
830+
} else {
801831

802-
if (insig == "p-value" && p_inSig) {
803-
text(pos.pNew[,1][ind.p], pos.pNew[,2][ind.p],
804-
round(pNew[ind.p],2), col = pch.col)
805-
}
832+
ind.p <- which(pNew > sig.level)
833+
p_inSig <- length(ind.p) > 0
834+
835+
if (insig == "pch" && p_inSig) {
836+
points(pos.pNew[,1][ind.p], pos.pNew[,2][ind.p],
837+
pch = pch, col = pch.col, cex = pch.cex, lwd = 2)
838+
}
806839

807-
if (insig == "blank" && p_inSig) {
808-
symbols(pos.pNew[,1][ind.p], pos.pNew[,2][ind.p], inches = FALSE,
809-
squares = rep(1, length(pos.pNew[,1][ind.p])),
810-
fg = addgrid.col, bg = bg, add = TRUE)
840+
if (insig == "p-value" && p_inSig) {
841+
text(pos.pNew[,1][ind.p], pos.pNew[,2][ind.p],
842+
round(pNew[ind.p],2), col = pch.col)
843+
}
844+
845+
if (insig == "blank" && p_inSig) {
846+
symbols(pos.pNew[,1][ind.p], pos.pNew[,2][ind.p], inches = FALSE,
847+
squares = rep(1, length(pos.pNew[,1][ind.p])),
848+
fg = addgrid.col, bg = bg, add = TRUE)
849+
}
811850
}
812851
}
813852

853+
854+
814855
if (cl.pos != "n") {
815856
colRange <- assign.color(dat = cl.lim2)
816857
ind1 <- which(col == colRange[1])

man/corrplot.Rd

Lines changed: 14 additions & 9 deletions
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,15 @@ test_that("Issue #76: separate `col` parameters corrplot.mixed", {
215215
expect_silent(corrplot.mixed(M, lower = "circle",
216216
upper = "number", upper.col = "black"))
217217
})
218+
219+
test_that("Mark significant correlations", {
220+
M <- cor(mtcars)
221+
fakepmat <- 1 - abs(M) ^ .2 # Hmisc::rcorr provides a p-value matrix, but
222+
# don't want to introduce the dependency
223+
expect_silent(corrplot(M, p.mat = fakepmat, insig = "label_sig", pch = "!",
224+
sig.level = c(.001, .1, .99)))
225+
expect_silent(corrplot(M[1:2, ], p.mat = fakepmat[1:2, ], method = "ellipse",
226+
insig = "label_sig", pch.col = "white"))
227+
expect_silent(corrplot(M, p.mat = fakepmat, insig = "label_sig",
228+
pch = "p<.05", pch.cex = .5, order = "AOE"))
229+
})

0 commit comments

Comments
 (0)