corrplot/ 0000755 0001762 0000144 00000000000 14703205146 012120 5 ustar ligges users corrplot/tests/ 0000755 0001762 0000144 00000000000 14703127123 013260 5 ustar ligges users corrplot/tests/testthat/ 0000755 0001762 0000144 00000000000 14703205146 015122 5 ustar ligges users corrplot/tests/testthat/test-colorlegend.R 0000644 0001762 0000144 00000003665 14703115404 020526 0 ustar ligges users context('Color legend')
# suppress generating any PDFs
pdf(NULL)
test_that('Basic usage of colorlegend', {
plot(0, type = 'n')
colorlegend(rainbow(100), 0:9, align = 'r')
colorlegend(rainbow(100), 0:9, align = 'l', vertical = FALSE)
colorlegend(rainbow(100), 0:9, align = 'r', vertical = FALSE)
})
test_that('Calling colorlegend without first calling plot should fail', {
if (length(dev.list()) != 0) {
dev.off()
pdf(NULL)
}
expect_error(colorlegend(rainbow(100), 0:9),
regexp = 'plot.new has not been called yet')
})
test_that('Issues #64, #66: lim.segment in function colorlegend()', {
plot(0, type = 'n')
expect_error(colorlegend(rainbow(100), 0:9, lim.segment = 1),
regexp = 'should be a vector of length 2')
expect_error(colorlegend(rainbow(100), 0:9, lim.segment = c(1, 2, 3)),
regexp = 'should be a vector of length 2')
# lim.segment[1] >= 0
expect_error(colorlegend(rainbow(100), 0:9, lim.segment = c(-0.1, 0)),
regexp = 'should be between 0 and 1')
# lim.segment[2] <= 1
expect_error(colorlegend(rainbow(100), 0:9, lim.segment = c(0, 1.1)),
regexp = 'should be between 0 and 1')
# automatic lim.segment
expect_silent(colorlegend(rainbow(100), 0:9, lim.segment = NULL))
# Issue #66: more intuitive value for automatic lim.segment
expect_silent(colorlegend(rainbow(100), 0:9, lim.segment = 'auto'))
expect_error(colorlegend(rainbow(100), 0:9, lim.segment = 'otherstring'),
regexp = 'should be a vector of length 2')
colorlegend(rainbow(100), 0:9, lim.segment = c(0, 1), align ='l')
})
test_that('Parameter `at` should be between 0 and 1', {
plot(0, type = 'n')
expect_error(colorlegend(rainbow(100), 0:2, at = c(-1, 0.5, 0.8)),
regexp = 'should be between 0 and 1')
expect_silent(colorlegend(rainbow(100), 0:2, at = c(0, 0.5, 0.8)))
})
corrplot/tests/testthat/test-corrRect.R 0000644 0001762 0000144 00000004002 14703115404 017776 0 ustar ligges users context('Adding Correlation Rectangles')
# Tests ==========
test_that('Testing running', {
M = cor(mtcars)
r = rbind(c('gear', 'wt', 'qsec', 'carb'),
c('wt', 'qsec', 'carb', 'gear'))
i = c(1, 5, 11)
expect_silent(corrRect(corrplot(M, type = 'upper'), i))
expect_silent(corrRect(corrplot(M, type = 'lower'), i))
expect_silent(corrRect(corrplot(M), namesMat = c('gear', 'wt', 'qsec', 'carb')))
expect_silent(corrRect(corrplot(M), namesMat = r))
})
test_that('Testing error', {
M = cor(mtcars)
r = rbind(c('gear', 'wt', 'qsec', 'carb'),
c('wt', 'qsec', 'carb', 'gear'))
corrplot(M, method = 'circle', order = 'AOE', diag = TRUE, type='upper') -> p
expect_error(corrRect(namesMat = r, corrRes = p))
})
test_that('pipe operator', {
if(getRversion() >= '4.1.0') {
M = cor(mtcars)
r = c('gear', 'wt', 'qsec', 'carb')
corrplot(M, method = 'circle', order = 'AOE') |> corrRect(name = r)
}
})
test_that('You should just input one of index, name and namesMat!', {
M = cor(mtcars)
expect_error(corrRect(corrplot(M), index = c(1, 5, 11), name = c('gear', 'wt')),
regexp = 'You should just input one of index, name and namesMat!')
})
test_that('List \'corrRes\' must be inputted!', {
M = cor(mtcars)
corrplot(M)
expect_error(corrRect(), regexp = 'List \'corrRes\' must be inputted!')
})
test_that('Non-existent name found!', {
M = cor(mtcars)
expect_error(corrRect(corrplot(M), name = c('carb', 'gearssss')),
regexp = 'Non-existent name found!')
})
test_that('colnames and rownames must be same when index or name is inputted!', {
M = cor(mtcars)
expect_error(corrRect(corrplot(M[1:3, 4:6]), name = c('mpg', 'carb')),
regexp = 'colnames and rownames must be same when index or name is inputted!')
expect_error(corrRect(corrplot(M[1:3, 4:6]), index = c(1, 5, 11)),
regexp = 'colnames and rownames must be same when index or name is inputted!')
})
corrplot/tests/testthat/test-corrplot.R 0000644 0001762 0000144 00000023051 14703115404 020064 0 ustar ligges users context('Visualization of a correlation matrix')
# Tests ==========
test_that('Testing `cl.pos` parameter', {
M = cor(mtcars)
expect_silent(corrplot(M, cl.pos = TRUE, diag = FALSE))
expect_silent(corrplot(M, cl.pos = FALSE))
expect_silent(corrplot(M, cl.pos = 'r'))
expect_silent(corrplot(M, method = 'shade', addshade = 'all', cl.pos = 'b'))
})
test_that('Testing `tl.pos` parameter', {
M = cor(mtcars)
expect_silent(corrplot(M, tl.pos = TRUE))
expect_silent(corrplot(M, tl.pos = FALSE))
expect_silent(corrplot(M, tl.pos = 'd'))
expect_silent(corrplot(M, tl.pos = 'n'))
expect_silent(corrplot(M, tl.pos = 'lt'))
expect_silent(corrplot(M, tl.pos = 'ld', type = 'lower'))
expect_silent(corrplot(M, tl.pos = 'td', type = 'upper'))
})
test_that('Testing `col.lim` parameter', {
M = cor(mtcars)
expect_silent(corrplot(M, col.lim = c(-1, 1)))
expect_error(corrplot(M, col.lim = c(0, 1)),
regexp = 'color limits should cover matrix')
expect_error(corrplot(M, col.lim = c(-1, 2)),
regexp = 'col.lim should be within the interval')
})
test_that('Testing `tl.pos` parameter', {
M = cor(mtcars)
expect_silent(corrplot(M, tl.pos = 'td', type = 'upper'))
expect_error(corrplot(M, tl.pos = 'td', type = 'lower'),
regexp = 'type should be')
expect_silent(corrplot(M, tl.pos = 'ld', type = 'lower'))
expect_error(corrplot(M, tl.pos = 'ld', type = 'upper'),
regexp = 'type should be')
})
test_that('Testing `corrRect` function', {
M = cor(mtcars)
corrplot(M, method = 'circle', order = 'FPC')
expect_error(corrRect(c(5, 6)))
})
test_that('Testing `outline` parameter', {
M = cor(mtcars)
expect_silent(corrplot(M, outline = FALSE))
expect_silent(corrplot(M, outline = TRUE))
expect_silent(corrplot(M, outline = 'white'))
for (unsupported in list(42, NA, NULL)) {
expect_error(corrplot(M, outline = unsupported),
regexp = 'Unsupported value type for parameter outline')
}
expect_error(corrplot(M, outline = ''),
regexp = 'invalid color name')
})
test_that('Issue #7: Enable to plot a matrix with NA', {
M = cor(mtcars)
diag(M) = NA
expect_equal(corrplot(M)$corr, M)
})
test_that('Issue #20: plotmath expressions in rownames / colnames', {
M = cor(mtcars)[1:5, 1:5]
colnames(M) = c('alpha', 'beta', ':alpha+beta', ': a[0]', '=a[beta]')
rownames(M) = c('alpha', 'beta', NA, '$a[0]', '$ a[beta]')
corrplot(M)
})
test_that('Issue #20: using dollar sign in plotmath expressions', {
M = cor(mtcars)
rownames(M)[1] = ' $'
corrplot(M)
})
test_that('Issue #21: plotCI=rect incompatible with some methods', {
M = cor(mtcars)
L = M - 0.1
U = M + 0.1
expect_equal(corrplot.mixed(M, lower = 'circle', upper = 'number',
lowCI = L, uppCI = U, plotCI = 'rect')$corr, M)
})
test_that('Issue #43: Return value should be the same as corrplot function', {
M = cor(mtcars)
expect_equal(corrplot.mixed(M)$corr, corrplot(M)$corr)
})
test_that('Should only work with matrix or dataframe', {
expect_error(corrplot('some string'), regexp = 'matrix or data frame')
expect_error(corrplot(42), regexp = 'matrix or data frame')
})
test_that('Non-correlation matrix', {
M = matrix(runif(100, 0, 10), nrow = 10)
expect_error(corrplot(M), regexp = 'The matrix is not in')
expect_true(is.matrix(corrplot(M, is.corr = FALSE)$corr))
expect_true(is.data.frame(corrplot(M, is.corr = FALSE)$corrPos))
})
test_that('Try different ordering', {
M = cor(mtcars)
expect_true(identical(M, corrplot(M)$corr))
expect_false(identical(M, corrplot(M, order = 'AOE')))
expect_false(identical(M, corrplot(M, order = 'FPC')))
expect_false(identical(M, corrplot(M, order = 'hclust')))
expect_false(identical(M, corrplot(M, order = 'alphabet')))
expect_silent(corrplot(M, addrect = 2, order = 'hclust', type = 'full'))
})
test_that('Plot without a grid should not crash', {
# TODO: currently, we cannot test how the rendered grid looks like
# we can only check whether it does not crash
M = cor(mtcars)
# without grid
expect_silent(corrplot(M, addgrid.col = NA))
# white grid
expect_silent(corrplot(M, addgrid.col = NULL, method = 'color'))
expect_silent(corrplot(M, addgrid.col = NULL, method = 'shade'))
# grey grid
expect_silent(corrplot(M, addgrid.col = NULL, method = 'circle'))
})
test_that('Issue #46: Rendering NA values', {
M = cor(mtcars)
diag(M) = NA
M[4, 2] = NA
# default label for NAs
expect_silent(corrplot(M))
# black square instead of the label
expect_silent(corrplot(M, na.label = 'square', na.label.col = 'black'))
# large matrix
M = matrix(runif(10000, 0.5, 1), nrow = 100)
M[40:50, 30:70] = 0
diag(M) = NA
expect_silent(corrplot(M, method = 'color', cl.pos = 'n', tl.pos = 'n',
na.label = 'square', addgrid.col = NA))
})
test_that('Issue #55: Support for multiple characters when rendering NAs', {
M = cor(mtcars)
diag(M) = NA
# label with 2 chars should work
expect_silent(corrplot(M, na.label = 'NA'))
expect_error(corrplot(M, na.label = 'ABC'),
regexp = 'Maximum number of characters for NA label is: 2')
})
test_that('Using `number.digits` parameter', {
M = cor(mtcars)
expect_silent(corrplot(M, number.digits = 0))
expect_silent(corrplot(M, number.digits = 1))
# the allowed values for nsmall parameter of format() function is [0, 20]
expect_silent(corrplot(M, method = 'number', number.digits = 20))
expect_error(corrplot(M, number.digits = 1.2), regexp = 'is not TRUE')
expect_error(corrplot(M, number.digits = -1), regexp = 'is not TRUE')
})
test_that('par() is restored after corrplot()', {
grDevices::pdf(NULL)
par1 = par('mar')
corrplot(cor(mtcars))
par2 = par('mar')
dev.off()
expect_identical(par1, par2)
})
test_that('Issue #79: Changing aspect ratio for the plot', {
M = matrix(rnorm(70), ncol = 7)
expect_silent(corrplot(M, is.corr = FALSE, win.asp = .7, method = 'circle'))
expect_silent(corrplot(M, is.corr = FALSE, win.asp = .7, method = 'square'))
expect_error(corrplot(M, is.corr = FALSE, win.asp = .7, method = 'pie'),
regexp = 'supported only for circle and square methods')
})
test_that('Issue #18', {
M = cor(mtcars)
# TODO: calling the function without actually checking anything
expect_silent(corrplot(M, method = 'pie', is.corr = FALSE, diag = FALSE))
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'))
})
test_that('Issue #99: Mark significant correlations', {
M = cor(mtcars)
fakepmat = 1 - abs(M) ^ 0.2 # Hmisc::rcorr provides a p-value matrix, but
# don't want to introduce the dependency
expect_silent(corrplot(M, p.mat = fakepmat, insig = 'label_sig', pch = '!',
sig.level = c(0.001, 0.1, 0.99)))
expect_silent(corrplot(M[1:2, ], p.mat = fakepmat[1:2, ], method = 'ellipse',
insig = 'label_sig', pch.col = 'white'))
expect_silent(corrplot(M, p.mat = fakepmat, insig = 'label_sig',
pch = 'p<.05', pch.cex = 0.5, order = 'AOE'))
expect_warning(corrplot(M, p.mat = fakepmat[, 11:1], insig = 'label_sig',
pch = 'p<.05', pch.cex = 0.5, order = 'AOE'))
})
test_that('col.lim', {
M = cor(mtcars)
expect_warning(corrplot(M*2, is.corr = FALSE, col.lim=c(-2, 2) * 2))
expect_warning(corrplot(abs(M), is.corr = FALSE, col.lim=c(-1, 1)))
})
test_that("plotCI == 'circle'", {
M = cor(mtcars)
res = cor.mtest(mtcars, conf.level = 0.95)
## plot confidence interval(0.95), 'circle' method
corrplot(M, p.mat = res$p, low = res$lowCI, upp = res$uppCI,
plotCI = 'circle', addg = 'grey20', cl.pos = 'n')
expect_error(corrplot(M, p.mat = res$p, plotCI = 'circle'),
regexp = 'Need lowCI.mat and uppCI.mat!')
})
test_that("plotCI == 'square'", {
M = cor(mtcars)
res = cor.mtest(mtcars, conf.level = 0.95)
## plot confidence interval(0.95), 'circle' method
corrplot(M, p.mat = res$p, low = res$lowCI, upp = res$uppCI, order = 'AOE',
plotCI = 'square', addg = 'grey20', cl.pos = 'n')
})
test_that("add numbers", {
M = cor(mtcars)
corrplot(M, addCoef.col ='black')
})
test_that("p-value", {
M = cor(mtcars)
testRes = cor.mtest(mtcars, conf.level = 0.95)
## specialized the insignificant value according to the significant level
corrplot(M, p.mat = testRes$p, sig.level = 0.10, order = 'hclust', addrect = 2)
## leave blank on no significant coefficient
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig='blank',
addCoef.col ='black', number.cex = 0.8, order = 'AOE', diag = FALSE)
## add p-values on no significant coefficients
corrplot(M, p.mat = testRes$p, insig = 'p-value')
})
test_that("if (diag == 'n' && tl.pos != 'd')", {
M = cor(mtcars)
corrplot.mixed(M, diag = 'n', tl.pos = 'lt')
})
test_that('long name warning', {
M = cor(mtcars)
colnames(M) = rownames(M) = rep(toString(rep(LETTERS, 5)), 11)
expect_warning(corrplot(M), regexp = 'Not been able to calculate text margin')
})
corrplot/tests/testthat/test-cor-mtest.R 0000644 0001762 0000144 00000001263 14703115404 020136 0 ustar ligges users context('cor.mtest - significance test and confidence intervals')
test_that('Basic usage of cor.mtest', {
res1 = cor.mtest(mtcars)
expect_true(is.list(res1))
expect_true(is.matrix(res1$p))
expect_true(is.matrix(res1$lowCI))
expect_true(is.matrix(res1$uppCI))
expect_equal(dim(res1$p), c(11, 11))
expect_equal(dim(res1$p), dim(res1$lowCI))
expect_equal(dim(res1$p), dim(res1$uppCI))
})
test_that('Additional params', {
expect_silent(cor.mtest(mtcars, conf.level = 0.95))
expect_silent(cor.mtest(mtcars, method = 'spearman', exact = FALSE))
# unknown parameters are silently ignored
expect_silent(cor.mtest(mtcars, dummy = 'dummy'))
})
corrplot/tests/testthat.R 0000644 0001762 0000144 00000000100 14703115404 015231 0 ustar ligges users library(testthat)
library(corrplot)
test_check('corrplot')
corrplot/MD5 0000644 0001762 0000144 00000004367 14703205146 012442 0 ustar ligges users ac52b9ff5042987af891fb938584cfad *DESCRIPTION
32815a1a57a48b5607d42c181f8ab669 *LICENSE
8ebba318bd58185ad6486cbb5bf9fe14 *NAMESPACE
26923f4a66e6b8b8747f2632069ebe2c *NEWS.md
f65aebbac0d79ebed47688df1d9acd32 *R/colorlegend.R
8f938ea675042f8279033801701e0af9 *R/colors.R
13c2da4738fdf920856e533d91ee6b83 *R/cor-mtest.R
574c08dad2972b5ea641d69445c722ed *R/corrMatOrder.R
0bc92f234380dd1546bd6e765e8e4981 *R/corrRect.R
a55dac41056a16401b6cb33b73230ded *R/corrRect.hclust.R
3527af3ec785895f610eff6790346976 *R/corrplot-package.R
1127fe6fe22d532061d58bdbda01f732 *R/corrplot.R
9ff8e62a8ea6dcebc96f98648145021c *R/corrplot.mixed.R
d3c029ea349e8c24b42c2c55929d48cb *README.md
aa3d04b24715ec5b58186438334ab378 *build/vignette.rds
06455ae94bf823e3cf37543dae416ae7 *inst/CITATION
d41d8cd98f00b204e9800998ecf8427e *inst/NEWS
3027bf8f119816998b328fbc1ffec663 *inst/doc/corrplot-intro.R
4dda5c35b1de86d073ca3ccbdab657cd *inst/doc/corrplot-intro.Rmd
88a879078a453bc5d9db13f4ad238e3a *inst/doc/corrplot-intro.html
9e89b00cf85d47ec576187c0803c14a7 *man/COL1.Rd
473315e1a4fab39674356b586bc9538c *man/COL2.Rd
6fa9264802e06e971f51c5199a769989 *man/colorlegend.Rd
a82a46c544e6081237d8b4705d7e55ee *man/cor.mtest.Rd
d8e4360a7d146904711f581b285fa8f7 *man/corrMatOrder.Rd
12fd1abd8d95e9cdfe3b3546e80fdf58 *man/corrRect.Rd
a99a8225c177af0d859307a646e45e40 *man/corrRect.hclust.Rd
0b2033069ff2f0cdc4bdb6ece9f30b04 *man/corrplot-package.Rd
aed8721bb00f46fb641466b5e1cee70f *man/corrplot.Rd
612a3af17b90d78c995cb6a1172494ae *man/corrplot.mixed.Rd
09fa6ded1663d8f1f792b6898d04d656 *tests/testthat.R
21ab84cb8449eaf25880f36e897f9799 *tests/testthat/test-colorlegend.R
0eb26cdeb3bf8b1cd2a119bac116a92a *tests/testthat/test-cor-mtest.R
366b9cfd05c33ccde3e240f74be96e3f *tests/testthat/test-corrRect.R
a5255e6295e61010b1b45baa3027b3f2 *tests/testthat/test-corrplot.R
4dda5c35b1de86d073ca3ccbdab657cd *vignettes/corrplot-intro.Rmd
d2c1008bcf0833a729dcb1207a4fa72f *vignettes/example-colorlegend.R
6b04e2c72644202d2dbb7055d13d4053 *vignettes/example-corrMatOrder.R
2d0bcd452e1abb5c2b8a89f56424e031 *vignettes/example-corrRect.R
43278c2632ba64d2361cc0496689dd23 *vignettes/example-corrRect.hclust.R
e6fa8ff6600255e54ffbd65fedb74ed8 *vignettes/example-corrplot.R
4447a65dc5d32a42cc266469d8185734 *vignettes/example-corrplot.mixed.R
corrplot/R/ 0000755 0001762 0000144 00000000000 14703127123 012317 5 ustar ligges users corrplot/R/corrMatOrder.R 0000644 0001762 0000144 00000005337 14703115404 015054 0 ustar ligges users #' Reorder a correlation matrix.
#'
#' Draw rectangle(s) around the chart of corrrlation matrix based on the number
#' of each cluster's members.
#'
#' @param corr Correlation matrix to reorder.
#'
#' @param order Character, the ordering method for the correlation matrix.
#' \itemize{
#' \item{\code{'AOE'} for the angular order of the eigenvectors.
#' It is calculated from the order of the angles, \eqn{a_i}:
#' \deqn{ a_i = arctan (e_{i2} / e_{i1}), if e_{i1} > 0}
#' \deqn{ a_i = arctan (e_{i2} / e_{i1}) + \pi, otherwise.}
#' where \eqn{e_1} and \eqn{e_2} are the largest two eigenvalues
#' of matrix \code{corr}.
#' See Michael Friendly (2002) for details.}
#' \item{\code{'FPC'} for the first principal component order.}
#' \item{\code{'hclust'} for hierarchical clustering order.}
#' \item{\code{'alphabet'} for alphabetical order.}
#' }
#'
#' @param hclust.method Character, the agglomeration method to be used when
#' \code{order} is \code{hclust}. This should be one of \code{'ward'},
#' \code{'ward.D'}, \code{'ward.D2'}, \code{'single'}, \code{'complete'},
#' \code{'average'}, \code{'mcquitty'}, \code{'median'} or \code{'centroid'}.
#'
#' @return Returns a single permutation vector.
#'
#' @seealso Package \code{seriation} offers more methods to reorder matrices,
#' such as ARSA, BBURCG, BBWRCG, MDS, TSP, Chen and so forth.
#'
#' @example vignettes/example-corrMatOrder.R
#' @author Taiyun Wei
#' @keywords hplot
#' @export
corrMatOrder = function(
corr,
order = c('AOE', 'FPC', 'hclust', 'alphabet'),
hclust.method = c('complete', 'ward', 'ward.D', 'ward.D2', 'single',
'average', 'mcquitty', 'median', 'centroid'))
{
order = match.arg(order)
hclust.method = match.arg(hclust.method)
switch(order,
AOE = reorder_using_aoe(corr),
FPC = reorder_using_fpc(corr),
hclust = reorder_using_hclust(corr, hclust.method),
alphabet = sort(rownames(corr))
)
}
#' Reorder the variables using the angular order of the eigenvectors.
#' @noRd
reorder_using_aoe = function(corr) {
x.eigen = eigen(corr)$vectors[, 1:2]
e1 = x.eigen[, 1]
e2 = x.eigen[, 2]
alpha = ifelse(e1 > 0, atan(e2 / e1), atan(e2 / e1) + pi)
order(alpha) # returned vector
}
#' Reorder the variables using the first principal component.
#' @noRd
reorder_using_fpc = function(corr) {
x.eigen = eigen(corr)$vectors[, 1:2]
e1 = x.eigen[, 1]
order(e1) # returned vector
}
#' Reorder the variables using hclust (Hierarchical Clustering).
#' @noRd
reorder_using_hclust = function(corr, hclust.method) {
hc = hclust(as.dist(1 - corr), method = hclust.method)
order.dendrogram(as.dendrogram(hc)) # returned vector
}
corrplot/R/corrRect.R 0000644 0001762 0000144 00000010236 14703115404 014226 0 ustar ligges users #' Draw rectangle(s) on the correlation matrix graph.
#'
#' Draw rectangle(s) after the correlation matrix plotted. SUGGESTION: It's more convenient
#' to draw rectangle(s) by using pipe operator `|>` since R 4.1.0.
#'
#' \code{corrRect} needs one of \code{index}, \code{name} and \code{namesMat} inputted.
#' While \code{corrRect.hclust} can get the members in each cluster
#' based on hierarchical clustering (\code{\link{hclust}}).
#'
#' @param corrRes List of the \code{corrplot()} returns.
#' @param index Vector, variable index of diag rect \code{c(Rect1from, Rect2from,
#' Rect3from, ..., RectNto)} on the correlation matrix graph.
#' It works when the colnames are the same as rownames, or both of them is NULL.
#' It needs \code{corrRes} inputted.
#' @param name Vector, variable name of diag rect \code{c(Rect1from, Rect2from,
#' Rect3from, ..., RectNto)} on the correlation matrix graph.
#' OIt works when the colnames are the same as rownames.
#' It needs \code{corrRes} inputted.
#' @param namesMat 4-length character vector or 4-columns character matrix,
#' represents the names of xleft, ybottom, xright, ytop correspondingly.
#' It needs \code{corrRes} inputted.
#' @param col Color of rectangles.
#' @param lwd Line width of rectangles.
#' @param \dots Additional arguments passing to function \code{rect()}.
#'
#' @return (Invisibly) returns input parameter \code{corrRes},
#' usually \code{list(corr, corrTrans, arg)}.
#'
#' @example vignettes/example-corrRect.R
#' @keywords hplot
#' @author Taiyun Wei
#' @export
corrRect = function(corrRes = NULL, index = NULL, name = NULL, namesMat = NULL,
col = 'black', lwd = 2, ...)
{
if((as.integer(!is.null(index)) + as.integer(!is.null(name)) +
as.integer(!is.null(namesMat))) > 1) {
stop('You should just input one of index, name and namesMat!')
}
if(is.null(corrRes)|!is.list(corrRes)) {
stop('List \'corrRes\' must be inputted!')
}
corr = corrRes$corr
corrPos = corrRes$corrPos
type = corrRes$arg$type
cName = colnames(corr)
rName = rownames(corr)
if(!is.null(name)) {
if(any(cName != rName)) {
stop('colnames and rownames must be same when index or name is inputted!')
}
if(!all(name %in% cName)) {
stop('Non-existent name found!')
}
index = unlist(lapply(name, function(n) which(cName==n)))
}
if(!is.null(index)) {
if(any(cName != rName)) {
stop('colnames and rownames must be same when index or name is inputted!')
}
n = length(index)
index[-n] = index[-n] - 1
x1 = index[-n] + 0.5
y1 = nrow(corr) - index[-n] + 0.5
x2 = index[-1] + 0.5
y2 = nrow(corr) - index[-1] + 0.5
St = S = cbind(c(x1, x1, x2, x2), c(y1, y1, y2, y2),
c(x2, x1, x2, x1), c(y1, y2, y1, y2))
St[, 2] = abs(St[, 2] - nrow(corr) - 1)
St[, 4] = abs(St[, 4] - nrow(corr) - 1)
if(type=='upper') {
i = which((St[, 1] - St[, 2]) > -0.1 & (St[, 3] - St[, 4]) > -0.1)
S = S[i, ]
}
if(type=='lower') {
i = which((St[, 2] - St[, 1]) > -0.1 & (St[, 4] - St[, 3]) > -0.1)
S = S[i, ]
}
segments(S[, 1], S[, 2], S[, 3], S[, 4], col = col, lwd = lwd, ...)
}
if(!is.null(namesMat)) {
if(is.vector(namesMat)) {
namesMat = matrix(namesMat, ncol = 4, nrow = 1)
}
xy1 = getCharXY(namesMat[, 1:2, drop=FALSE], corrPos)
xy2 = getCharXY(namesMat[, 3:4, drop=FALSE], corrPos)
xy = cbind(xy1, xy2)
x1 = apply(xy[, c(1, 3), drop=FALSE], 1, min) - 0.5
y1 = apply(xy[, c(2, 4), drop=FALSE], 1, min) - 0.5
x2 = apply(xy[, c(1, 3), drop=FALSE], 1, max) + 0.5
y2 = apply(xy[, c(2, 4), drop=FALSE], 1, max) + 0.5
rect(x1, y1, x2, y2, border = col, lwd = lwd, ...)
}
invisible(corrRes)
}
#' @noRd
getCharXY = function(x, dat) {
res = apply(x, 1, function(n, d=dat) d[d[, 1]==n[1]&d[, 2]==n[2], 3:4])
f = which(unlist(lapply(res, nrow))==0)
if(length(f) > 0) {
error = paste(toString(unique(x[f, ])), 'paired X-Y names were not found!')
stop(error)
}
return(matrix(unlist(res), byrow = TRUE, ncol = 2))
}
corrplot/R/cor-mtest.R 0000644 0001762 0000144 00000003252 14703115404 014360 0 ustar ligges users #' Significance test which produces p-values and confidence intervals for each
#' pair of input features.
#'
#' @param mat Input matrix of size \code{NxF},
#' with \code{N} rows that represent samples
#' and \code{F} columns that represent features.
#' @param \dots Additional arguments passed to function \code{\link{cor.test}},
#' e.g. \code{conf.level = 0.95}.
#'
#' @return Return a list containing:
#' \item{p}{Square matrix of size \code{FxF} with p-values as cells}
#' \item{lowCI}{Square matrix of size \code{FxF}, each cell represents the
#' \emph{lower part} of a confidence interval}
#' \item{uppCI}{Square matrix of size \code{FxF}, each cell represents the
#' \emph{upper part} of a confidence interval}
#'
#' @seealso Function \code{\link{cor.test}}
#'
#' @keywords p-value confidence significance
#' @export
cor.mtest = function(mat, ...) {
mat = as.matrix(mat)
n = ncol(mat)
p.mat = lowCI.mat = uppCI.mat = matrix(NA, n, n)
diag(p.mat) = 0
diag(lowCI.mat) = diag(uppCI.mat) = 1
for (i in 1:(n - 1)) {
for (j in (i + 1):n) {
tmp = cor.test(x = mat[, i], y = mat[, j], ...)
p.mat[i, j] = p.mat[j, i] = tmp$p.value
# only 'pearson' method provides confidence intervals
if (!is.null(tmp$conf.int)) {
lowCI.mat[i, j] = lowCI.mat[j, i] = tmp$conf.int[1]
uppCI.mat[i, j] = uppCI.mat[j, i] = tmp$conf.int[2]
}
}
}
colnames(p.mat) = rownames(p.mat) = colnames(mat)
colnames(lowCI.mat) = rownames(lowCI.mat) = colnames(mat)
colnames(uppCI.mat) = rownames(uppCI.mat) = colnames(mat)
list(
p = p.mat,
lowCI = lowCI.mat,
uppCI = uppCI.mat
)
}
corrplot/R/corrRect.hclust.R 0000644 0001762 0000144 00000003070 14703115404 015525 0 ustar ligges users #' Draw rectangles on the correlation matrix graph.
#'
#' Draw rectangles on the correlation matrix graph based on hierarchical cluster
#' (\code{\link{hclust}}).
#'
#' @param corr Correlation matrix for function \code{corrRect.hclust}. It use
#' \code{1-corr} as dist in hierarchical clustering (\code{\link{hclust}}).
#'
#' @param k Integer, the number of rectangles drawn on the graph according to
#' the hierarchical cluster, for function \code{corrRect.hclust}.
#'
#' @param col Color of rectangles.
#' @param lwd Line width of rectangles.
#'
#' @param method Character, the agglomeration method to be used for hierarchical
#' clustering (\code{\link{hclust}}). This should be (an unambiguous
#' abbreviation of) one of \code{'ward'}, \code{'ward.D'}, \code{'ward.D2'},
#' \code{'single'}, \code{'complete'}, \code{'average'}, \code{'mcquitty'},
#' \code{'median'} or \code{'centroid'}.
#'
#' @example vignettes/example-corrRect.hclust.R
#' @keywords hplot
#' @author Taiyun Wei
#' @export
corrRect.hclust = function(
corr,
k = 2,
col = 'black',
lwd = 2,
method = c('complete', 'ward', 'ward.D', 'ward.D2', 'single', 'average',
'mcquitty', 'median', 'centroid'))
{
n = nrow(corr)
method = match.arg(method)
tree = hclust(as.dist(1 - corr), method = method)
hc = cutree(tree, k = k)
clustab = table(hc)[unique(hc[tree$order])]
cu = c(0, cumsum(clustab))
rect(cu[-(k + 1)] + 0.5,
n - cu[-(k + 1)] + 0.5,
cu[-1] + 0.5,
n - cu[-1] + 0.5,
border = col, lwd = lwd)
}
corrplot/R/colorlegend.R 0000644 0001762 0000144 00000006616 14703115404 014747 0 ustar ligges users #' Draw color legend.
#'
#' @param colbar Vector, color of colbar.
#' @param labels Vector, numeric or character to be written.
#' @param at Numeric vector (quantile), the position to put labels. See examples
#' for details.
#' @param xlim See in \code{\link{plot}}
#' @param ylim See in \code{\link{plot}}
#' @param vertical Logical, whether the colorlegend is vertical or horizon.
#' @param ratio.colbar The width ratio of colorbar to the total colorlegend
#' (including colorbar, segments and labels).
#' @param lim.segment Vector (quantile) of length 2, the elements should be in
#' [0,1], giving segments coordinates ranges. If the value is NULL or 'auto',
#' then the ranges are derived automatically.
#' @param align Character, alignment type of labels, \code{'l'} means left,
#' \code{'c'} means center and \code{'r'} right.
#' Only valid when \code{vertical} is \code{TRUE}.
#' @param addlabels Logical, whether add text label or not.
#' @param \dots Additional arguments, passed to \code{\link{plot}}
#'
#' @example vignettes/example-colorlegend.R
#' @keywords hplot
#' @author Taiyun Wei
#' @export
colorlegend = function(
colbar,
labels,
at = NULL,
xlim = c(0, 1),
ylim = c(0, 1),
vertical = TRUE,
ratio.colbar = 0.4,
lim.segment = 'auto', # NOTE: NULL treated as 'auto'
align = c('c', 'l', 'r'),
addlabels = TRUE,
...)
{
if (is.null(at) && addlabels) {
at = seq(0L, 1L, length = length(labels))
}
if (any(is.null(lim.segment)) || any(lim.segment == 'auto')) {
lim.segment = ratio.colbar + c(0, ratio.colbar * .2)
}
if (any(at < 0L) || any(at > 1L)) {
stop('at should be between 0 and 1')
}
if (length(lim.segment) != 2) {
stop('lim.segment should be a vector of length 2')
}
if (any(lim.segment < 0L) || any(lim.segment > 1L)) {
stop('lim.segment should be between 0 and 1')
}
align = match.arg(align)
xgap = diff(xlim)
ygap = diff(ylim)
len = length(colbar)
rat1 = ratio.colbar
rat2 = lim.segment
if (vertical) {
at = at * ygap + ylim[1]
yyy = seq(ylim[1], ylim[2], length = len + 1)
rect(rep(xlim[1], len), yyy[1:len],
rep(xlim[1] + xgap * rat1, len), yyy[-1],
col = colbar, border = colbar)
rect(xlim[1], ylim[1], xlim[1] + xgap * rat1, ylim[2], border = 'black')
segments(xlim[1] + xgap * rat2[1], at, xlim[1] + xgap * rat2[2], at)
if (addlabels) {
pos.xlabel = rep(xlim[1] + xgap * max(rat2, rat1), length(at))
if(align == 'l') {
text(pos.xlabel, y = at, labels = labels, pos = 4, ...)
}
if(align == 'r') {
text(xlim[2], y = at, labels = labels, pos = 2, ...)
}
if(align == 'c') {
text((pos.xlabel + xlim[2]) / 2, y = at, labels = labels, ...)
}
}
} else {
at = at * xgap + xlim[1]
xxx = seq(xlim[1], xlim[2], length = len + 1)
rect(xxx[1:len], rep(ylim[2] - rat1 * ygap, len),
xxx[-1], rep(ylim[2], len),
col = colbar, border = colbar)
rect(xlim[1], ylim[2] - rat1 * ygap, xlim[2], ylim[2], border = 'black')
segments(at, ylim[2] - ygap * rat2[1], at, ylim[2] - ygap * rat2[2])
if (addlabels) {
pos.ylabel = rep(ylim[2] - ygap * max(rat2, rat1), length(at))
text(x = at, y = pos.ylabel, labels = labels, pos = 1, ...)
}
}
}
corrplot/R/corrplot-package.R 0000644 0001762 0000144 00000002611 14703115404 015676 0 ustar ligges users #' @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)
}
corrplot/R/colors.R 0000644 0001762 0000144 00000010051 14703115404 013737 0 ustar ligges users #' Get diverging colors
#'
#' Get diverging colors from palette theme name and n. The color palettes
#' are from RColorBrewer, but with the middle color changing to '#FFFFFF'(white),
#' thus we can visualize element 0 with white color.
#' Diverging colors are suitable for visualize a matrix which elements are partly positive and
#' partly negative (e.g. correlation matrix in [-1, 1], or [-20, 100]).
#'
#' @param diverging Diverging color Palettes
#' @param n the number of colors (>= 1) to be in the palette.
#'
#' @return A character vector containing color names
#'
#' @seealso Function \code{\link{colorRampPalette}}, package \code{RColorBrewer}
#'
#' @keywords color
#' @example vignettes/example-colorlegend.R
#' @export
COL2 = function(diverging = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu'),
n = 200) {
diverging = match.arg(diverging)
colors = switch(
diverging,
RdBu = c('#67001F', '#B2182B', '#D6604D', '#F4A582', '#FDDBC7', '#FFFFFF',
'#D1E5F0', '#92C5DE', '#4393C3', '#2166AC', '#053061'),
BrBG = c('#543005', '#8C510A', '#BF812D', '#DFC27D', '#F6E8C3', '#FFFFFF',
'#C7EAE5', '#80CDC1', '#35978F', '#01665E', '#003C30'),
PiYG = c('#8E0152', '#C51B7D', '#DE77AE', '#F1B6DA', '#FDE0EF', '#FFFFFF',
'#E6F5D0', '#B8E186', '#7FBC41', '#4D9221', '#276419'),
PRGn = c('#40004B', '#762A83', '#9970AB', '#C2A5CF', '#E7D4E8', '#FFFFFF',
'#D9F0D3', '#A6DBA0', '#5AAE61', '#1B7837', '#00441B'),
PuOr = c('#7F3B08', '#B35806', '#E08214', '#FDB863', '#FEE0B6', '#FFFFFF',
'#D8DAEB', '#B2ABD2', '#8073AC', '#542788', '#2D004B'),
RdYlBu = c('#A50026', '#D73027', '#F46D43', '#FDAE61', '#FEE090', '#FFFFFF',
'#E0F3F8', '#ABD9E9', '#74ADD1', '#4575B4', '#313695')
)
return(colorRampPalette(colors)(n))
}
#' Get sequential colors
#'
#' Get sequential colors from palette theme name and n. The color palettes
#' are from RColorBrewer. Sequential colors are suitable for visualize a non-negative
#' or non-positive matrix (e.g. matrix in [0, 20], or [-100, -10], or [100, 500]).
#'
#' @param sequential Sequential color Palettes
#' @param n the number of colors (>= 1) to be in the palette.
#'
#' @return A character vector containing color names
#'
#' @seealso Function \code{\link{colorRampPalette}}, package \code{RColorBrewer}
#'
#' @keywords color
#' @example vignettes/example-colorlegend.R
#' @export
COL1 = function(sequential = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens',
'Greys', 'OrRd', 'YlOrRd', 'YlOrBr', 'YlGn'),
n = 200) {
sequential = match.arg(sequential)
colors = switch(
sequential,
Oranges = c('#FFF5EB', '#FEE6CE', '#FDD0A2', '#FDAE6B', '#FD8D3C', '#F16913',
'#D94801', '#A63603', '#7F2704'),
Purples = c('#FCFBFD', '#EFEDF5', '#DADAEB', '#BCBDDC', '#9E9AC8', '#807DBA',
'#6A51A3', '#54278F', '#3F007D'),
Reds = c('#FFF5F0', '#FEE0D2', '#FCBBA1', '#FC9272', '#FB6A4A', '#EF3B2C',
'#CB181D', '#A50F15', '#67000D'),
Blues = c('#F7FBFF', '#DEEBF7', '#C6DBEF', '#9ECAE1', '#6BAED6', '#4292C6',
'#2171B5', '#08519C', '#08306B'),
Greens = c('#F7FCF5', '#E5F5E0', '#C7E9C0', '#A1D99B', '#74C476', '#41AB5D',
'#238B45', '#006D2C', '#00441B'),
Greys = c('#FFFFFF', '#F0F0F0', '#D9D9D9', '#BDBDBD', '#969696', '#737373',
'#525252', '#252525', '#000000'),
OrRd = c('#FFF7EC', '#FEE8C8', '#FDD49E', '#FDBB84', '#FC8D59', '#EF6548',
'#D7301F', '#B30000', '#7F0000'),
YlOrRd = c('#FFFFCC', '#FFEDA0', '#FED976', '#FEB24C', '#FD8D3C', '#FC4E2A',
'#E31A1C', '#BD0026', '#800026'),
YlOrBr = c('#FFFFE5', '#FFF7BC', '#FEE391', '#FEC44F', '#FE9929', '#EC7014',
'#CC4C02', '#993404', '#662506'),
YlGn = c('#FFFFE5', '#F7FCB9', '#D9F0A3', '#ADDD8E', '#78C679', '#41AB5D',
'#238443', '#006837', '#004529')
)
return(colorRampPalette(colors)(n))
}
corrplot/R/corrplot.mixed.R 0000644 0001762 0000144 00000006164 14703115404 015421 0 ustar ligges users #' Using mixed methods to visualize a correlation matrix.
#'
#' @param corr Matrix, the correlation matrix to visualize.
#' @param lower Character, the visualization method for the lower triangular
#' correlation matrix.
#' @param upper Character, the visualization method for the upper triangular
#' correlation matrix.
#' @param tl.pos Character, \code{'lt'}, \code{'d'} or \code{'n'}, giving
#' position of text labels, \code{'lt'} means left and top, \code{'d'} means
#' diagonal. If \code{'n'}, add no textlabel.
#' @param diag Character, for specifying the glyph on the principal diagonal. It
#' is one of \code{'n'} (default, draw nothing), \code{'l'} (draw the glyphs
#' of lower triangular) or \code{'u'} (draw the glyphs of upper triangular).
#' @param bg The background color.
#' @param addgrid.col See the \code{addgrid.col} 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 plotCI See the \code{plotCI} parameter in the function
#' \code{\link{corrplot}}
#' @param mar See \code{\link{par}}.
#' @param \dots Additional arguments for corrplot's wrappers
#'
#' @author Taiyun Wei
#' @example vignettes/example-corrplot.mixed.R
#' @export
corrplot.mixed = function(
corr,
lower = 'number',
upper = 'circle',
tl.pos = c('d', 'lt', 'n'),
diag = c('n', 'l', 'u'),
bg = 'white',
addgrid.col = 'grey',
lower.col = NULL,
upper.col = NULL,
plotCI = c('n', 'square', 'circle', 'rect'),
mar = c(0, 0, 0, 0),
...)
{
tl.pos = match.arg(tl.pos)
diag = match.arg(diag)
plotCI = match.arg(plotCI)
n = nrow(corr)
# fixes issue #21
# some methods are not compatible with plotCI='rect'
adjust_plotCI = function(plotCI, method) {
if (plotCI != 'rect' || method %in% c('circle', 'square')) {
return(plotCI)
}
return('n')
}
plotCI_lower = adjust_plotCI(plotCI, lower)
plotCI_upper = adjust_plotCI(plotCI, upper)
# fixed issue #102
# restore this parameter when exiting the corrplot.mixed function in any way
oldpar = par(mar = mar, bg = par()$bg)
on.exit(par(oldpar), add = TRUE)
corrplot(corr, type = 'upper', method = upper, diag = TRUE,
tl.pos = tl.pos, plotCI = plotCI_upper,
bg = bg, addgrid.col = addgrid.col,
col = upper.col, mar = mar, ...) -> res1
corrplot(corr, add = TRUE, type = 'lower', method = lower,
diag = (diag == 'l'),
bg = bg, addgrid.col = addgrid.col,
tl.pos = 'n', cl.pos = 'n', plotCI = plotCI_lower,
col = lower.col, mar = mar, ...) -> res2
if (diag == 'n' && tl.pos != 'd') {
# draw empty rectangles over the diagonal to 'clean' it graphically
symbols(1:n, n:1, add = TRUE, bg = bg, fg = addgrid.col,
inches = FALSE, squares = rep(1, n))
}
corr = res1$corr
corrPos = rbind(res1$corrPos, res2$corrPos)
corrPos = corrPos[order(corrPos[, 1], corrPos[, 2]), ]
res = list(corr=corr, corrPos=corrPos)
invisible(res)
}
corrplot/R/corrplot.R 0000644 0001762 0000144 00000111356 14703126567 014330 0 ustar ligges users #' A visualization of a correlation matrix.
#'
#' A graphical display of a correlation matrix, confidence interval. The details
#' are paid great attention to. It can also visualize a general matrix by
#' setting \code{is.corr = FALSE}.
#'
#' @param corr The correlation matrix to visualize, must be square if
#' \code{order} is not \code{'original'}. For general matrix, please using
#' \code{is.corr = FALSE} to convert.
#'
#' @param method Character, the visualization method of correlation matrix to be
#' used. Currently, it supports seven methods, named \code{'circle'}
#' (default), \code{'square'}, \code{'ellipse'}, \code{'number'},
#' \code{'pie'}, \code{'shade'} and \code{'color'}. See examples for details.
#'
#' The areas of circles or squares show the absolute value of corresponding
#' correlation coefficients. Method \code{'pie'} and \code{'shade'} came from
#' Michael Friendly's job (with some adjustment about the shade added on), and
#' \code{'ellipse'} came from D.J. Murdoch and E.D. Chow's job, see in section
#' References.
#'
#' @param type Character, \code{'full'} (default), \code{'upper'} or
#' \code{'lower'}, display full matrix, lower triangular or upper triangular
#' matrix.
#'
#'
#' @param col Vector, the colors of glyphs. They are distributed uniformly in
#' \code{col.lim} interval.
#' If \code{is.corr} is \code{TRUE}, the default value will be \code{COL2('RdBu', 200)}.
#' If \code{is.corr} is \code{FALSE} and \code{corr} is a non-negative or non-positive matrix,
#' the default value will be \code{COL1('YlOrBr', 200)};
#' otherwise (elements are partly positive and partly negative),
#' the default value will be \code{COL2('RdBu', 200)}.
#'
#' @param col.lim The limits \code{(x1, x2)} interval for assigning color by
#' \code{col}. If \code{NULL},
#' \code{col.lim} will be \code{c(-1, 1)} when \code{is.corr} is \code{TRUE},
#' \code{col.lim} will be \code{c(min(corr), max(corr))} when \code{is.corr}
#' is \code{FALSE}
#'
#' NOTICE: if you set \code{col.lim} when \code{is.corr} is \code{TRUE}, the assigning colors
#' are still distributed uniformly in [-1, 1], it only affect the display
#' on color-legend.
#'
#' @param is.corr Logical, whether the input matrix is a correlation matrix or
#' not. We can visualize the non-correlation matrix by setting
#' \code{is.corr = FALSE}.
#'
#'
#' @param bg The background color.
#'
#' @param title Character, title of the graph.
#'
#'
#' @param add Logical, if \code{TRUE}, the graph is added to an existing plot,
#' otherwise a new plot will be created.
#'
#' @param diag Logical, whether display the correlation coefficients on the
#' principal diagonal.
#'
#' @param outline Logical or character, whether plot outline of circles, square
#' and ellipse, or the color of these glyphs. For pie, this represents the
#' color of the circle outlining the pie. If \code{outline} is \code{TRUE},
#' the default value is \code{'black'}.
#'
#' @param mar See \code{\link{par}}.
#'
#' @param addgrid.col The color of the grid. If \code{NA}, don't add grid. If
#' \code{NULL} the default value is chosen. The default value depends on
#' \code{method}, if \code{method} is \code{color} or \code{shade}, the color
#' of the grid is \code{NA}, that is, not draw grid; otherwise \code{'grey'}.
#'
#' @param addCoef.col Color of coefficients added on the graph. If \code{NULL}
#' (default), add no coefficients.
#'
#' @param addCoefasPercent Logic, whether translate coefficients into percentage
#' style for spacesaving.
#'
#' @param order Character, the ordering method of the correlation matrix.
#' \itemize{
#' \item{\code{'original'} for original order (default).}
#' \item{\code{'AOE'} for the angular order of the eigenvectors.}
#' \item{\code{'FPC'} for the first principal component order.}
#' \item{\code{'hclust'} for the hierarchical clustering order.}
#' \item{\code{'alphabet'} for alphabetical order.}
#' }
#'
#' See function \code{\link{corrMatOrder}} for details.
#'
#' @param hclust.method Character, the agglomeration method to be used when
#' \code{order} is \code{\link{hclust}}. This should be one of \code{'ward'},
#' \code{'ward.D'}, \code{'ward.D2'}, \code{'single'}, \code{'complete'},
#' \code{'average'}, \code{'mcquitty'}, \code{'median'} or \code{'centroid'}.
#'
#' @param addrect Integer, the number of rectangles draws on the graph according
#' to the hierarchical cluster, only valid when \code{order} is \code{hclust}.
#' If \code{NULL} (default), then add no rectangles.
#'
#' @param rect.col Color for rectangle border(s), only valid when \code{addrect}
#' is equal or greater than 1.
#'
#' @param rect.lwd Numeric, line width for borders for rectangle border(s), only
#' valid when \code{addrect} is equal or greater than 1.
#'
#'
#' @param tl.pos Character or logical, position of text labels. If character, it
#' must be one of \code{'lt'}, \code{'ld'}, \code{'td'}, \code{'d'} or
#' \code{'n'}. \code{'lt'}(default if \code{type=='full'}) means left and top,
#' \code{'ld'}(default if \code{type=='lower'}) means left and diagonal,
#' \code{'td'}(default if \code{type=='upper'}) means top and diagonal(near),
#' \code{'l'} means left,
#' \code{'d'} means diagonal, \code{'n'} means don't add text-label.
#'
#' @param tl.cex Numeric, for the size of text label (variable names).
#'
#' @param tl.col The color of text label.
#'
#' @param tl.offset Numeric, for text label, see \code{\link{text}}.
#'
#' @param tl.srt Numeric, for text label string rotation in degrees, see
#' \code{\link{text}}.
#'
#' @param cl.pos Character or logical, position of color-legend; If character,
#' it must be one of \code{'r'} (default if \code{type=='upper'} or
#' \code{'full'}), \code{'b'} (default if \code{type=='lower'}) or \code{'n'},
#' \code{'n'} means don't draw color-legend.
#'
#' @param cl.length Integer, the number of number-text in color-legend, passed to
#' \code{\link{colorlegend}}. If \code{NULL}, \code{cl.length} is
#' \code{length(col) + 1} when \code{length(col) <=20}; \code{cl.length} is 11
#' when \code{length(col) > 20}
#'
#' @param cl.cex Numeric, text size of number-label in color-legend, passed to
#' \code{\link{colorlegend}}.
#'
#' @param cl.ratio Numeric, to justify the width of color-legend, 0.1~0.2 is
#' suggested.
#'
#' @param cl.align.text Character, \code{'l'}, \code{'c'} (default) or
#' \code{'r'}, for number-label in color-legend, \code{'l'} means left,
#' \code{'c'} means center, and \code{'r'} means right.
#'
#' @param cl.offset Numeric, for number-label in color-legend, see
#' \code{\link{text}}.
#'
#' @param number.cex The \code{cex} parameter to send to the call to \code{text}
#' when writing the correlation coefficients into the plot.
#'
#' @param number.font the \code{font} parameter to send to the call to
#' \code{text} when writing the correlation coefficients into the plot.
#'
#' @param number.digits indicating the number of decimal digits to be
#' added into the plot. Non-negative integer or NULL, default NULL.
#'
#' @param addshade Character for shade style, \code{'negative'},
#' \code{'positive'} or \code{'all'}, only valid when \code{method} is
#' \code{'shade'}. If \code{'all'}, all correlation coefficients' glyph will
#' be shaded; if \code{'positive'}, only the positive will be shaded; if
#' \code{'negative'}, only the negative will be shaded. Note: the angle of
#' shade line is different, 45 degrees for positive and 135 degrees for
#' negative.
#'
#' @param shade.lwd Numeric, the line width of shade.
#'
#' @param shade.col The color of shade line.
#'
#' @param transKeepSign Logical, whether or not to keep matrix values' sign when
#' transforming non-corr matrix for plotting.
#' Only valid when \code{is.corr = FALSE}. The default value is \code{TRUE}.
#'
#' NOTE: If \code{FALSE},the non-corr matrix will be
#'
#' @param p.mat Matrix of p-value, if \code{NULL}, parameter \code{sig.level},
#' \code{insig}, \code{pch}, \code{pch.col}, \code{pch.cex} are invalid.
#'
#' @param sig.level Significant level, if the p-value in \code{p-mat} is bigger
#' than \code{sig.level}, then the corresponding correlation coefficient is
#' regarded as insignificant. If \code{insig} is \code{'label_sig'}, this may
#' be an increasing vector of significance levels, in which case \code{pch}
#' will be used once for the highest p-value interval and multiple times
#' (e.g. '*', '**', '***') for each lower p-value interval.
#'
#' @param insig Character, specialized insignificant correlation coefficients,
#' \code{'pch'} (default), \code{'p-value'}, \code{'blank'}, \code{'n'}, or
#' \code{'label_sig'}. If \code{'blank'}, wipe away the corresponding glyphs;
#' if \code{'p-value'}, add p-values the corresponding glyphs;
#' if \code{'pch'}, add characters (see \code{pch} for details) on
#' corresponding glyphs; if \code{'n'}, don't take any measures; if
#' \code{'label_sig'}, mark significant correlations with pch
#' (see \code{sig.level}).
#'
#' @param pch Add character on the glyphs of insignificant correlation
#' coefficients(only valid when \code{insig} is \code{'pch'}). See
#' \code{\link{par}}.
#'
#' @param pch.col The color of pch (only valid when \code{insig} is
#' \code{'pch'}).
#'
#' @param pch.cex The cex of pch (only valid when \code{insig} is \code{'pch'}).
#'
#' @param plotCI Character, method of ploting confidence interval. If
#' \code{'n'}, don't plot confidence interval. If 'rect', plot rectangles
#' whose upper side means upper bound and lower side means lower bound,
#' respectively. If 'circle', first plot a circle with the bigger absolute
#' bound, and then plot the smaller. Warning: if the two bounds are the same
#' sign, the smaller circle will be wiped away, thus forming a ring. Method
#' 'square' is similar to 'circle'.
#'
#' @param lowCI.mat Matrix of the lower bound of confidence interval.
#'
#' @param uppCI.mat Matrix of the upper bound of confidence interval.
#'
#' @param na.label Label to be used for rendering \code{NA} cells. Default is
#' \code{'?'}. If 'square', then the cell is rendered as a square with the
#' \code{na.label.col} color.
#'
#' @param na.label.col Color used for rendering \code{NA} cells. Default is
#' \code{'black'}.
#'
#' @param win.asp Aspect ration for the whole plot. Value other than 1 is
#' currently compatible only with methods 'circle' and 'square'.
#'
#' @param \dots Additional arguments passing to function \code{text} for drawing
#' text label.
#'
#' @return (Invisibly) returns a \code{list(corr, corrTrans, arg)}.
#' \code{corr} is a reordered correlation matrix for plotting.
#' \code{corrPos} is a data frame with \code{xName, yName, x, y, corr} and
#' \code{p.value}(if p.mat is not NULL)
#' column, which x and y are the position on the correlation matrix plot.
#' \code{arg} is a list of some corrplot() input parameters' value.
#' Now \code{type} is in.
#'
#' @details \code{corrplot} function offers flexible ways to visualize
#' correlation matrix, lower and upper bound of confidence interval matrix.
#'
#' @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.
#'
#' @author Taiyun Wei (weitaiyun@@gmail.com)
#' @author Viliam Simko (viliam.simko@@gmail.com)
#' @author Michael Levy (michael.levy@@healthcatalyst.com)
#'
#' @note \code{Cairo} and \code{cairoDevice} packages is strongly recommended to
#' produce high-quality PNG, JPEG, TIFF bitmap files, especially for that
#' \code{method} \code{circle}, \code{ellipse}.
#'
#' @note Row- and column names of the input matrix are used as labels rendered
#' in the corrplot. Plothmath expressions will be used if the name is prefixed
#' by one of the following characters: \code{:}, \code{=} or \code{$}.
#' For example \code{':alpha + beta'}.
#'
#' @seealso Function \code{plotcorr} in the \code{ellipse} package and
#' \code{corrgram} in the \code{corrgram} package have some similarities.
#'
#' Package \code{seriation} offered more methods to reorder matrices, such as
#' ARSA, BBURCG, BBWRCG, MDS, TSP, Chen and so forth.
#'
#' @example vignettes/example-corrplot.R
#' @import graphics grDevices stats
#' @export
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),
addgrid.col = NULL, addCoef.col = NULL, addCoefasPercent = FALSE,
order = c('original', 'AOE', 'FPC', 'hclust', 'alphabet'),
hclust.method = c('complete', 'ward', 'ward.D', 'ward.D2', 'single',
'average', 'mcquitty', 'median', 'centroid'),
addrect = NULL, rect.col = 'black', rect.lwd = 2,
tl.pos = NULL, tl.cex = 1,
tl.col = 'red', tl.offset = 0.4, tl.srt = 90,
cl.pos = NULL, cl.length = NULL, cl.cex = 0.8,
cl.ratio = 0.15, cl.align.text = 'c', cl.offset = 0.5,
number.cex = 1, number.font = 2, number.digits = NULL,
addshade = c('negative', 'positive', 'all'),
shade.lwd = 1, shade.col = 'white',
transKeepSign = TRUE,
p.mat = NULL, sig.level = 0.05,
insig = c('pch', 'p-value', 'blank', 'n', 'label_sig'),
pch = 4, pch.col = 'black', pch.cex = 3,
plotCI = c('n', 'square', 'circle', 'rect'),
lowCI.mat = NULL, uppCI.mat = NULL,
na.label = '?', na.label.col = 'black',
win.asp = 1,
...)
{
# checking multi-option input parameters
method = match.arg(method)
type = match.arg(type)
order = match.arg(order)
hclust.method = match.arg(hclust.method)
addshade = match.arg(addshade)
insig = match.arg(insig)
plotCI = match.arg(plotCI)
# rescale symbols within the corrplot based on win.asp parameter
if (win.asp != 1 && !(method %in% c('circle', 'square'))) {
stop('Parameter \'win.asp\' is supported only for circle and square methods.')
}
asp_rescale_factor = min(1, win.asp) / max(1, win.asp)
stopifnot(asp_rescale_factor >= 0 && asp_rescale_factor <= 1)
if (!is.matrix(corr) && !is.data.frame(corr)) {
stop('Need a matrix or data frame!')
}
# select grid color automatically if not specified
if (is.null(addgrid.col)) {
addgrid.col = switch(method, color = NA, shade = NA, 'grey')
}
if(!is.corr & !transKeepSign & method %in% c('circle', 'square', 'ellipse', 'shade', 'pie')) {
stop("method should not be in c('circle', 'square', 'ellipse', 'shade', 'pie') when transKeepSign = FALSE")
}
# Issue #142
# checks for all values that are not missing
if (any(corr[!is.na(corr)] < col.lim[1]) || any(corr[!is.na(corr)] > col.lim[2])) {
stop('color limits should cover matrix')
}
if (is.null(col.lim)) {
if (is.corr) {
# if the matrix is expected to be a correlation matrix
# it MUST be within the interval [-1,1]
col.lim = c(-1, 1)
} else {
# Issue #91
# if not a correlation matrix and the diagonal is hidden,
# we need to compute limits from all cells except the diagonal
if(!diag) {
diag(corr) = NA
}
col.lim = c(min(corr, na.rm = TRUE), max(corr, na.rm = TRUE))
}
}
# if the mat have both negative and positive values, it is a SpecialCorr
SpecialCorr = 0
if(is.corr) {
# check the interval if expecting a correlation matrix
# otherwise, the values can be any number
if (min(corr, na.rm = TRUE) < -1 - .Machine$double.eps ^ 0.75 ||
max(corr, na.rm = TRUE) > 1 + .Machine$double.eps ^ 0.75) {
stop('The matrix is not in [-1, 1]!')
}
SpecialCorr = 1
if(col.lim[1] < -1 | col.lim[2] > 1) {
stop('col.lim should be within the interval [-1, 1]')
}
}
intercept = 0
zoom = 1
if (!is.corr) {
c_max = max(corr, na.rm = TRUE)
c_min = min(corr, na.rm = TRUE)
if((col.lim[1] > c_min) | (col.lim[2] < c_max))
{
stop('Wrong color: matrix should be in col.lim interval!')
}
if(diff(col.lim)/(c_max - c_min)> 2) {
warning('col.lim interval too wide, please set a suitable value')
}
# all negative or positive or NOT transkeepSign, trans to [0, 1]
if (c_max <= 0 | c_min>=0 | !transKeepSign) {
intercept = - col.lim[1]
zoom = 1 / (diff(col.lim))
#if(col.lim[1] * col.lim[2] < 0) {
# warning('col.lim interval not suitable to the matrix')
#}
}
# mixed negative and positive, remain its sign, e.g. [-0.8, 1] or [-1, 0.7]
else {
# expression from the original code as a sanity check
stopifnot(c_max * c_min < 0)
# newly derived expression which covers the single remaining case
stopifnot(c_min < 0 && c_max > 0)
intercept = 0
zoom = 1 / max(abs(col.lim))
SpecialCorr = 1
}
corr = (intercept + corr) * zoom
}
col.lim2 = (intercept + col.lim) * zoom
int = intercept * zoom
if (is.null(col) & is.corr) {
col = COL2('RdBu', 200)
}
if (is.null(col) & !is.corr) {
if(col.lim[1] * col.lim[2] < 0) {
col = COL2('RdBu', 200)
} else {
col = COL1('YlOrBr', 200)
}
}
n = nrow(corr)
m = ncol(corr)
min.nm = min(n, m)
ord = 1:min.nm
if (order != 'original') {
ord = corrMatOrder(corr, order = order, hclust.method = hclust.method)
corr = corr[ord, ord]
if (!is.null(p.mat)) {
p.mat = p.mat[ord, ord]
}
}
## set up variable names
if (is.null(rownames(corr))) {
rownames(corr) = 1:n
}
if (is.null(colnames(corr))) {
colnames(corr) = 1:m
}
# assigns Inf to cells in the matrix depending on the type paramter
apply_mat_filter = function(mat) {
x = matrix(1:n * m, nrow = n, ncol = m)
switch(type,
upper = mat[row(x) > col(x)] <- Inf,
lower = mat[row(x) < col(x)] <- Inf
)
if (!diag) {
diag(mat) = Inf
}
return(mat)
}
# retrieves coordinates of cells to be rendered
getPos.Dat = function(mat) {
tmp = apply_mat_filter(mat)
Dat = tmp[is.finite(tmp)]
ind = which(is.finite(tmp), arr.ind = TRUE)
Pos = ind
Pos[, 1] = ind[, 2]
Pos[, 2] = -ind[, 1] + 1 + n
PosName = ind
PosName[, 1] = colnames(mat)[ind[, 2]]
PosName[, 2] = rownames(mat)[ind[, 1]]
return(list(Pos, Dat, PosName))
}
# retrieves coordinates of NA cells
# 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)
Pos = ind
Pos[, 1] = ind[, 2]
Pos[, 2] = -ind[, 1] + 1 + n
return(Pos)
}
testTemp = getPos.Dat(corr)
Pos = getPos.Dat(corr)[[1]]
PosName = getPos.Dat(corr)[[3]]
# decide whether NA labels are going to be rendered or whether we ignore them
if (any(is.na(corr)) && is.character(na.label)) {
PosNA = getPos.NAs(corr)
} else {
# explicitly set to NULL to indicate that NA labels are not going to be
# rendered
PosNA = NULL
}
AllCoords = rbind(Pos, PosNA)
# rows
n2 = max(AllCoords[, 2])
n1 = min(AllCoords[, 2])
nn = n2 - n1
# columns
m2 = max(AllCoords[, 1])
m1 = min(AllCoords[, 1])
# Issue #19: legend color bar width 0 when using just one column matrix
# also discussed here: http://stackoverflow.com/questions/34638555/
mm = max(1, m2 - m1)
# Issue #20: support plotmath expressions in rownames and colnames
expand_expression = function(s) {
ifelse(grepl('^[:=$]', s), parse(text = substring(s, 2)), s)
}
newrownames = sapply(
rownames(corr)[(n + 1 - n2):(n + 1 - n1)], expand_expression)
newcolnames = sapply(
colnames(corr)[m1:m2], expand_expression)
DAT = getPos.Dat(corr)[[2]]
len.DAT = length(DAT)
rm(expand_expression) # making sure the function is only used here
## assign colors
assign.color = function(dat = DAT, color = col, isSpecialCorr = SpecialCorr) {
if(isSpecialCorr) {
newcorr = (dat + 1) / 2
} else {
newcorr = dat
}
newcorr[newcorr <= 0] = 0
newcorr[newcorr >= 1] = 1 - 1e-16
color[floor(newcorr * length(color)) + 1] # new color returned
}
col.fill = assign.color()
isFALSE = function(x) identical(x, FALSE)
isTRUE = function(x) identical(x, TRUE)
if (isFALSE(tl.pos)) {
tl.pos = 'n'
}
if (is.null(tl.pos) || isTRUE(tl.pos)) {
tl.pos = switch(type, full = 'lt', lower = 'ld', upper = 'td')
}
if (isFALSE(cl.pos)) {
cl.pos = 'n'
}
if (is.null(cl.pos) || isTRUE(cl.pos)) {
cl.pos = switch(type, full = 'r', lower = 'b', upper = 'r')
}
if (isFALSE(outline)) {
col.border = col.fill
} else if (isTRUE(outline)) {
col.border = 'black'
} else if (is.character(outline)) {
col.border = outline
} else {
stop('Unsupported value type for parameter outline')
}
# restore this parameter when exiting the corrplot function in any way
oldpar = par(mar = mar, bg = par()$bg)
on.exit(par(oldpar), add = TRUE)
## calculate label-text width approximately
if (!add) {
plot.new()
# Issue #10: code from Sebastien 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 - 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)
ylim = c(
n1 - 0.5 - nn * cl.ratio * (cl.pos == 'b') - laboffset,
n2 + 0.5 + laboffset +
ylabwidth * abs(sin(tl.srt * pi / 180)) * grepl('t', tl.pos) +
ylabwidth * abs(sin(tl.srt * pi / 180)) * (type=='lower') * grepl('d', tl.pos)
) #+ c(-0.15, 0)
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))
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
laboffset = laboffset.tmp
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,
height = 7 * diff(ylim) / diff(xlim))
}
xlim = xlim + diff(xlim) * 0.01 * c(-1, 1)
ylim = ylim + diff(ylim) * 0.01 * c(-1, 1)
plot.window(xlim = xlim, ylim = ylim,
asp = win.asp, xlab = '', ylab = '', xaxs = 'i', yaxs = 'i')
}
## for: add = TRUE
laboffset = strwidth('W', cex = tl.cex) * tl.offset
## background for the cells
symbols(Pos, add = TRUE, inches = FALSE,
rectangles = matrix(1, len.DAT, 2), bg = bg, fg = bg)
## circle
if (method == 'circle' && plotCI == 'n') {
symbols(Pos, add = TRUE, inches = FALSE,
circles = asp_rescale_factor * 0.9 * abs(DAT) ^ 0.5 / 2,
fg = col.border, bg = col.fill)
}
## ellipse
if (method == 'ellipse' && plotCI == 'n') {
ell.dat = function(rho, length = 99) {
k = seq(0, 2 * pi, length = length)
x = cos(k + acos(rho) / 2) / 2
y = cos(k - acos(rho) / 2) / 2
cbind(rbind(x, y), c(NA, NA))
}
ELL.dat = lapply(DAT, ell.dat)
ELL.dat2 = 0.85 * matrix(unlist(ELL.dat), ncol = 2, byrow = TRUE)
ELL.dat2 = ELL.dat2 + Pos[rep(1: length(DAT), each = 100), ]
polygon(ELL.dat2, border = col.border, col = col.fill)
}
## number
if (is.null(number.digits)) {
number.digits = switch(addCoefasPercent + 1, 2, 0)
}
stopifnot(number.digits %% 1 == 0) # is whole number
stopifnot(number.digits >= 0) # is non-negative number
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),
cex = number.cex)
}
# Issue #55: Support for multiple characters when rendering NAs
NA_LABEL_MAX_CHARS = 2
# renders NA cells
if (is.matrix(PosNA) && nrow(PosNA) > 0) {
stopifnot(is.matrix(PosNA)) # sanity check
if (na.label == 'square') {
symbols(PosNA, add = TRUE, inches = FALSE,
squares = rep(1, nrow(PosNA)),
bg = na.label.col, fg = na.label.col)
} else if (nchar(na.label) %in% 1:NA_LABEL_MAX_CHARS) {
symbols(PosNA, add = TRUE, inches = FALSE,
squares = rep(1, nrow(PosNA)), fg = bg, bg = bg)
text(PosNA[, 1], PosNA[, 2], font = number.font,
col = na.label.col,
labels = na.label, cex = number.cex, ...)
} else {
stop(paste('Maximum number of characters for NA label is:',
NA_LABEL_MAX_CHARS))
}
}
## pie
if (method == 'pie' && plotCI == 'n') {
# Issue #18: Corrplot background circle
symbols(Pos, add = TRUE, inches = FALSE,
circles = rep(0.5, len.DAT) * 0.85, fg = col.border)
pie.dat = function(theta, length = 100) {
k = seq(pi / 2, pi / 2 - theta, length = 0.5 * length * abs(theta) / pi)
x = c(0, cos(k) / 2, 0)
y = c(0, sin(k) / 2, 0)
cbind(rbind(x, y), c(NA, NA)) # pie.dat returned
}
PIE.dat = lapply(DAT * 2 * pi, pie.dat)
len.pie = unlist(lapply(PIE.dat, length)) / 2
PIE.dat2 = 0.85 * matrix(unlist(PIE.dat), ncol = 2, byrow = TRUE)
PIE.dat2 = PIE.dat2 + Pos[rep(1:length(DAT), len.pie), ]
polygon(PIE.dat2, border = 'black', col = col.fill)
}
## shade
if (method == 'shade' && plotCI == 'n') {
symbols(Pos, add = TRUE, inches = FALSE, squares = rep(1, len.DAT),
bg = col.fill, fg = addgrid.col)
shade.dat = function(w) {
x = w[1]
y = w[2]
rho = w[3]
x1 = x - 0.5
x2 = x + 0.5
y1 = y - 0.5
y2 = y + 0.5
dat = NA
if ((addshade == 'positive' || addshade == 'all') && rho > 0) {
dat = cbind(c(x1, x1, x), c(y, y1, y1),
c(x, x2, x2), c(y2, y2, y))
}
if ((addshade == 'negative' || addshade == 'all') && rho < 0) {
dat = cbind(c(x1, x1, x), c(y, y2, y2),
c(x, x2, x2), c(y1, y1, y))
}
return(t(dat))
}
pos_corr = rbind(cbind(Pos, DAT))
pos_corr2 = split(pos_corr, 1: nrow(pos_corr))
SHADE.dat = matrix(na.omit(unlist(lapply(pos_corr2, shade.dat))),
byrow = TRUE, ncol = 4)
segments(SHADE.dat[, 1], SHADE.dat[, 2], SHADE.dat[, 3],
SHADE.dat[, 4], col = shade.col, lwd = shade.lwd)
}
## square
if (method == 'square' && plotCI == 'n') {
draw_method_square(Pos, DAT, asp_rescale_factor, col.border, col.fill)
}
## color
if (method == 'color' && plotCI == 'n') {
draw_method_color(Pos, col.border, col.fill)
}
## add grid
draw_grid(AllCoords, addgrid.col)
if (plotCI != 'n') {
if (is.null(lowCI.mat) || is.null(uppCI.mat)) {
stop('Need lowCI.mat and uppCI.mat!')
}
if (order != 'original') {
lowCI.mat = lowCI.mat[ord, ord]
uppCI.mat = uppCI.mat[ord, ord]
}
pos.lowNew = getPos.Dat(lowCI.mat)[[1]]
lowNew = getPos.Dat(lowCI.mat)[[2]]
pos.uppNew = getPos.Dat(uppCI.mat)[[1]]
uppNew = getPos.Dat(uppCI.mat)[[2]]
k1 = (abs(uppNew) > abs(lowNew))
bigabs = uppNew
bigabs[which(!k1)] = lowNew[!k1]
smallabs = lowNew
smallabs[which(!k1)] = uppNew[!k1]
sig = sign(uppNew * lowNew)
color_bigabs = col[ceiling((bigabs + 1) * length(col) / 2)]
color_smallabs = col[ceiling((smallabs + 1) * length(col) / 2)]
if (plotCI == 'circle') {
symbols(pos.uppNew[, 1], pos.uppNew[, 2],
add = TRUE, inches = FALSE,
circles = 0.95 * abs(bigabs) ^ 0.5 / 2,
bg = ifelse(sig > 0, col.fill, color_bigabs),
fg = ifelse(sig > 0, col.fill, color_bigabs)
)
symbols(pos.lowNew[, 1], pos.lowNew[, 2],
add = TRUE, inches = FALSE,
circles = 0.95 * abs(smallabs) ^ 0.5 / 2,
bg = ifelse(sig > 0, bg, color_smallabs),
fg = ifelse(sig > 0, col.fill, color_smallabs))
}
if (plotCI == 'square') {
symbols(pos.uppNew[, 1], pos.uppNew[, 2],
add = TRUE, inches = FALSE,
squares = abs(bigabs) ^ 0.5,
bg = ifelse(sig > 0, col.fill, color_bigabs),
fg = ifelse(sig > 0, col.fill, color_bigabs))
symbols(pos.lowNew[, 1], pos.lowNew[, 2],
add = TRUE, inches = FALSE,
squares = abs(smallabs) ^ 0.5,
bg = ifelse(sig > 0, bg, color_smallabs),
fg = ifelse(sig > 0, col.fill, color_smallabs))
}
if (plotCI == 'rect') {
rect.width = 0.25
rect(pos.uppNew[, 1] - rect.width, pos.uppNew[, 2] + smallabs / 2,
pos.uppNew[, 1] + rect.width, pos.uppNew[, 2] + bigabs / 2,
col = col.fill, border = col.fill)
segments(pos.lowNew[, 1] - rect.width, pos.lowNew[, 2] + DAT / 2,
pos.lowNew[, 1] + rect.width, pos.lowNew[, 2] + DAT / 2,
col = 'black', lwd = 1)
segments(pos.uppNew[, 1] - rect.width, pos.uppNew[, 2] + uppNew / 2,
pos.uppNew[, 1] + rect.width, pos.uppNew[, 2] + uppNew / 2,
col = 'black', lwd = 1)
segments(pos.lowNew[, 1] - rect.width, pos.lowNew[, 2] + lowNew / 2,
pos.lowNew[, 1] + rect.width, pos.lowNew[, 2] + lowNew / 2,
col = 'black', lwd = 1)
segments(pos.lowNew[, 1] - 0.5, pos.lowNew[, 2],
pos.lowNew[, 1] + 0.5, pos.lowNew[, 2], col = 'grey70', lty = 3)
}
}
## 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
),
cex = number.cex, font = number.font)
}
if (!is.null(p.mat)) {
pos.pNew = getPos.Dat(p.mat)[[1]]
pNew = getPos.Dat(p.mat)[[2]]
}
if (!is.null(p.mat) && insig != 'n') {
if(!is.null(rownames(p.mat)) | !is.null(rownames(p.mat))) {
if(!all(colnames(p.mat)==colnames(corr)) |
!all(rownames(p.mat)==rownames(corr))) {
warning('p.mat and corr may be not paired, their rownames and colnames are not totally same!')
}
}
if (insig == 'label_sig') {
# Unless another character is specified, mark sig with *
if (!is.character(pch))
pch = '*'
place_points = function(sig.locs, point) {
text(pos.pNew[, 1][sig.locs], pos.pNew[, 2][sig.locs],
labels = point, col = pch.col, cex = pch.cex, lwd = 2)
}
if (length(sig.level) == 1) {
place_points(sig.locs = which(pNew < sig.level), point = pch)
} else {
l = length(sig.level)
for (i in seq_along(sig.level)) {
iter = l + 1 - i
pchTmp = paste(rep(pch, i), collapse = '')
if (i == length(sig.level)) {
locs = which(pNew < sig.level[iter])
if (length(locs)) {
place_points(sig.locs = locs, point = pchTmp)
}
} else {
locs = which(pNew < sig.level[iter] & pNew > sig.level[iter - 1])
if (length(locs)) {
place_points(sig.locs = locs, point = pchTmp)
}
}
}
}
} else {
ind.p = which(pNew > sig.level)
p_inSig = length(ind.p) > 0
if (insig == 'pch' && p_inSig) {
points(pos.pNew[, 1][ind.p], pos.pNew[, 2][ind.p],
pch = pch, col = pch.col, cex = pch.cex, lwd = 2)
}
if (insig == 'p-value' && p_inSig) {
text(pos.pNew[, 1][ind.p], pos.pNew[, 2][ind.p],
round(pNew[ind.p], number.digits), col = pch.col)
}
if (insig == 'blank' && p_inSig) {
symbols(pos.pNew[, 1][ind.p], pos.pNew[, 2][ind.p], inches = FALSE,
squares = rep(1, length(pos.pNew[, 1][ind.p])),
fg = addgrid.col, bg = bg, add = TRUE)
}
}
}
### color legend
if (cl.pos != 'n') {
colRange = assign.color(dat = col.lim2)
ind1 = which(col == colRange[1])
ind2 = which(col == colRange[2])
colbar = col[ind1:ind2]
if (is.null(cl.length)) {
cl.length = ifelse(length(colbar) > 20, 11, length(colbar) + 1)
}
labels = seq(col.lim[1], col.lim[2], length = cl.length)
if (cl.pos == 'r') {
vertical = TRUE
xlim = c(m2 + 0.5 + mm * 0.02, m2 + 0.5 + mm * cl.ratio)
ylim = c(n1 - 0.5, n2 + 0.5)
}
if (cl.pos == 'b') {
vertical = FALSE
xlim = c(m1 - 0.5, m2 + 0.5)
ylim = c(n1 - 0.5 - nn * cl.ratio, n1 - 0.5 - nn * 0.02)
}
colorlegend(colbar = colbar, labels = round(labels, 2),
offset = cl.offset, ratio.colbar = 0.3, cex = cl.cex,
xlim = xlim, ylim = ylim, vertical = vertical,
align = cl.align.text)
}
## add variable names and title
if (tl.pos != 'n') {
pos.xlabel = cbind(m1:m2, n2 + 0.5 + laboffset)
pos.ylabel = cbind(m1 - 0.5, n2:n1)
if (tl.pos == 'td') {
if (type != 'upper') {
stop('type should be \'upper\' if tl.pos is \'dt\'.')
}
pos.ylabel = cbind(m1:(m1 + nn) - 0.5, n2:n1)
}
if (tl.pos == 'ld') {
if (type != 'lower') {
stop('type should be \'lower\' if tl.pos is \'ld\'.')
}
pos.xlabel = cbind(m1:m2, n2:(n2 - mm) + 0.5 + laboffset)
}
if (tl.pos == 'd') {
pos.ylabel = cbind(m1:(m1 + nn) - 0.5, n2:n1)
pos.ylabel = pos.ylabel[1:min(n, m), ]
symbols(pos.ylabel[, 1] + 0.5, pos.ylabel[, 2], add = TRUE,
bg = bg, fg = addgrid.col,
inches = FALSE, squares = rep(1, length(pos.ylabel[, 1])))
text(pos.ylabel[, 1] + 0.5, pos.ylabel[, 2], newcolnames[1:min(n, m)],
col = tl.col, cex = tl.cex, ...)
} else {
if(tl.pos != 'l') {
text(pos.xlabel[, 1], pos.xlabel[, 2], newcolnames, srt = tl.srt,
adj = ifelse(tl.srt == 0, c(0.5, 0), c(0, 0)),
col = tl.col, cex = tl.cex, offset = tl.offset, ...)
}
text(pos.ylabel[, 1], pos.ylabel[, 2], newrownames,
col = tl.col, cex = tl.cex, pos = 2, offset = tl.offset, ...)
}
}
title(title, ...)
## add grid, in case of the grid is ate when 'diag=FALSE'
if (type == 'full' && plotCI == 'n' && !is.null(addgrid.col)) {
rect(m1 - 0.5, n1 - 0.5, m2 + 0.5, n2 + 0.5, border = addgrid.col)
}
## draws rectangles, call function corrRect.hclust
if (!is.null(addrect) && order == 'hclust' && type == 'full') {
corrRect.hclust(corr, k = addrect, method = hclust.method,
col = rect.col, lwd = rect.lwd)
}
corrPos = data.frame(PosName, Pos, DAT)
colnames(corrPos) = c('xName', 'yName', 'x', 'y', 'corr')
if(!is.null(p.mat)) {
corrPos = cbind(corrPos, pNew)
colnames(corrPos)[6] = c('p.value')
}
corrPos = corrPos[order(corrPos[, 3], -corrPos[, 4]), ]
rownames(corrPos) = NULL
res = list(corr = corr, corrPos = corrPos, arg = list(type = type))
invisible(res) # reordered correlation matrix, and Position
}
#' @noRd
draw_method_square = function(coords, values, asp_rescale_factor, fg, bg) {
symbols(coords, add = TRUE, inches = FALSE,
squares = asp_rescale_factor * abs(values) ^ 0.5,
bg = bg, fg = fg)
}
#' @noRd
draw_method_color = function(coords, fg, bg) {
symbols(coords, squares = rep(1, nrow(coords)), fg = fg, bg = bg,
add = TRUE, inches = FALSE)
}
#' @noRd
draw_grid = function(coords, fg) {
symbols(coords, add = TRUE, inches = FALSE, fg = fg, bg = NA,
rectangles = matrix(1, nrow = nrow(coords), ncol = 2))
}
corrplot/vignettes/ 0000755 0001762 0000144 00000000000 14703127133 014127 5 ustar ligges users corrplot/vignettes/example-corrplot.mixed.R 0000644 0001762 0000144 00000000654 14703115404 020657 0 ustar ligges users M = cor(mtcars)
ord = corrMatOrder(M, order = 'AOE')
M2 = M[ord, ord]
corrplot.mixed(M2)
corrplot.mixed(M2, lower = 'ellipse', upper = 'circle')
corrplot.mixed(M2, lower = 'square', upper = 'circle')
corrplot.mixed(M2, lower = 'shade', upper = 'circle')
corrplot.mixed(M2, tl.pos = 'lt')
corrplot.mixed(M2, tl.pos = 'lt', diag = 'u')
corrplot.mixed(M2, tl.pos = 'lt', diag = 'l')
corrplot.mixed(M2, tl.pos = 'n')
corrplot/vignettes/example-corrplot.R 0000644 0001762 0000144 00000021601 14703115404 017545 0 ustar ligges users data(mtcars)
M = cor(mtcars)
set.seed(0)
## different color series
## COL2: Get diverging colors
## c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu')
## COL1: Get sequential colors
## c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd', 'YlOrRd', 'YlOrBr', 'YlGn')
wb = c('white', 'black')
par(ask = TRUE)
## different color scale and methods to display corr-matrix
corrplot(M, method = 'number', col = 'black', cl.pos = 'n')
corrplot(M, method = 'number')
corrplot(M)
corrplot(M, order = 'AOE')
corrplot(M, order = 'AOE', addCoef.col = 'grey')
corrplot(M, order = 'AOE', cl.length = 21, addCoef.col = 'grey')
corrplot(M, order = 'AOE', col = COL2(n=10), addCoef.col = 'grey')
corrplot(M, order = 'AOE', col = COL2('PiYG'))
corrplot(M, order = 'AOE', col = COL2('PRGn'), addCoef.col = 'grey')
corrplot(M, order = 'AOE', col = COL2('PuOr', 20), cl.length = 21, addCoef.col = 'grey')
corrplot(M, order = 'AOE', col = COL2('PuOr', 10), addCoef.col = 'grey')
corrplot(M, order = 'AOE', col = COL2('RdYlBu', 100))
corrplot(M, order = 'AOE', col = COL2('RdYlBu', 10))
corrplot(M, method = 'color', col = COL2(n=20), cl.length = 21, order = 'AOE',
addCoef.col = 'grey')
corrplot(M, method = 'square', col = COL2(n=200), order = 'AOE')
corrplot(M, method = 'ellipse', col = COL2(n=200), order = 'AOE')
corrplot(M, method = 'shade', col = COL2(n=20), order = 'AOE')
corrplot(M, method = 'pie', order = 'AOE')
## col = wb
corrplot(M, col = wb, order = 'AOE', outline = TRUE, cl.pos = 'n')
## like Chinese wiqi, suit for either on screen or white-black print.
corrplot(M, col = wb, bg = 'gold2', order = 'AOE', cl.pos = 'n')
## mixed methods: It's more efficient if using function 'corrplot.mixed'
## circle + ellipse
corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd')
corrplot(M, add = TRUE, type = 'lower', method = 'ellipse', order = 'AOE',
diag = FALSE, tl.pos = 'n', cl.pos = 'n')
## circle + square
corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd')
corrplot(M, add = TRUE, type = 'lower', method = 'square', order = 'AOE',
diag = FALSE, tl.pos = 'n', cl.pos = 'n')
## circle + colorful number
corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd')
corrplot(M, add = TRUE, type = 'lower', method = 'number', order = 'AOE',
diag = FALSE, tl.pos = 'n', cl.pos = 'n')
## circle + black number
corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'tp')
corrplot(M, add = TRUE, type = 'lower', method = 'number', order = 'AOE',
col = 'black', diag = FALSE, tl.pos = 'n', cl.pos = 'n')
## order is hclust and draw rectangles
corrplot(M, order = 'hclust')
corrplot(M, order = 'hclust', addrect = 2)
corrplot(M, order = 'hclust', addrect = 3, rect.col = 'red')
corrplot(M, order = 'hclust', addrect = 4, rect.col = 'blue')
corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 4)
## visualize a matrix in [0, 1]
corrplot(abs(M), order = 'AOE', col.lim = c(0, 1))
corrplot(abs(M), order = 'AOE', is.corr = FALSE, col.lim = c(0, 1))
# when is.corr=TRUE, col.lim only affect the color legend
# If you change it, the color is still assigned on [-1, 1]
corrplot(M/2)
corrplot(M/2, col.lim = c(-0.5, 0.5))
# when is.corr=FALSE, col.lim is also used to assign colors
# if the matrix have both positive and negative values
# the matrix transformation keep every values positive and negative
corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2))
corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2) * 2)
corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2) * 4)
## 0.5~0.6
corrplot(abs(M)/10+0.5, col = COL1('Greens', 10))
corrplot(abs(M)/10+0.5, is.corr = FALSE, col.lim = c(0.5, 0.6), col = COL1('YlGn', 10))
## visualize a matrix in [-100, 100]
ran = round(matrix(runif(225, -100, 100), 15))
corrplot(ran, is.corr = FALSE)
corrplot(ran, is.corr = FALSE, col.lim = c(-100, 100))
## visualize a matrix in [100, 300]
ran2 = ran + 200
# bad color, not suitable for a matrix in [100, 300]
corrplot(ran2, is.corr = FALSE, col.lim = c(100, 300), col = COL2(, 100))
# good color
corrplot(ran2, is.corr = FALSE, col.lim = c(100, 300), col = COL1(, 100))
## text-labels and plot type
corrplot(M, order = 'AOE', tl.srt = 45)
corrplot(M, order = 'AOE', tl.srt = 60)
corrplot(M, order = 'AOE', tl.pos = 'd', cl.pos = 'n')
corrplot(M, order = 'AOE', diag = FALSE, tl.pos = 'd')
corrplot(M, order = 'AOE', type = 'upper')
corrplot(M, order = 'AOE', type = 'upper', diag = FALSE)
corrplot(M, order = 'AOE', type = 'lower', cl.pos = 'b')
corrplot(M, order = 'AOE', type = 'lower', cl.pos = 'b', diag = FALSE)
#### color-legend
corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'l')
corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'c')
corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'r')
corrplot(M, order = 'AOE', cl.pos = 'b')
corrplot(M, order = 'AOE', cl.pos = 'b', tl.pos = 'd')
corrplot(M, order = 'AOE', cl.pos = 'n')
## deal with missing Values
M2 = M
diag(M2) = NA
corrplot(M2)
corrplot(M2, na.label = 'o')
corrplot(M2, na.label = 'NA')
##the input matrix is not square
corrplot(M[1:8, ])
corrplot(M[, 1:8])
testRes = cor.mtest(mtcars, conf.level = 0.95)
## specialized the insignificant value according to the significant level
corrplot(M, p.mat = testRes$p, sig.level = 0.05, order = 'hclust', addrect = 2)
## leave blank on no significant coefficient
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig ='blank',
addCoef.col ='black', number.cex = 0.8, order = 'AOE', diag = FALSE)
## add p-values on no significant coefficients
corrplot(M, p.mat = testRes$p, insig = 'p-value')
## add all p-values
corrplot(M, p.mat = testRes$p, insig = 'p-value', sig.level = -1)
## add significant level stars
corrplot(M, p.mat = testRes$p, method = 'color', diag = FALSE, type = 'upper',
sig.level = c(0.001, 0.01, 0.05), pch.cex = 0.9,
insig = 'label_sig', pch.col = 'grey20', order = 'AOE')
## add significant level stars and cluster rectangles
corrplot(M, p.mat = testRes$p, tl.pos = 'd', order = 'hclust', addrect = 2,
insig = 'label_sig', sig.level = c(0.001, 0.01, 0.05),
pch.cex = 0.9, pch.col = 'grey20')
# Visualize confidence interval
corrplot(M, lowCI = testRes$lowCI, uppCI = testRes$uppCI, order = 'hclust',
tl.pos = 'd', rect.col = 'navy', plotC = 'rect', cl.pos = 'n')
# Visualize confidence interval and cross the significant coefficients
corrplot(M, p.mat = testRes$p, lowCI = testRes$lowCI, uppCI = testRes$uppCI,
addrect = 3, rect.col = 'navy', plotC = 'rect', cl.pos = 'n')
res1 = cor.mtest(mtcars, conf.level = 0.95)
res2 = cor.mtest(mtcars, conf.level = 0.99)
## plot confidence interval(0.95), 'circle' method
corrplot(M, low = res1$uppCI, upp = res1$uppCI,
plotCI = 'circle', addg = 'grey20', cl.pos = 'n')
corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
plotCI = 'circle', addg = 'grey20', cl.pos = 'n')
corrplot(M, low = res1$lowCI, upp = res1$uppCI,
col = c('white', 'black'), bg = 'gold2', order = 'AOE',
plotCI = 'circle', cl.pos = 'n', pch.col = 'red')
corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
col = c('white', 'black'), bg = 'gold2', order = 'AOE',
plotCI = 'circle', cl.pos = 'n', pch.col = 'red')
## plot confidence interval(0.95), 'square' method
corrplot(M, low = res1$lowCI, upp = res1$uppCI,
col = c('white', 'black'), bg = 'gold2', order = 'AOE',
plotCI = 'square', addg = NULL, cl.pos = 'n')
corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
col = c('white', 'black'), bg = 'gold2', order = 'AOE', pch.col = 'red',
plotCI = 'square', addg = NULL, cl.pos = 'n')
## plot confidence interval0.95, 0.95, 0.99, 'rect' method
corrplot(M, low = res1$lowCI, upp = res1$uppCI, order = 'hclust',
rect.col = 'navy', plotCI = 'rect', cl.pos = 'n')
corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
order = 'hclust', pch.col = 'red', sig.level = 0.05, addrect = 3,
rect.col = 'navy', plotCI = 'rect', cl.pos = 'n')
corrplot(M, p.mat = res2$p, low = res2$lowCI, upp = res2$uppCI,
order = 'hclust', pch.col = 'red', sig.level = 0.01, addrect = 3,
rect.col = 'navy', plotCI = 'rect', cl.pos = 'n')
## an animation of changing confidence interval in different significance level
## begin.animaton
par(ask = FALSE)
for (i in seq(0.1, 0, -0.005)) {
tmp = cor.mtest(mtcars, conf.level = 1 - i)
corrplot(M, p.mat = tmp$p, low = tmp$lowCI, upp = tmp$uppCI, order = 'hclust',
pch.col = 'red', sig.level = i, plotCI = 'rect', cl.pos = 'n',
mar = c(0, 0, 1, 0),
title = substitute(alpha == x,
list(x = format(i, digits = 3, nsmall = 3))))
Sys.sleep(0.15)
}
## end.animaton
corrplot/vignettes/corrplot-intro.Rmd 0000644 0001762 0000144 00000047346 14703115404 017604 0 ustar ligges users ---
title: 'An Introduction to corrplot Package'
author: 'Taiyun Wei, Viliam Simko'
date: '`r Sys.Date()`'
output:
prettydoc::html_pretty:
theme: cayman
toc: true
toc-title: 'Table of Contents'
vignette: >
%\VignetteIndexEntry{An Introduction to corrplot Package}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
fig.align = 'center',
fig.path = 'webimg/',
fig.width = 7,
fig.height = 7,
out.width = '600px',
dev = 'png')
get_os = function() {
sysinf = Sys.info()
if (!is.null(sysinf)) {
os = sysinf['sysname']
if (os == 'Darwin')
os = 'osx'
} else { ## mystery machine
os = .Platform$OS.type
if (grepl('^darwin', R.version$os))
os = 'osx'
if (grepl('linux-gnu', R.version$os))
os = 'linux'
}
tolower(os)
}
if(get_os() =='windows' & capabilities('cairo') | all(capabilities(c('cairo', 'X11')))) {
knitr::opts_chunk$set(dev.args = list(type='cairo'))
}
```
## Introduction
R package **corrplot** provides a visual exploratory tool on correlation matrix that
supports automatic variable reordering to help detect hidden patterns among variables.
corrplot is very easy to use and provides a rich array of plotting options in
visualization method, graphic layout, color, legend, text labels, etc.
It also provides p-values and confidence intervals to help users determine the
statistical significance of the correlations.
`corrplot()` has about 50 parameters, however the mostly common ones are only a few.
We can get a correlation matrix plot with only one line of code in most scenes.
The mostly using parameters include `method`, `type`, `order`, `diag`, and etc.
There are seven visualization methods (parameter `method`) in
corrplot package, named `'circle'`, `'square'`, `'ellipse'`,
`'number'`, `'shade'`, `'color'`, `'pie'`. Color intensity of the glyph
is proportional to the correlation coefficients by default color setting.
- `'circle'` and `'square'`, the **areas** of circles or squares show the
absolute value of corresponding correlation coefficients.
- `'ellipse'`, the ellipses have their eccentricity parametrically scaled to the correlation value.
It comes from D.J. Murdoch and E.D. Chow's job, see in section References.
- `'number'`, coefficients numbers with different color.
- `'color'`, square of equal size with different color.
- `'shade'`, similar to `'color'`, but the negative coefficients glyphs are shaded.
Method `'pie'` and `'shade'` come from Michael Friendly's job.
- `'pie'`, the circles are filled clockwise for positive values, anti-clockwise for negative
values.
`corrplot.mixed()` is a wrapped function for mixed visualization style,
which can set the visual methods of lower and upper triangular
separately.
There are three layout types (parameter `type`): `'full'`, `'upper'` and
`'lower'`.
The correlation matrix can be reordered according to the correlation
matrix coefficients. This is important to identify the hidden structure
and pattern in the matrix.
```{r intro}
library(corrplot)
M = cor(mtcars)
corrplot(M, method = 'number') # colorful number
corrplot(M, method = 'color', order = 'alphabet')
corrplot(M) # by default, method = 'circle'
corrplot(M, order = 'AOE') # after 'AOE' reorder
corrplot(M, method = 'shade', order = 'AOE', diag = FALSE)
corrplot(M, method = 'square', order = 'FPC', type = 'lower', diag = FALSE)
corrplot(M, method = 'ellipse', order = 'AOE', type = 'upper')
corrplot.mixed(M, order = 'AOE')
corrplot.mixed(M, lower = 'shade', upper = 'pie', order = 'hclust')
```
## Reorder a correlation matrix
The details of four `order` algorithms, named `'AOE'`, `'FPC'`,
`'hclust'`, `'alphabet'` are as following.
- `'AOE'` is for the angular order of the eigenvectors. It is
calculated from the order of the angles $a_i$,
$$
a_i =
\begin{cases}
\arctan (e_{i2}/e_{i1}), & \text{if $e_{i1}>0$;}
\newline
\arctan (e_{i2}/e_{i1}) + \pi, & \text{otherwise.}
\end{cases}
$$
where $e_1$ and $e_2$ are the largest two eigenvalues of the
correlation matrix. See [Michael Friendly
(2002)](http://www.datavis.ca/papers/corrgram.pdf) for details.
- `'FPC'` for the first principal component order.
- `'hclust'` for hierarchical clustering order, and `'hclust.method'`
for the agglomeration method to be used. `'hclust.method'` should be
one of `'ward'`, `'ward.D'`, `'ward.D2'`, `'single'`, `'complete'`,
`'average'`, `'mcquitty'`, `'median'` or `'centroid'`.
- `'alphabet'` for alphabetical order.
You can also reorder the matrix 'manually' via function
`corrMatOrder()`.
If using `'hclust'`, `corrplot()` can draw rectangles around the plot of
correlation matrix based on the results of hierarchical clustering.
```{r hclust}
corrplot(M, order = 'hclust', addrect = 2)
corrplot(M, method = 'square', diag = FALSE, order = 'hclust',
addrect = 3, rect.col = 'blue', rect.lwd = 3, tl.pos = 'd')
```
R package **seriation** provides the infrastructure for ordering objects with an
implementation of several seriation/sequencing/ordination techniques to reorder
matrices, dissimilarity matrices, and dendrograms. For more information,
see in section References.
We can reorder the matrix via **seriation** package and then corrplot it.
Here are some examples.
```{r seriation}
library(seriation)
list_seriation_methods('matrix')
list_seriation_methods('dist')
data(Zoo)
Z = cor(Zoo[, -c(15, 17)])
dist2order = function(corr, method, ...) {
d_corr = as.dist(1 - corr)
s = seriate(d_corr, method = method, ...)
i = get_order(s)
return(i)
}
```
Methods `'PCA_angle'` and `'HC'` in **seriation**, are same as `'AOE'` and `'hclust'`
separately in `corrplot()` and `corrMatOrder()`.
Here are some plots after seriation.
```{r seriation-plot}
# Fast Optimal Leaf Ordering for Hierarchical Clustering
i = dist2order(Z, 'OLO')
corrplot(Z[i, i], cl.pos = 'n')
# Quadratic Assignment Problem
i = dist2order(Z, 'QAP_2SUM')
corrplot(Z[i, i], cl.pos = 'n')
# Multidimensional Scaling
i = dist2order(Z, 'MDS_nonmetric')
corrplot(Z[i, i], cl.pos = 'n')
# Simulated annealing
i = dist2order(Z, 'ARSA')
corrplot(Z[i, i], cl.pos = 'n')
# TSP solver
i = dist2order(Z, 'TSP')
corrplot(Z[i, i], cl.pos = 'n')
# Spectral seriation
i = dist2order(Z, 'Spectral')
corrplot(Z[i, i], cl.pos = 'n')
```
`corrRect()` can add rectangles on the plot with three ways(parameter
`index`, `name` and `namesMat`) after `corrplot()`.
We can use pipe operator `*>%` in package `magrittr` with more convenience.
Since R 4.1.0, `|>` is supported without extra package.
```{r rectangles}
library(magrittr)
# Rank-two ellipse seriation, use index parameter
i = dist2order(Z, 'R2E')
corrplot(Z[i, i], cl.pos = 'n') %>% corrRect(c(1, 9, 15))
# use name parameter
# Since R 4.1.0, the following one line code works:
# corrplot(M, order = 'AOE') |> corrRect(name = c('gear', 'wt', 'carb'))
corrplot(Z, order = 'AOE') %>%
corrRect(name = c('tail', 'airborne', 'venomous', 'predator'))
# use namesMat parameter
r = rbind(c('eggs', 'catsize', 'airborne', 'milk'),
c('catsize', 'eggs', 'milk', 'airborne'))
corrplot(Z, order = 'hclust') %>% corrRect(namesMat = r)
```
## Change color spectra, color-legend and text-legend
We can get sequential and diverging colors from `COL1()` and `COL2()`.
The color palettes are borrowed from `RColorBrewer` package.
**Notice**: the middle color getting from `COL2()` is fixed to `'#FFFFFF'`(white),
thus we can visualizing element 0 with white color.
- `COL1()`: Get sequential colors, suitable for visualize a non-negative or
non-positive matrix (e.g. matrix in [0, 20], or [-100, -10], or [100, 500]).
- `COL2()`: Get diverging colors, suitable for visualize a matrix which elements
are partly positive and partly negative (e.g. correlation matrix in [-1, 1], or [-20, 100]).
The colors of the correlation plots can be customized by `col` in `corrplot()`.
They are distributed uniformly in `col.lim` interval.
- `col`: vector, the colors of glyphs. They are distributed uniformly in `col.lim` interval. By default,
- If `is.corr` is `TRUE`, `col` will be `COL2('RdBu', 200)`.
- If `is.corr` is `FALSE`,
- and `corr` is a non-negative or non-positive matrix, `col` will be `COL1('YlOrBr', 200)`;
- otherwise (elements are partly positive and partly negative), `col` will be `COL2('RdBu', 200)`.
- `col.lim`: the limits (x1, x2) interval for assigning color by `col`. By default,
- `col.lim` will be `c(-1, 1)` when `is.corr` is `TRUE`,
- `col.lim` will be `c(min(corr), max(corr))` when `is.corr` is `FALSE`.
- **NOTICE**: if you set `col.lim` when `is.corr` is `TRUE`, the assigning colors are still
distributed uniformly in [-1, 1], it only affect the display on color-legend.
- `is.corr`: logical, whether the input matrix is a correlation matrix or not. The default value is `TRUE`.
We can visualize a non-correlation matrix by setting `is.corr = FALSE`.
Here all diverging colors from `COL2()` and sequential colors from `COL1()` are shown below.
**Diverging colors**:
```{r echo=FALSE, fig.width = 8, fig.height = 6, out.width = '700px'}
## diverging colors
plot.new()
par(mar = c(0, 0, 0, 0) + 0.1)
plot.window(xlim = c(-0.2, 1.1), ylim = c(0, 1), xaxs = 'i', yaxs = 'i')
col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu')
for(i in 1:length(col)) {
colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8)
}
```
**Sequential colors**:
```{r echo=FALSE, fig.width = 8, fig.height = 6, out.width = '700px'}
## sequential colors
plot.new()
par(mar = c(0, 0, 0, 0) + 0.1)
plot.window(xlim = c(-0.2, 1.1), ylim = c(0, 1), xaxs = 'i', yaxs = 'i')
col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd',
'YlOrRd', 'YlOrBr', 'YlGn')
for(i in 1:length(col)) {
colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2)
}
```
Usage of `COL1()` and `COL2()`:
```{r eval=FALSE}
COL1(sequential = c("Oranges", "Purples", "Reds", "Blues", "Greens",
"Greys", "OrRd", "YlOrRd", "YlOrBr", "YlGn"), n = 200)
COL2(diverging = c("RdBu", "BrBG", "PiYG", "PRGn", "PuOr", "RdYlBu"), n = 200)
```
In addition, function `colorRampPalette()` is very convenient for generating color spectrum.
Parameters group `cl.*` is for color-legend. The common-using are:
- `cl.pos` is for the position of color labels. It is character or
logical. If character, it must be one of `'r'` (means right, default
if `type='upper'` or `'full'`), `'b'` (means bottom, default if
`type='lower'`) or `'n'`(means don't draw color-label).
- `cl.ratio` is to justify the width of color-legend, 0.1\~0.2 is
suggested.
Parameters group `tl.*` is for text-legend. The common-using are:
- `tl.pos` is for the position of text labels. It is character or
logical. If character, it must be one of `'lt'`, `'ld'`, `'td'`,
`'d'`, `'l'` or `'n'`. `'lt'`(default if `type='full'`) means left and top,
`'ld'`(default if `type='lower'`) means left and diagonal,
`'td'`(default if `type='upper'`) means top and diagonal(near),
`'d'` means diagonal, `'l'` means left, `'n'` means don't add text-label.
- `tl.cex` is for the size of text label (variable names).
- `tl.srt` is for text label string rotation in degrees.
```{r color}
corrplot(M, order = 'AOE', col = COL2('RdBu', 10))
corrplot(M, order = 'AOE', addCoef.col = 'black', tl.pos = 'd',
cl.pos = 'n', col = COL2('PiYG'))
corrplot(M, method = 'square', order = 'AOE', addCoef.col = 'black', tl.pos = 'd',
cl.pos = 'n', col = COL2('BrBG'))
## bottom color legend, diagonal text legend, rotate text label
corrplot(M, order = 'AOE', cl.pos = 'b', tl.pos = 'd',
col = COL2('PRGn'), diag = FALSE)
## text labels rotated 45 degrees and wider color legend with numbers right aligned
corrplot(M, type = 'lower', order = 'hclust', tl.col = 'black',
cl.ratio = 0.2, tl.srt = 45, col = COL2('PuOr', 10))
## remove color legend, text legend and principal diagonal glyph
corrplot(M, order = 'AOE', cl.pos = 'n', tl.pos = 'n',
col = c('white', 'black'), bg = 'gold2')
```
## Visualize non-correlation matrix, NA value and math label
We can visualize a non-correlation matrix by set `is.corr=FALSE`, and
assign colors by `col.lim`. If the matrix have both positive and
negative values, the matrix transformation keep every values
positiveness and negativeness.
If your matrix is rectangular, you can adjust the aspect ratio with the
`win.asp` parameter to make the matrix rendered as a square.
```{r non-corr}
## matrix in [20, 26], grid.col
N1 = matrix(runif(80, 20, 26), 8)
corrplot(N1, is.corr = FALSE, col.lim = c(20, 30), method = 'color', tl.pos = 'n',
col = COL1('YlGn'), cl.pos = 'b', addgrid.col = 'white', addCoef.col = 'grey50')
## matrix in [-15, 10]
N2 = matrix(runif(80, -15, 10), 8)
## using sequential colors, transKeepSign = FALSE
corrplot(N2, is.corr = FALSE, transKeepSign = FALSE, method = 'color', col.lim = c(-15, 10),
tl.pos = 'n', col = COL1('YlGn'), cl.pos = 'b', addCoef.col = 'grey50')
## using diverging colors, transKeepSign = TRUE (default)
corrplot(N2, is.corr = FALSE, col.lim = c(-15, 10),
tl.pos = 'n', col = COL2('PiYG'), cl.pos = 'b', addCoef.col = 'grey50')
## using diverging colors
corrplot(N2, is.corr = FALSE, method = 'color', col.lim = c(-15, 10), tl.pos = 'n',
col = COL2('PiYG'), cl.pos = 'b', addCoef.col = 'grey50')
```
Notice: when `is.corr` is `TRUE`, `col.lim` only affect the color legend If
you change it, the color on correlation matrix plot is still assigned on
`c(-1, 1)`
```{r col-lim}
# when is.corr=TRUE, col.lim only affect the color legend display
corrplot(M/2)
corrplot(M/2, col.lim=c(-0.5, 0.5))
```
By default, **corrplot** renders NA values as `'?'` characters. Using
`na.label` parameter, it is possible to use a different value (max. two
characters are supported).
Since version `0.78`, it is possible to use
[plotmath](https://www.rdocumentation.org/packages/grDevices/topics/plotmath)
expression in variable names. To activate plotmath rendering, prefix
your label with `'$'`.
```{r NA-math}
M2 = M
diag(M2) = NA
colnames(M2) = rep(c('$alpha+beta', '$alpha[0]', '$alpha[beta]'),
c(4, 4, 3))
rownames(M2) = rep(c('$Sigma[i]^n', '$sigma', '$alpha[0]^100', '$alpha[beta]'),
c(2, 4, 2, 3))
corrplot(10*abs(M2), is.corr = FALSE, col.lim = c(0, 10), tl.cex = 1.5)
```
## Visualize p-value and confidence interval
`corrplot()` can also visualize p-value and confidence interval on the
correlation matrix plot. Here are some important parameters.
About p-value:
- `p.mat` is the p-value matrix, if `NULL`, parameter `sig.level`,
`insig, pch`, `pch.col`, `pch.cex` are invalid.
- `sig.level` is significant level, with default value 0.05. If the
p-value in `p-mat` is bigger than `sig.level`, then the
corresponding correlation coefficient is regarded as insignificant.
If `insig` is `'label_sig'`, `sig.level` can be an increasing vector
of significance levels, in which case `pch` will be used once for
the highest p-value interval and multiple times (e.g. `'*'`, `'**'`,
`'***'`) for each lower p-value interval.
- `insig` Character, specialized insignificant correlation
coefficients, `'pch'` (default), `'p-value'`, `'blank',` `'n'`, or
`'label_sig'`. If `'blank'`, wipe away the corresponding glyphs; if
`'p-value'`, add p-values the corresponding glyphs; if `'pch'`, add
characters (see pch for details) on corresponding glyphs; if `'n'`,
don't take any measures; if `'label_sig'`, mark significant
correlations with `pch` (see `sig.level`).
- `pch` is for adding character on the glyphs of insignificant
correlation coefficients (only valid when insig is `'pch'`). See
`?par` .
About confidence interval:
- `plotCI` is character for the method of plotting confidence
interval. If `'n'`, don't plot confidence interval. If `'rect'`,
plot rectangles whose upper side means upper bound and lower side
means lower bound respectively.
- `lowCI.mat` is the matrix of the lower bound of confidence interval.
- `uppCI.mat` is the Matrix of the upper bound of confidence interval.
We can get p-value matrix and confidence intervals matrix by
`cor.mtest()` which returns a list containing:
- `p` is the p-values matrix.
- `lowCI` is the lower bound of confidence interval matrix.
- `uppCI` is the lower bound of confidence interval matrix.
```{r test}
testRes = cor.mtest(mtcars, conf.level = 0.95)
## specialized the insignificant value according to the significant level
corrplot(M, p.mat = testRes$p, sig.level = 0.10, order = 'hclust', addrect = 2)
## leave blank on non-significant coefficient
## add significant correlation coefficients
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig='blank',
addCoef.col ='black', number.cex = 0.8, order = 'AOE', diag=FALSE)
```
```{r special}
## leave blank on non-significant coefficient
## add all correlation coefficients
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig='blank',
order = 'AOE', diag = FALSE)$corrPos -> p1
text(p1$x, p1$y, round(p1$corr, 2))
```
```{r p-values}
## add p-values on no significant coefficients
corrplot(M, p.mat = testRes$p, insig = 'p-value')
## add all p-values
corrplot(M, p.mat = testRes$p, insig = 'p-value', sig.level = -1)
## add significant level stars
corrplot(M, p.mat = testRes$p, method = 'color', diag = FALSE, type = 'upper',
sig.level = c(0.001, 0.01, 0.05), pch.cex = 0.9,
insig = 'label_sig', pch.col = 'grey20', order = 'AOE')
## add significant level stars and cluster rectangles
corrplot(M, p.mat = testRes$p, tl.pos = 'd', order = 'hclust', addrect = 2,
insig = 'label_sig', sig.level = c(0.001, 0.01, 0.05),
pch.cex = 0.9, pch.col = 'grey20')
```
Visualize confidence interval.
```{r confidence-interval}
# Visualize confidence interval
corrplot(M, lowCI = testRes$lowCI, uppCI = testRes$uppCI, order = 'hclust',
tl.pos = 'd', rect.col = 'navy', plotC = 'rect', cl.pos = 'n')
# Visualize confidence interval and cross the significant coefficients
corrplot(M, p.mat = testRes$p, lowCI = testRes$lowCI, uppCI = testRes$uppCI,
addrect = 3, rect.col = 'navy', plotC = 'rect', cl.pos = 'n')
```
## References
- Michael Friendly (2002). Corrgrams: Exploratory displays for correlation
matrices. The American Statistician, 56, 316--324.
- D.J. Murdoch, E.D. Chow (1996). A graphical display of large correlation
matrices. The American Statistician, 50, 178--180.
- Michael Hahsler, Christian Buchta and Kurt Hornik (2020). seriation: Infrastructure for Ordering
Objects Using Seriation. R package version 1.2-9. https://CRAN.R-project.org/package=seriation
- Hahsler M, Hornik K, Buchta C (2008). "Getting things in order: An introduction to the R package
seriation." _Journal of Statistical Software_, *25*(3), 1-34. ISSN 1548-7660, doi:
10.18637/jss.v025.i03 (URL: https://doi.org/10.18637/jss.v025.i03), .
corrplot/vignettes/example-colorlegend.R 0000644 0001762 0000144 00000003541 14703115404 020201 0 ustar ligges users ## diverging colors
par(mar = c(0, 0, 0, 0) + 0.1)
plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n')
col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu')
for(i in 1:length(col)) {
colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8)
}
## sequential colors
par(mar = c(0, 0, 0, 0) + 0.1)
plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n')
col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd',
'YlOrRd', 'YlOrBr', 'YlGn')
for(i in 1:length(col)) {
colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2)
}
## other examples to show colorlegend function
par(mar = rep(0, 4))
plot(0, xlim = c(0, 6), ylim = c(-0.5, 1.2), type = 'n')
colorlegend(rainbow(100), 0:9)
colorlegend(heat.colors(100), LETTERS[1:12], xlim = c(1, 2))
colorlegend(terrain.colors(100), 0:9, ratio.colbar = 0.6,
lim.segment = c(0, 0.6), xlim = c(2, 3), align = 'l')
colorlegend(topo.colors(100), 0:9, lim.segment = c(0, 0.6),
xlim = c(3, 4), align = 'l', offset = 0)
colorlegend(cm.colors(100), 1:5, xlim = c(4, 5))
colorlegend(sample(rainbow(12)), labels = LETTERS[1:12],
at = seq(0.05, 0.95, len = 12), xlim = c(5, 6), align = 'r')
colorlegend(colbar = grey(1:100 / 100), 1:10, col = 'red', align = 'l',
xlim = c(0, 6), ylim = c(-0.5, -0.1), vertical = FALSE)
colorlegend(sample(rainbow(12)),
labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12),
xlim = c(0, 6), ylim = c(1.1, 1.2), vertical = FALSE)
corrplot/vignettes/example-corrMatOrder.R 0000644 0001762 0000144 00000001167 14703115404 020311 0 ustar ligges users M = cor(mtcars)
(order.AOE = corrMatOrder(M, order = 'AOE'))
(order.FPC = corrMatOrder(M, order = 'FPC'))
(order.hc = corrMatOrder(M, order = 'hclust'))
(order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D'))
M.AOE = M[order.AOE, order.AOE]
M.FPC = M[order.FPC, order.FPC]
M.hc = M[order.hc, order.hc]
M.hc2 = M[order.hc2, order.hc2]
par(ask = TRUE)
corrplot(M)
corrplot(M.AOE)
corrplot(M.FPC)
corrplot(M.hc)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 2)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 3)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 2, method = 'ward.D')
corrplot/vignettes/example-corrRect.R 0000644 0001762 0000144 00000003545 14703115404 017473 0 ustar ligges users data(mtcars)
M = cor(mtcars)
r = rbind(c('gear', 'wt', 'qsec', 'carb'),
c('wt', 'gear', 'carb', 'qsec'))
corrplot(M, order = 'AOE') -> p
corrRect(p, namesMat = r)
# same as using pipe operator `|>` if R version >= 4.1.0:
# corrplot(M, order = 'AOE') |> corrRect(namesMat = r)
r = c('gear', 'carb', 'qsec', 'wt')
corrplot(M, order = 'AOE', type='lower') -> p
corrRect(p, namesMat = r)
# same as using pipe operator `|>` if R version >= 4.1.0:
# corrplot(M, order = 'AOE', type='lower') |> corrRect(namesMat = r)
corrplot(M, order = 'hclust', type = 'upper') -> p
corrRect(p, index = c(1, 6, 11))
# same as using pipe operator `|>` if R version >= 4.1.0:
# corrplot(M, order = 'AOE', type='lower') |> corrRect(index = c(1, 6, 11))
corrplot(M, order = 'hclust') -> p
corrRect(p, name = c('carb', 'qsec', 'gear'))
# same as using pipe operator `|>` if R version >= 4.1.0:
# corrplot(M, order = 'hclust') |> corrRect(name = c('carb', 'qsec', 'gear'))
(order.hc = corrMatOrder(M, order = 'hclust'))
(order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D'))
M.hc = M[order.hc, order.hc]
M.hc2 = M[order.hc2, order.hc2]
par(ask = TRUE)
# same as: corrplot(M, order = 'hclust', addrect = 2)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 2)
# same as: corrplot(M, order = 'hclust', addrect = 3)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 3)
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 2)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 2, method = 'ward.D')
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 3)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 3, method = 'ward.D')
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 4)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 4, method = 'ward.D')
corrplot/vignettes/example-corrRect.hclust.R 0000644 0001762 0000144 00000002073 14703115404 020767 0 ustar ligges users data(mtcars)
M = cor(mtcars)
corrplot(M, order = 'FPC') -> p
corrRect(p, index = c(1, 6, 11))
if(getRversion() >= '4.1.0') {
corrplot(M, order = 'FPC') |> corrRect(index = c(1, 6, 11))
}
(order.hc = corrMatOrder(M, order = 'hclust'))
(order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D2'))
M.hc = M[order.hc, order.hc]
M.hc2 = M[order.hc2, order.hc2]
par(ask = TRUE)
# same as: corrplot(M, order = 'hclust', addrect = 2)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 2)
# same as: corrplot(M, order = 'hclust', addrect = 3)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 3)
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 2)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 2, method = 'ward.D2')
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 3)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 3, method = 'ward.D2')
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 4)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 4, method = 'ward.D2')
corrplot/NAMESPACE 0000644 0001762 0000144 00000000423 14703115404 013333 0 ustar ligges users # Generated by roxygen2: do not edit by hand
export(COL1)
export(COL2)
export(colorlegend)
export(cor.mtest)
export(corrMatOrder)
export(corrRect)
export(corrRect.hclust)
export(corrplot)
export(corrplot.mixed)
import(grDevices)
import(graphics)
import(stats)
corrplot/LICENSE 0000644 0001762 0000144 00000000114 14703126567 013132 0 ustar ligges users YEAR: 2010-2024
COPYRIGHT HOLDER: Capital of Statistics, http://cosx.org/
corrplot/NEWS.md 0000644 0001762 0000144 00000014532 14703126567 013234 0 ustar ligges users # corrplot 0.95
## Changes
* Fix #279: Version 0.94 is not assigning significant ocurrences correctly. (thanks, @brenoliisboa and @david-priest)
# corrplot 0.94
## Changes
* Fix #275: format correlations with `nsmall = number.digits`. (thanks, @the-mad-statter)
# corrplot 0.93
## Changes
* Fix #247: `addgrid.col` and `bg` don't work in mixed plot. (thanks, @ZoomMan91)
* Fix #246: using `insig = "n"` and `p.mat` sometimes causes an error. (thanks, @Sumidu)
* Fix #255: add new parameter `transKeepSign`, whether or not to keep matrix values' sign when transforming non-corr matrix.
# corrplot 0.92
## Changes
* Fix #228: assigning colors incorrectly when `is.corr = FALSE`. (thanks, @bixiou)
* Fix #232: some functions wrongly marked as pure.
* Fix colorlegend lables alignment when `type = 'lower'`.
* Revise the document.
# corrplot 0.91
## Changes
* `tl.pos` add a new parameter `'l'` in `corrplot()`. (thanks, @ggordn3r)
* Add new function `COL1()`: Get sequential colors.
* Add new function `COL2()`: Get diverging colors.
# corrplot 0.90
## Changes
* Add customizable rectangles to correlation plots in `corrRect()`.(#185, requirements from @douglaswhitaker thanks).
* Remove `clus`; add `index` and `name` parameters in `corrRect()`.
* Add `xName`, `yName` two columns in `corrPos` data frame(e.g. `corrplot(...)$corrPos`).
* Rename parameter `cl.lim` to `col.lim` in `corrplot()`.
* Add `arg` to the `corrplot()` return list.
* Add pipe operator `|>` examples when using corrRect().
* Set 'seriation' as Suggests package, and add examples in the document.
* Parameter `number.digits` also works on p-value. (thanks, @bassam-abulnoor)
* Revise the document. (thanks, Shuai Huang)
# corrplot 0.89
## Changes
* Change the return value of corrplot() and corrplot.mixed() from matrix `corr` to `list(corr, corrPos)`
* Fix #177: the top margin is too small when parameter `type` is 'lower' or 'upper'. (thanks, @lijian13)
* Parameter `addCoef.col` works prior to parameter `insig`
* Revise the document
# corrplot 0.88
## Changes
* Revise the document
* Remove full_col and fix #152 #157 #165 #166
* Fix #150 in document, it should be the arcus tangens functions. (thanks, @surmann)
* Change vignette engine from 'knitr::knitr' to 'knitr::rmarkdown'
* Use 'prettydoc' package creating vignettes
* Set 'rmarkdown' and 'prettydoc' as Suggests packages
# corrplot 0.87
## Changes
* Change to the MIT license
* Fix #142: NA issues when cl.lim is supplied. (thanks, @AlexChristensen)
# corrplot 0.83
## Changes
* CITATION now uses fields from DESCRIPTION
* RColorBrewer is now a suggested package (not required)
## New features
* Fixed #99 : A new option insig = 'label_sig' to mark significant correlations.
# corrplot 0.82
## Bug fixes
* Fixed #10: corrplot with type = 'upper' and long colname strings cuts off top labels
* Fixed #19: Color Legend has 0 width when only 1 column is used
* Fixed #70: NA errors when is.corr = F
* Fixed #77: Error when the matrix(corr) contains NA values.
## New features
* Fixed #18: title position and pie corrplot background circle.Used existing parameter `outline` to control the border color of pie symbols.
* Fixed #66: `lim.segment` parameter default value. Added default value 'auto' for `lim.segment` parameter
* Fixed #76: corrplot.mixed with black color correlation coefficient.Added two new parameters `lower.col` and `upper.col`
* Fixed #79: Changing aspect ratio for the plot.Added a new parameter `win.asp` which controls the aspect ratio of the plotted matrix.
* Added more examples to the vignette
# corrplot 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
# corrplot 0.80
## Changes
* Fixed #70: Enable to plot a matrix with NA when 'is.corr = F'
# corrplot 0.77
## Changes
* Fixed #58: make sure the margin is correct in corrplot.mixed().
* Revised document.
# corrplot 0.76
## Changes
* In corrplot(), added parameters na.label and na.label.col that define how NA values inside a matrix should be rendered.
* In corrplot(), na.label can now use one or two characters, default is '?' (issue #55)
* Fixed #16: checks for [-1, 1] interval are too strict.
* Fixed #15: error with correlation plot using insig argument when all values are significant.
* Fixed #9: added ward.D and ward.D2 hclust methods (thanks, #jeffzemla)
# corrplot 0.70
## Changes
* In corrplot(), parameter insig add a option 'p-value', now p-values can be conveniently added on the glyph.
* Return value changes, corrplot() now returns a reordered correlation matrix.
# corrplot 0.66
## Changes
* Add html vignette, which was generated from markdown file by knitr.
* In corrplot(), remove parameter 'zoom', add 'is.corr'; now it is more convenient to
visualize non-correlation matrix. Parameter 'addtextlabel' was renamed as 'tl.pos',
and 'addcolorlabel' was renamed as 'cl.pos'.
# corrplot 0.60
## New features
* Now corrplot() can deal with the matrix not in [-1,1] by parameter 'zoom' now.
Warning: please make sure the visualization you take after data zoom is meaningful!
## Changes
* Function corr.rect() was renamed as corrRect().
* Revise document. (THANKS, Tao Gao)
* In function corrplot(), 'order''s choice 'PCA' was precisely renamed as 'AOE'.
and 'FPC' was added for the first principal component order.
* Add function corrMatOrder(), and corrplot.mixed().
* Remove seldom used functions: corrplot.circle(), corrplot.ellipse(),
corrplot.square(), corrplot.shade(), corrplot.color(), corrplot.number().
* In corrplot(), remove parameter 'assign.col' and 'cl.range',
add 'zoom', 'cl.lim' and 'addCoefasPercent'.
# corrplot 0.54
## Changes
* Parameter 'tl.cex' in function corrplot() is more sensitive.
## Bug fixes
* The issue that too much space adding to the margins (especially using a longer text label
or a larger font size) has been fixed.
* Specifying wrong color to the glyphs has been fixed.
corrplot/inst/ 0000755 0001762 0000144 00000000000 14703127132 013073 5 ustar ligges users corrplot/inst/CITATION 0000644 0001762 0000144 00000001405 14703115404 014227 0 ustar ligges users YEAR <- strftime(meta$Date, '%Y')
# all roles, see also http://www.loc.gov/marc/relators/relaterm.html
ALL <- eval(parse(text = meta$`Authors@R`))
# authors only
AUTHORS <- ALL[sapply(ALL$role, function(x) 'aut' %in% x)]
AUTHORS <- paste(AUTHORS$given, AUTHORS$family, collapse = ' and ')
bibentry('Manual',
key = paste0(meta$Package, YEAR),
title = sprintf('R package \'%s\': %s', meta$Package, meta$Title),
author = AUTHORS,
year = YEAR,
note = sprintf('(Version %s)', meta$Version),
url = meta$URL,
mheader = sprintf('To cite %s in publications use:', meta$Package),
textVersion = sprintf(
'%s (%s). R package \'%s\': %s (Version %s). Available from %s',
AUTHORS, YEAR, meta$Package, meta$Title, meta$Version, meta$URL
)
)
corrplot/inst/doc/ 0000755 0001762 0000144 00000000000 14703127132 013640 5 ustar ligges users corrplot/inst/doc/corrplot-intro.R 0000644 0001762 0000144 00000022352 14703127131 016763 0 ustar ligges users ## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
fig.align = 'center',
fig.path = 'webimg/',
fig.width = 7,
fig.height = 7,
out.width = '600px',
dev = 'png')
get_os = function() {
sysinf = Sys.info()
if (!is.null(sysinf)) {
os = sysinf['sysname']
if (os == 'Darwin')
os = 'osx'
} else { ## mystery machine
os = .Platform$OS.type
if (grepl('^darwin', R.version$os))
os = 'osx'
if (grepl('linux-gnu', R.version$os))
os = 'linux'
}
tolower(os)
}
if(get_os() =='windows' & capabilities('cairo') | all(capabilities(c('cairo', 'X11')))) {
knitr::opts_chunk$set(dev.args = list(type='cairo'))
}
## ----intro--------------------------------------------------------------------
library(corrplot)
M = cor(mtcars)
corrplot(M, method = 'number') # colorful number
corrplot(M, method = 'color', order = 'alphabet')
corrplot(M) # by default, method = 'circle'
corrplot(M, order = 'AOE') # after 'AOE' reorder
corrplot(M, method = 'shade', order = 'AOE', diag = FALSE)
corrplot(M, method = 'square', order = 'FPC', type = 'lower', diag = FALSE)
corrplot(M, method = 'ellipse', order = 'AOE', type = 'upper')
corrplot.mixed(M, order = 'AOE')
corrplot.mixed(M, lower = 'shade', upper = 'pie', order = 'hclust')
## ----hclust-------------------------------------------------------------------
corrplot(M, order = 'hclust', addrect = 2)
corrplot(M, method = 'square', diag = FALSE, order = 'hclust',
addrect = 3, rect.col = 'blue', rect.lwd = 3, tl.pos = 'd')
## ----seriation----------------------------------------------------------------
library(seriation)
list_seriation_methods('matrix')
list_seriation_methods('dist')
data(Zoo)
Z = cor(Zoo[, -c(15, 17)])
dist2order = function(corr, method, ...) {
d_corr = as.dist(1 - corr)
s = seriate(d_corr, method = method, ...)
i = get_order(s)
return(i)
}
## ----seriation-plot-----------------------------------------------------------
# Fast Optimal Leaf Ordering for Hierarchical Clustering
i = dist2order(Z, 'OLO')
corrplot(Z[i, i], cl.pos = 'n')
# Quadratic Assignment Problem
i = dist2order(Z, 'QAP_2SUM')
corrplot(Z[i, i], cl.pos = 'n')
# Multidimensional Scaling
i = dist2order(Z, 'MDS_nonmetric')
corrplot(Z[i, i], cl.pos = 'n')
# Simulated annealing
i = dist2order(Z, 'ARSA')
corrplot(Z[i, i], cl.pos = 'n')
# TSP solver
i = dist2order(Z, 'TSP')
corrplot(Z[i, i], cl.pos = 'n')
# Spectral seriation
i = dist2order(Z, 'Spectral')
corrplot(Z[i, i], cl.pos = 'n')
## ----rectangles---------------------------------------------------------------
library(magrittr)
# Rank-two ellipse seriation, use index parameter
i = dist2order(Z, 'R2E')
corrplot(Z[i, i], cl.pos = 'n') %>% corrRect(c(1, 9, 15))
# use name parameter
# Since R 4.1.0, the following one line code works:
# corrplot(M, order = 'AOE') |> corrRect(name = c('gear', 'wt', 'carb'))
corrplot(Z, order = 'AOE') %>%
corrRect(name = c('tail', 'airborne', 'venomous', 'predator'))
# use namesMat parameter
r = rbind(c('eggs', 'catsize', 'airborne', 'milk'),
c('catsize', 'eggs', 'milk', 'airborne'))
corrplot(Z, order = 'hclust') %>% corrRect(namesMat = r)
## ----echo=FALSE, fig.width = 8, fig.height = 6, out.width = '700px'----------
## diverging colors
plot.new()
par(mar = c(0, 0, 0, 0) + 0.1)
plot.window(xlim = c(-0.2, 1.1), ylim = c(0, 1), xaxs = 'i', yaxs = 'i')
col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu')
for(i in 1:length(col)) {
colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8)
}
## ----echo=FALSE, fig.width = 8, fig.height = 6, out.width = '700px'----------
## sequential colors
plot.new()
par(mar = c(0, 0, 0, 0) + 0.1)
plot.window(xlim = c(-0.2, 1.1), ylim = c(0, 1), xaxs = 'i', yaxs = 'i')
col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd',
'YlOrRd', 'YlOrBr', 'YlGn')
for(i in 1:length(col)) {
colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2)
}
## ----eval=FALSE---------------------------------------------------------------
# COL1(sequential = c("Oranges", "Purples", "Reds", "Blues", "Greens",
# "Greys", "OrRd", "YlOrRd", "YlOrBr", "YlGn"), n = 200)
#
# COL2(diverging = c("RdBu", "BrBG", "PiYG", "PRGn", "PuOr", "RdYlBu"), n = 200)
## ----color--------------------------------------------------------------------
corrplot(M, order = 'AOE', col = COL2('RdBu', 10))
corrplot(M, order = 'AOE', addCoef.col = 'black', tl.pos = 'd',
cl.pos = 'n', col = COL2('PiYG'))
corrplot(M, method = 'square', order = 'AOE', addCoef.col = 'black', tl.pos = 'd',
cl.pos = 'n', col = COL2('BrBG'))
## bottom color legend, diagonal text legend, rotate text label
corrplot(M, order = 'AOE', cl.pos = 'b', tl.pos = 'd',
col = COL2('PRGn'), diag = FALSE)
## text labels rotated 45 degrees and wider color legend with numbers right aligned
corrplot(M, type = 'lower', order = 'hclust', tl.col = 'black',
cl.ratio = 0.2, tl.srt = 45, col = COL2('PuOr', 10))
## remove color legend, text legend and principal diagonal glyph
corrplot(M, order = 'AOE', cl.pos = 'n', tl.pos = 'n',
col = c('white', 'black'), bg = 'gold2')
## ----non-corr-----------------------------------------------------------------
## matrix in [20, 26], grid.col
N1 = matrix(runif(80, 20, 26), 8)
corrplot(N1, is.corr = FALSE, col.lim = c(20, 30), method = 'color', tl.pos = 'n',
col = COL1('YlGn'), cl.pos = 'b', addgrid.col = 'white', addCoef.col = 'grey50')
## matrix in [-15, 10]
N2 = matrix(runif(80, -15, 10), 8)
## using sequential colors, transKeepSign = FALSE
corrplot(N2, is.corr = FALSE, transKeepSign = FALSE, method = 'color', col.lim = c(-15, 10),
tl.pos = 'n', col = COL1('YlGn'), cl.pos = 'b', addCoef.col = 'grey50')
## using diverging colors, transKeepSign = TRUE (default)
corrplot(N2, is.corr = FALSE, col.lim = c(-15, 10),
tl.pos = 'n', col = COL2('PiYG'), cl.pos = 'b', addCoef.col = 'grey50')
## using diverging colors
corrplot(N2, is.corr = FALSE, method = 'color', col.lim = c(-15, 10), tl.pos = 'n',
col = COL2('PiYG'), cl.pos = 'b', addCoef.col = 'grey50')
## ----col-lim------------------------------------------------------------------
# when is.corr=TRUE, col.lim only affect the color legend display
corrplot(M/2)
corrplot(M/2, col.lim=c(-0.5, 0.5))
## ----NA-math------------------------------------------------------------------
M2 = M
diag(M2) = NA
colnames(M2) = rep(c('$alpha+beta', '$alpha[0]', '$alpha[beta]'),
c(4, 4, 3))
rownames(M2) = rep(c('$Sigma[i]^n', '$sigma', '$alpha[0]^100', '$alpha[beta]'),
c(2, 4, 2, 3))
corrplot(10*abs(M2), is.corr = FALSE, col.lim = c(0, 10), tl.cex = 1.5)
## ----test---------------------------------------------------------------------
testRes = cor.mtest(mtcars, conf.level = 0.95)
## specialized the insignificant value according to the significant level
corrplot(M, p.mat = testRes$p, sig.level = 0.10, order = 'hclust', addrect = 2)
## leave blank on non-significant coefficient
## add significant correlation coefficients
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig='blank',
addCoef.col ='black', number.cex = 0.8, order = 'AOE', diag=FALSE)
## ----special------------------------------------------------------------------
## leave blank on non-significant coefficient
## add all correlation coefficients
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig='blank',
order = 'AOE', diag = FALSE)$corrPos -> p1
text(p1$x, p1$y, round(p1$corr, 2))
## ----p-values-----------------------------------------------------------------
## add p-values on no significant coefficients
corrplot(M, p.mat = testRes$p, insig = 'p-value')
## add all p-values
corrplot(M, p.mat = testRes$p, insig = 'p-value', sig.level = -1)
## add significant level stars
corrplot(M, p.mat = testRes$p, method = 'color', diag = FALSE, type = 'upper',
sig.level = c(0.001, 0.01, 0.05), pch.cex = 0.9,
insig = 'label_sig', pch.col = 'grey20', order = 'AOE')
## add significant level stars and cluster rectangles
corrplot(M, p.mat = testRes$p, tl.pos = 'd', order = 'hclust', addrect = 2,
insig = 'label_sig', sig.level = c(0.001, 0.01, 0.05),
pch.cex = 0.9, pch.col = 'grey20')
## ----confidence-interval------------------------------------------------------
# Visualize confidence interval
corrplot(M, lowCI = testRes$lowCI, uppCI = testRes$uppCI, order = 'hclust',
tl.pos = 'd', rect.col = 'navy', plotC = 'rect', cl.pos = 'n')
# Visualize confidence interval and cross the significant coefficients
corrplot(M, p.mat = testRes$p, lowCI = testRes$lowCI, uppCI = testRes$uppCI,
addrect = 3, rect.col = 'navy', plotC = 'rect', cl.pos = 'n')
corrplot/inst/doc/corrplot-intro.html 0000644 0001762 0000144 00022737566 14703127132 017557 0 ustar ligges users
An Introduction to corrplot Package
Introduction
R package corrplot provides a visual exploratory
tool on correlation matrix that supports automatic variable reordering
to help detect hidden patterns among variables.
corrplot is very easy to use and provides a rich array of plotting
options in visualization method, graphic layout, color, legend, text
labels, etc. It also provides p-values and confidence intervals to help
users determine the statistical significance of the correlations.
corrplot()
has about 50 parameters, however the mostly
common ones are only a few. We can get a correlation matrix plot with
only one line of code in most scenes.
The mostly using parameters include method
,
type
, order
, diag
, and etc.
There are seven visualization methods (parameter method
)
in corrplot package, named 'circle'
, 'square'
,
'ellipse'
, 'number'
, 'shade'
,
'color'
, 'pie'
. Color intensity of the glyph
is proportional to the correlation coefficients by default color
setting.
'circle'
and 'square'
, the
areas of circles or squares show the absolute value of
corresponding correlation coefficients.
'ellipse'
, the ellipses have their eccentricity
parametrically scaled to the correlation value. It comes from D.J.
Murdoch and E.D. Chow’s job, see in section References.
'number'
, coefficients numbers with different
color.
'color'
, square of equal size with different
color.
'shade'
, similar to 'color'
, but the
negative coefficients glyphs are shaded. Method 'pie'
and
'shade'
come from Michael Friendly’s job.
'pie'
, the circles are filled clockwise for positive
values, anti-clockwise for negative values.
corrplot.mixed()
is a wrapped function for mixed
visualization style, which can set the visual methods of lower and upper
triangular separately.
There are three layout types (parameter type
):
'full'
, 'upper'
and 'lower'
.
The correlation matrix can be reordered according to the correlation
matrix coefficients. This is important to identify the hidden structure
and pattern in the matrix.
## corrplot 0.95 loaded
M = cor(mtcars)
corrplot(M, method = 'number') # colorful number

corrplot(M, method = 'color', order = 'alphabet')

corrplot(M) # by default, method = 'circle'

corrplot(M, order = 'AOE') # after 'AOE' reorder

corrplot(M, method = 'shade', order = 'AOE', diag = FALSE)

corrplot(M, method = 'square', order = 'FPC', type = 'lower', diag = FALSE)

corrplot(M, method = 'ellipse', order = 'AOE', type = 'upper')

corrplot.mixed(M, order = 'AOE')

corrplot.mixed(M, lower = 'shade', upper = 'pie', order = 'hclust')

Reorder a correlation matrix
The details of four order
algorithms, named
'AOE'
, 'FPC'
, 'hclust'
,
'alphabet'
are as following.
'AOE'
is for the angular order of the eigenvectors.
It is calculated from the order of the angles \(a_i\),
\[
a_i =
\begin{cases}
\arctan (e_{i2}/e_{i1}), & \text{if $e_{i1}>0$;}
\newline
\arctan (e_{i2}/e_{i1}) + \pi, & \text{otherwise.}
\end{cases}
\]
where \(e_1\) and \(e_2\) are the largest two eigenvalues of
the correlation matrix. See Michael Friendly
(2002) for details.
'FPC'
for the first principal component
order.
'hclust'
for hierarchical clustering order, and
'hclust.method'
for the agglomeration method to be used.
'hclust.method'
should be one of 'ward'
,
'ward.D'
, 'ward.D2'
, 'single'
,
'complete'
, 'average'
,
'mcquitty'
, 'median'
or
'centroid'
.
'alphabet'
for alphabetical order.
You can also reorder the matrix ‘manually’ via function
corrMatOrder()
.
If using 'hclust'
, corrplot()
can draw
rectangles around the plot of correlation matrix based on the results of
hierarchical clustering.
corrplot(M, order = 'hclust', addrect = 2)

corrplot(M, method = 'square', diag = FALSE, order = 'hclust',
addrect = 3, rect.col = 'blue', rect.lwd = 3, tl.pos = 'd')

R package seriation provides the infrastructure for
ordering objects with an implementation of several
seriation/sequencing/ordination techniques to reorder matrices,
dissimilarity matrices, and dendrograms. For more information, see in
section References.
We can reorder the matrix via seriation package and
then corrplot it. Here are some examples.
library(seriation)
list_seriation_methods('matrix')
## [1] "AOE" "BEA" "BEA_TSP" "CA" "Heatmap" "Identity"
## [7] "LLE" "Mean" "PCA" "PCA_angle" "Random" "Reverse"
list_seriation_methods('dist')
## [1] "ARSA" "BBURCG" "BBWRCG" "Enumerate"
## [5] "GSA" "GW" "GW_average" "GW_complete"
## [9] "GW_single" "GW_ward" "HC" "HC_average"
## [13] "HC_complete" "HC_single" "HC_ward" "Identity"
## [17] "MDS" "MDS_angle" "OLO" "OLO_average"
## [21] "OLO_complete" "OLO_single" "OLO_ward" "QAP_2SUM"
## [25] "QAP_BAR" "QAP_Inertia" "QAP_LS" "R2E"
## [29] "Random" "Reverse" "SGD" "SPIN_NH"
## [33] "SPIN_STS" "Sammon_mapping" "Spectral" "Spectral_norm"
## [37] "TSP" "VAT" "isoMDS" "isomap"
## [41] "metaMDS" "monoMDS"
data(Zoo)
Z = cor(Zoo[, -c(15, 17)])
dist2order = function(corr, method, ...) {
d_corr = as.dist(1 - corr)
s = seriate(d_corr, method = method, ...)
i = get_order(s)
return(i)
}
Methods 'PCA_angle'
and 'HC'
in
seriation, are same as 'AOE'
and
'hclust'
separately in corrplot()
and
corrMatOrder()
.
Here are some plots after seriation.
# Fast Optimal Leaf Ordering for Hierarchical Clustering
i = dist2order(Z, 'OLO')
corrplot(Z[i, i], cl.pos = 'n')

# Quadratic Assignment Problem
i = dist2order(Z, 'QAP_2SUM')
corrplot(Z[i, i], cl.pos = 'n')

# Multidimensional Scaling
i = dist2order(Z, 'MDS_nonmetric')
## Warning in get_seriation_method("dist", method): seriation method
## 'MDS_nonmetric' is now deprecated and will be removed in future releases. Using
## `isoMDS`
corrplot(Z[i, i], cl.pos = 'n')

# Simulated annealing
i = dist2order(Z, 'ARSA')
corrplot(Z[i, i], cl.pos = 'n')

# TSP solver
i = dist2order(Z, 'TSP')
corrplot(Z[i, i], cl.pos = 'n')

# Spectral seriation
i = dist2order(Z, 'Spectral')
corrplot(Z[i, i], cl.pos = 'n')

corrRect()
can add rectangles on the plot with three
ways(parameter index
, name
and
namesMat
) after corrplot()
. We can use pipe
operator *>%
in package magrittr
with more
convenience. Since R 4.1.0, |>
is supported without
extra package.
library(magrittr)
# Rank-two ellipse seriation, use index parameter
i = dist2order(Z, 'R2E')
corrplot(Z[i, i], cl.pos = 'n') %>% corrRect(c(1, 9, 15))

# use name parameter
# Since R 4.1.0, the following one line code works:
# corrplot(M, order = 'AOE') |> corrRect(name = c('gear', 'wt', 'carb'))
corrplot(Z, order = 'AOE') %>%
corrRect(name = c('tail', 'airborne', 'venomous', 'predator'))

# use namesMat parameter
r = rbind(c('eggs', 'catsize', 'airborne', 'milk'),
c('catsize', 'eggs', 'milk', 'airborne'))
corrplot(Z, order = 'hclust') %>% corrRect(namesMat = r)

Change color spectra, color-legend and text-legend
We can get sequential and diverging colors from COL1()
and COL2()
. The color palettes are borrowed from
RColorBrewer
package.
Notice: the middle color getting from
COL2()
is fixed to '#FFFFFF'
(white), thus we
can visualizing element 0 with white color.
COL1()
: Get sequential colors, suitable for visualize a
non-negative or non-positive matrix (e.g. matrix in [0, 20], or [-100,
-10], or [100, 500]).
COL2()
: Get diverging colors, suitable for visualize a
matrix which elements are partly positive and partly negative
(e.g. correlation matrix in [-1, 1], or [-20, 100]).
The colors of the correlation plots can be customized by
col
in corrplot()
. They are distributed
uniformly in col.lim
interval.
col
: vector, the colors of glyphs. They are distributed
uniformly in col.lim
interval. By default,
- If
is.corr
is TRUE
, col
will
be COL2('RdBu', 200)
.
- If
is.corr
is FALSE
,
- and
corr
is a non-negative or non-positive matrix,
col
will be COL1('YlOrBr', 200)
;
- otherwise (elements are partly positive and partly negative),
col
will be COL2('RdBu', 200)
.
col.lim
: the limits (x1, x2) interval for assigning
color by col
. By default,
col.lim
will be c(-1, 1)
when
is.corr
is TRUE
,
col.lim
will be c(min(corr), max(corr))
when is.corr
is FALSE
.
- NOTICE: if you set
col.lim
when
is.corr
is TRUE
, the assigning colors are
still distributed uniformly in [-1, 1], it only affect the display on
color-legend.
is.corr
: logical, whether the input matrix is a
correlation matrix or not. The default value is TRUE
. We
can visualize a non-correlation matrix by setting
is.corr = FALSE
.
Here all diverging colors from COL2()
and sequential
colors from COL1()
are shown below.
Diverging colors:

Sequential colors:

Usage of COL1()
and COL2()
:
COL1(sequential = c("Oranges", "Purples", "Reds", "Blues", "Greens",
"Greys", "OrRd", "YlOrRd", "YlOrBr", "YlGn"), n = 200)
COL2(diverging = c("RdBu", "BrBG", "PiYG", "PRGn", "PuOr", "RdYlBu"), n = 200)
In addition, function colorRampPalette()
is very
convenient for generating color spectrum.
Parameters group cl.*
is for color-legend. The
common-using are:
cl.pos
is for the position of color labels. It is
character or logical. If character, it must be one of 'r'
(means right, default if type='upper'
or
'full'
), 'b'
(means bottom, default if
type='lower'
) or 'n'
(means don’t draw
color-label).
cl.ratio
is to justify the width of color-legend,
0.1~0.2 is suggested.
Parameters group tl.*
is for text-legend. The
common-using are:
tl.pos
is for the position of text labels. It is
character or logical. If character, it must be one of 'lt'
,
'ld'
, 'td'
, 'd'
, 'l'
or 'n'
. 'lt'
(default if
type='full'
) means left and top, 'ld'
(default
if type='lower'
) means left and diagonal,
'td'
(default if type='upper'
) means top and
diagonal(near), 'd'
means diagonal, 'l'
means
left, 'n'
means don’t add text-label.
tl.cex
is for the size of text label (variable
names).
tl.srt
is for text label string rotation in
degrees.
corrplot(M, order = 'AOE', col = COL2('RdBu', 10))

corrplot(M, order = 'AOE', addCoef.col = 'black', tl.pos = 'd',
cl.pos = 'n', col = COL2('PiYG'))

corrplot(M, method = 'square', order = 'AOE', addCoef.col = 'black', tl.pos = 'd',
cl.pos = 'n', col = COL2('BrBG'))

## bottom color legend, diagonal text legend, rotate text label
corrplot(M, order = 'AOE', cl.pos = 'b', tl.pos = 'd',
col = COL2('PRGn'), diag = FALSE)

## text labels rotated 45 degrees and wider color legend with numbers right aligned
corrplot(M, type = 'lower', order = 'hclust', tl.col = 'black',
cl.ratio = 0.2, tl.srt = 45, col = COL2('PuOr', 10))

## remove color legend, text legend and principal diagonal glyph
corrplot(M, order = 'AOE', cl.pos = 'n', tl.pos = 'n',
col = c('white', 'black'), bg = 'gold2')

Visualize non-correlation matrix, NA value and math label
We can visualize a non-correlation matrix by set
is.corr=FALSE
, and assign colors by col.lim
.
If the matrix have both positive and negative values, the matrix
transformation keep every values positiveness and negativeness.
If your matrix is rectangular, you can adjust the aspect ratio with
the win.asp
parameter to make the matrix rendered as a
square.
## matrix in [20, 26], grid.col
N1 = matrix(runif(80, 20, 26), 8)
corrplot(N1, is.corr = FALSE, col.lim = c(20, 30), method = 'color', tl.pos = 'n',
col = COL1('YlGn'), cl.pos = 'b', addgrid.col = 'white', addCoef.col = 'grey50')

## matrix in [-15, 10]
N2 = matrix(runif(80, -15, 10), 8)
## using sequential colors, transKeepSign = FALSE
corrplot(N2, is.corr = FALSE, transKeepSign = FALSE, method = 'color', col.lim = c(-15, 10),
tl.pos = 'n', col = COL1('YlGn'), cl.pos = 'b', addCoef.col = 'grey50')

## using diverging colors, transKeepSign = TRUE (default)
corrplot(N2, is.corr = FALSE, col.lim = c(-15, 10),
tl.pos = 'n', col = COL2('PiYG'), cl.pos = 'b', addCoef.col = 'grey50')

## using diverging colors
corrplot(N2, is.corr = FALSE, method = 'color', col.lim = c(-15, 10), tl.pos = 'n',
col = COL2('PiYG'), cl.pos = 'b', addCoef.col = 'grey50')

Notice: when is.corr
is TRUE
,
col.lim
only affect the color legend If you change it, the
color on correlation matrix plot is still assigned on
c(-1, 1)
# when is.corr=TRUE, col.lim only affect the color legend display
corrplot(M/2)

corrplot(M/2, col.lim=c(-0.5, 0.5))

By default, corrplot renders NA values as
'?'
characters. Using na.label
parameter, it
is possible to use a different value (max. two characters are
supported).
Since version 0.78
, it is possible to use plotmath
expression in variable names. To activate plotmath rendering, prefix
your label with '$'
.
M2 = M
diag(M2) = NA
colnames(M2) = rep(c('$alpha+beta', '$alpha[0]', '$alpha[beta]'),
c(4, 4, 3))
rownames(M2) = rep(c('$Sigma[i]^n', '$sigma', '$alpha[0]^100', '$alpha[beta]'),
c(2, 4, 2, 3))
corrplot(10*abs(M2), is.corr = FALSE, col.lim = c(0, 10), tl.cex = 1.5)

Visualize p-value and confidence interval
corrplot()
can also visualize p-value and confidence
interval on the correlation matrix plot. Here are some important
parameters.
About p-value:
p.mat
is the p-value matrix, if NULL
,
parameter sig.level
, insig, pch
,
pch.col
, pch.cex
are invalid.
sig.level
is significant level, with default value
0.05. If the p-value in p-mat
is bigger than
sig.level
, then the corresponding correlation coefficient
is regarded as insignificant. If insig
is
'label_sig'
, sig.level
can be an increasing
vector of significance levels, in which case pch
will be
used once for the highest p-value interval and multiple times
(e.g. '*'
, '**'
, '***'
) for each
lower p-value interval.
insig
Character, specialized insignificant correlation
coefficients, 'pch'
(default), 'p-value'
,
'blank',
'n'
, or 'label_sig'
. If
'blank'
, wipe away the corresponding glyphs; if
'p-value'
, add p-values the corresponding glyphs; if
'pch'
, add characters (see pch for details) on
corresponding glyphs; if 'n'
, don’t take any measures; if
'label_sig'
, mark significant correlations with
pch
(see sig.level
).
pch
is for adding character on the glyphs of
insignificant correlation coefficients (only valid when insig is
'pch'
). See ?par
.
About confidence interval:
plotCI
is character for the method of plotting
confidence interval. If 'n'
, don’t plot confidence
interval. If 'rect'
, plot rectangles whose upper side means
upper bound and lower side means lower bound respectively.
lowCI.mat
is the matrix of the lower bound of
confidence interval.
uppCI.mat
is the Matrix of the upper bound of
confidence interval.
We can get p-value matrix and confidence intervals matrix by
cor.mtest()
which returns a list containing:
p
is the p-values matrix.
lowCI
is the lower bound of confidence interval
matrix.
uppCI
is the lower bound of confidence interval
matrix.
testRes = cor.mtest(mtcars, conf.level = 0.95)
## specialized the insignificant value according to the significant level
corrplot(M, p.mat = testRes$p, sig.level = 0.10, order = 'hclust', addrect = 2)

## leave blank on non-significant coefficient
## add significant correlation coefficients
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig='blank',
addCoef.col ='black', number.cex = 0.8, order = 'AOE', diag=FALSE)

## leave blank on non-significant coefficient
## add all correlation coefficients
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig='blank',
order = 'AOE', diag = FALSE)$corrPos -> p1
text(p1$x, p1$y, round(p1$corr, 2))

## add p-values on no significant coefficients
corrplot(M, p.mat = testRes$p, insig = 'p-value')

## add all p-values
corrplot(M, p.mat = testRes$p, insig = 'p-value', sig.level = -1)

## add significant level stars
corrplot(M, p.mat = testRes$p, method = 'color', diag = FALSE, type = 'upper',
sig.level = c(0.001, 0.01, 0.05), pch.cex = 0.9,
insig = 'label_sig', pch.col = 'grey20', order = 'AOE')

## add significant level stars and cluster rectangles
corrplot(M, p.mat = testRes$p, tl.pos = 'd', order = 'hclust', addrect = 2,
insig = 'label_sig', sig.level = c(0.001, 0.01, 0.05),
pch.cex = 0.9, pch.col = 'grey20')

Visualize confidence interval.
# Visualize confidence interval
corrplot(M, lowCI = testRes$lowCI, uppCI = testRes$uppCI, order = 'hclust',
tl.pos = 'd', rect.col = 'navy', plotC = 'rect', cl.pos = 'n')

# Visualize confidence interval and cross the significant coefficients
corrplot(M, p.mat = testRes$p, lowCI = testRes$lowCI, uppCI = testRes$uppCI,
addrect = 3, rect.col = 'navy', plotC = 'rect', cl.pos = 'n')

References
Michael Friendly (2002). Corrgrams: Exploratory displays for
correlation matrices. The American Statistician, 56, 316–324.
D.J. Murdoch, E.D. Chow (1996). A graphical display of large
correlation matrices. The American Statistician, 50, 178–180.
Michael Hahsler, Christian Buchta and Kurt Hornik (2020).
seriation: Infrastructure for Ordering Objects Using Seriation. R
package version 1.2-9. https://CRAN.R-project.org/package=seriation
Hahsler M, Hornik K, Buchta C (2008). “Getting things in order:
An introduction to the R package seriation.” Journal of Statistical
Software, 25(3), 1-34. ISSN 1548-7660, doi:
10.18637/jss.v025.i03 (URL: https://doi.org/10.18637/jss.v025.i03), <URL: https://www.jstatsoft.org/v25/i03/>.
corrplot/inst/doc/corrplot-intro.Rmd 0000644 0001762 0000144 00000047346 14703115404 017316 0 ustar ligges users ---
title: 'An Introduction to corrplot Package'
author: 'Taiyun Wei, Viliam Simko'
date: '`r Sys.Date()`'
output:
prettydoc::html_pretty:
theme: cayman
toc: true
toc-title: 'Table of Contents'
vignette: >
%\VignetteIndexEntry{An Introduction to corrplot Package}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
fig.align = 'center',
fig.path = 'webimg/',
fig.width = 7,
fig.height = 7,
out.width = '600px',
dev = 'png')
get_os = function() {
sysinf = Sys.info()
if (!is.null(sysinf)) {
os = sysinf['sysname']
if (os == 'Darwin')
os = 'osx'
} else { ## mystery machine
os = .Platform$OS.type
if (grepl('^darwin', R.version$os))
os = 'osx'
if (grepl('linux-gnu', R.version$os))
os = 'linux'
}
tolower(os)
}
if(get_os() =='windows' & capabilities('cairo') | all(capabilities(c('cairo', 'X11')))) {
knitr::opts_chunk$set(dev.args = list(type='cairo'))
}
```
## Introduction
R package **corrplot** provides a visual exploratory tool on correlation matrix that
supports automatic variable reordering to help detect hidden patterns among variables.
corrplot is very easy to use and provides a rich array of plotting options in
visualization method, graphic layout, color, legend, text labels, etc.
It also provides p-values and confidence intervals to help users determine the
statistical significance of the correlations.
`corrplot()` has about 50 parameters, however the mostly common ones are only a few.
We can get a correlation matrix plot with only one line of code in most scenes.
The mostly using parameters include `method`, `type`, `order`, `diag`, and etc.
There are seven visualization methods (parameter `method`) in
corrplot package, named `'circle'`, `'square'`, `'ellipse'`,
`'number'`, `'shade'`, `'color'`, `'pie'`. Color intensity of the glyph
is proportional to the correlation coefficients by default color setting.
- `'circle'` and `'square'`, the **areas** of circles or squares show the
absolute value of corresponding correlation coefficients.
- `'ellipse'`, the ellipses have their eccentricity parametrically scaled to the correlation value.
It comes from D.J. Murdoch and E.D. Chow's job, see in section References.
- `'number'`, coefficients numbers with different color.
- `'color'`, square of equal size with different color.
- `'shade'`, similar to `'color'`, but the negative coefficients glyphs are shaded.
Method `'pie'` and `'shade'` come from Michael Friendly's job.
- `'pie'`, the circles are filled clockwise for positive values, anti-clockwise for negative
values.
`corrplot.mixed()` is a wrapped function for mixed visualization style,
which can set the visual methods of lower and upper triangular
separately.
There are three layout types (parameter `type`): `'full'`, `'upper'` and
`'lower'`.
The correlation matrix can be reordered according to the correlation
matrix coefficients. This is important to identify the hidden structure
and pattern in the matrix.
```{r intro}
library(corrplot)
M = cor(mtcars)
corrplot(M, method = 'number') # colorful number
corrplot(M, method = 'color', order = 'alphabet')
corrplot(M) # by default, method = 'circle'
corrplot(M, order = 'AOE') # after 'AOE' reorder
corrplot(M, method = 'shade', order = 'AOE', diag = FALSE)
corrplot(M, method = 'square', order = 'FPC', type = 'lower', diag = FALSE)
corrplot(M, method = 'ellipse', order = 'AOE', type = 'upper')
corrplot.mixed(M, order = 'AOE')
corrplot.mixed(M, lower = 'shade', upper = 'pie', order = 'hclust')
```
## Reorder a correlation matrix
The details of four `order` algorithms, named `'AOE'`, `'FPC'`,
`'hclust'`, `'alphabet'` are as following.
- `'AOE'` is for the angular order of the eigenvectors. It is
calculated from the order of the angles $a_i$,
$$
a_i =
\begin{cases}
\arctan (e_{i2}/e_{i1}), & \text{if $e_{i1}>0$;}
\newline
\arctan (e_{i2}/e_{i1}) + \pi, & \text{otherwise.}
\end{cases}
$$
where $e_1$ and $e_2$ are the largest two eigenvalues of the
correlation matrix. See [Michael Friendly
(2002)](http://www.datavis.ca/papers/corrgram.pdf) for details.
- `'FPC'` for the first principal component order.
- `'hclust'` for hierarchical clustering order, and `'hclust.method'`
for the agglomeration method to be used. `'hclust.method'` should be
one of `'ward'`, `'ward.D'`, `'ward.D2'`, `'single'`, `'complete'`,
`'average'`, `'mcquitty'`, `'median'` or `'centroid'`.
- `'alphabet'` for alphabetical order.
You can also reorder the matrix 'manually' via function
`corrMatOrder()`.
If using `'hclust'`, `corrplot()` can draw rectangles around the plot of
correlation matrix based on the results of hierarchical clustering.
```{r hclust}
corrplot(M, order = 'hclust', addrect = 2)
corrplot(M, method = 'square', diag = FALSE, order = 'hclust',
addrect = 3, rect.col = 'blue', rect.lwd = 3, tl.pos = 'd')
```
R package **seriation** provides the infrastructure for ordering objects with an
implementation of several seriation/sequencing/ordination techniques to reorder
matrices, dissimilarity matrices, and dendrograms. For more information,
see in section References.
We can reorder the matrix via **seriation** package and then corrplot it.
Here are some examples.
```{r seriation}
library(seriation)
list_seriation_methods('matrix')
list_seriation_methods('dist')
data(Zoo)
Z = cor(Zoo[, -c(15, 17)])
dist2order = function(corr, method, ...) {
d_corr = as.dist(1 - corr)
s = seriate(d_corr, method = method, ...)
i = get_order(s)
return(i)
}
```
Methods `'PCA_angle'` and `'HC'` in **seriation**, are same as `'AOE'` and `'hclust'`
separately in `corrplot()` and `corrMatOrder()`.
Here are some plots after seriation.
```{r seriation-plot}
# Fast Optimal Leaf Ordering for Hierarchical Clustering
i = dist2order(Z, 'OLO')
corrplot(Z[i, i], cl.pos = 'n')
# Quadratic Assignment Problem
i = dist2order(Z, 'QAP_2SUM')
corrplot(Z[i, i], cl.pos = 'n')
# Multidimensional Scaling
i = dist2order(Z, 'MDS_nonmetric')
corrplot(Z[i, i], cl.pos = 'n')
# Simulated annealing
i = dist2order(Z, 'ARSA')
corrplot(Z[i, i], cl.pos = 'n')
# TSP solver
i = dist2order(Z, 'TSP')
corrplot(Z[i, i], cl.pos = 'n')
# Spectral seriation
i = dist2order(Z, 'Spectral')
corrplot(Z[i, i], cl.pos = 'n')
```
`corrRect()` can add rectangles on the plot with three ways(parameter
`index`, `name` and `namesMat`) after `corrplot()`.
We can use pipe operator `*>%` in package `magrittr` with more convenience.
Since R 4.1.0, `|>` is supported without extra package.
```{r rectangles}
library(magrittr)
# Rank-two ellipse seriation, use index parameter
i = dist2order(Z, 'R2E')
corrplot(Z[i, i], cl.pos = 'n') %>% corrRect(c(1, 9, 15))
# use name parameter
# Since R 4.1.0, the following one line code works:
# corrplot(M, order = 'AOE') |> corrRect(name = c('gear', 'wt', 'carb'))
corrplot(Z, order = 'AOE') %>%
corrRect(name = c('tail', 'airborne', 'venomous', 'predator'))
# use namesMat parameter
r = rbind(c('eggs', 'catsize', 'airborne', 'milk'),
c('catsize', 'eggs', 'milk', 'airborne'))
corrplot(Z, order = 'hclust') %>% corrRect(namesMat = r)
```
## Change color spectra, color-legend and text-legend
We can get sequential and diverging colors from `COL1()` and `COL2()`.
The color palettes are borrowed from `RColorBrewer` package.
**Notice**: the middle color getting from `COL2()` is fixed to `'#FFFFFF'`(white),
thus we can visualizing element 0 with white color.
- `COL1()`: Get sequential colors, suitable for visualize a non-negative or
non-positive matrix (e.g. matrix in [0, 20], or [-100, -10], or [100, 500]).
- `COL2()`: Get diverging colors, suitable for visualize a matrix which elements
are partly positive and partly negative (e.g. correlation matrix in [-1, 1], or [-20, 100]).
The colors of the correlation plots can be customized by `col` in `corrplot()`.
They are distributed uniformly in `col.lim` interval.
- `col`: vector, the colors of glyphs. They are distributed uniformly in `col.lim` interval. By default,
- If `is.corr` is `TRUE`, `col` will be `COL2('RdBu', 200)`.
- If `is.corr` is `FALSE`,
- and `corr` is a non-negative or non-positive matrix, `col` will be `COL1('YlOrBr', 200)`;
- otherwise (elements are partly positive and partly negative), `col` will be `COL2('RdBu', 200)`.
- `col.lim`: the limits (x1, x2) interval for assigning color by `col`. By default,
- `col.lim` will be `c(-1, 1)` when `is.corr` is `TRUE`,
- `col.lim` will be `c(min(corr), max(corr))` when `is.corr` is `FALSE`.
- **NOTICE**: if you set `col.lim` when `is.corr` is `TRUE`, the assigning colors are still
distributed uniformly in [-1, 1], it only affect the display on color-legend.
- `is.corr`: logical, whether the input matrix is a correlation matrix or not. The default value is `TRUE`.
We can visualize a non-correlation matrix by setting `is.corr = FALSE`.
Here all diverging colors from `COL2()` and sequential colors from `COL1()` are shown below.
**Diverging colors**:
```{r echo=FALSE, fig.width = 8, fig.height = 6, out.width = '700px'}
## diverging colors
plot.new()
par(mar = c(0, 0, 0, 0) + 0.1)
plot.window(xlim = c(-0.2, 1.1), ylim = c(0, 1), xaxs = 'i', yaxs = 'i')
col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu')
for(i in 1:length(col)) {
colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8)
}
```
**Sequential colors**:
```{r echo=FALSE, fig.width = 8, fig.height = 6, out.width = '700px'}
## sequential colors
plot.new()
par(mar = c(0, 0, 0, 0) + 0.1)
plot.window(xlim = c(-0.2, 1.1), ylim = c(0, 1), xaxs = 'i', yaxs = 'i')
col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd',
'YlOrRd', 'YlOrBr', 'YlGn')
for(i in 1:length(col)) {
colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2)
}
```
Usage of `COL1()` and `COL2()`:
```{r eval=FALSE}
COL1(sequential = c("Oranges", "Purples", "Reds", "Blues", "Greens",
"Greys", "OrRd", "YlOrRd", "YlOrBr", "YlGn"), n = 200)
COL2(diverging = c("RdBu", "BrBG", "PiYG", "PRGn", "PuOr", "RdYlBu"), n = 200)
```
In addition, function `colorRampPalette()` is very convenient for generating color spectrum.
Parameters group `cl.*` is for color-legend. The common-using are:
- `cl.pos` is for the position of color labels. It is character or
logical. If character, it must be one of `'r'` (means right, default
if `type='upper'` or `'full'`), `'b'` (means bottom, default if
`type='lower'`) or `'n'`(means don't draw color-label).
- `cl.ratio` is to justify the width of color-legend, 0.1\~0.2 is
suggested.
Parameters group `tl.*` is for text-legend. The common-using are:
- `tl.pos` is for the position of text labels. It is character or
logical. If character, it must be one of `'lt'`, `'ld'`, `'td'`,
`'d'`, `'l'` or `'n'`. `'lt'`(default if `type='full'`) means left and top,
`'ld'`(default if `type='lower'`) means left and diagonal,
`'td'`(default if `type='upper'`) means top and diagonal(near),
`'d'` means diagonal, `'l'` means left, `'n'` means don't add text-label.
- `tl.cex` is for the size of text label (variable names).
- `tl.srt` is for text label string rotation in degrees.
```{r color}
corrplot(M, order = 'AOE', col = COL2('RdBu', 10))
corrplot(M, order = 'AOE', addCoef.col = 'black', tl.pos = 'd',
cl.pos = 'n', col = COL2('PiYG'))
corrplot(M, method = 'square', order = 'AOE', addCoef.col = 'black', tl.pos = 'd',
cl.pos = 'n', col = COL2('BrBG'))
## bottom color legend, diagonal text legend, rotate text label
corrplot(M, order = 'AOE', cl.pos = 'b', tl.pos = 'd',
col = COL2('PRGn'), diag = FALSE)
## text labels rotated 45 degrees and wider color legend with numbers right aligned
corrplot(M, type = 'lower', order = 'hclust', tl.col = 'black',
cl.ratio = 0.2, tl.srt = 45, col = COL2('PuOr', 10))
## remove color legend, text legend and principal diagonal glyph
corrplot(M, order = 'AOE', cl.pos = 'n', tl.pos = 'n',
col = c('white', 'black'), bg = 'gold2')
```
## Visualize non-correlation matrix, NA value and math label
We can visualize a non-correlation matrix by set `is.corr=FALSE`, and
assign colors by `col.lim`. If the matrix have both positive and
negative values, the matrix transformation keep every values
positiveness and negativeness.
If your matrix is rectangular, you can adjust the aspect ratio with the
`win.asp` parameter to make the matrix rendered as a square.
```{r non-corr}
## matrix in [20, 26], grid.col
N1 = matrix(runif(80, 20, 26), 8)
corrplot(N1, is.corr = FALSE, col.lim = c(20, 30), method = 'color', tl.pos = 'n',
col = COL1('YlGn'), cl.pos = 'b', addgrid.col = 'white', addCoef.col = 'grey50')
## matrix in [-15, 10]
N2 = matrix(runif(80, -15, 10), 8)
## using sequential colors, transKeepSign = FALSE
corrplot(N2, is.corr = FALSE, transKeepSign = FALSE, method = 'color', col.lim = c(-15, 10),
tl.pos = 'n', col = COL1('YlGn'), cl.pos = 'b', addCoef.col = 'grey50')
## using diverging colors, transKeepSign = TRUE (default)
corrplot(N2, is.corr = FALSE, col.lim = c(-15, 10),
tl.pos = 'n', col = COL2('PiYG'), cl.pos = 'b', addCoef.col = 'grey50')
## using diverging colors
corrplot(N2, is.corr = FALSE, method = 'color', col.lim = c(-15, 10), tl.pos = 'n',
col = COL2('PiYG'), cl.pos = 'b', addCoef.col = 'grey50')
```
Notice: when `is.corr` is `TRUE`, `col.lim` only affect the color legend If
you change it, the color on correlation matrix plot is still assigned on
`c(-1, 1)`
```{r col-lim}
# when is.corr=TRUE, col.lim only affect the color legend display
corrplot(M/2)
corrplot(M/2, col.lim=c(-0.5, 0.5))
```
By default, **corrplot** renders NA values as `'?'` characters. Using
`na.label` parameter, it is possible to use a different value (max. two
characters are supported).
Since version `0.78`, it is possible to use
[plotmath](https://www.rdocumentation.org/packages/grDevices/topics/plotmath)
expression in variable names. To activate plotmath rendering, prefix
your label with `'$'`.
```{r NA-math}
M2 = M
diag(M2) = NA
colnames(M2) = rep(c('$alpha+beta', '$alpha[0]', '$alpha[beta]'),
c(4, 4, 3))
rownames(M2) = rep(c('$Sigma[i]^n', '$sigma', '$alpha[0]^100', '$alpha[beta]'),
c(2, 4, 2, 3))
corrplot(10*abs(M2), is.corr = FALSE, col.lim = c(0, 10), tl.cex = 1.5)
```
## Visualize p-value and confidence interval
`corrplot()` can also visualize p-value and confidence interval on the
correlation matrix plot. Here are some important parameters.
About p-value:
- `p.mat` is the p-value matrix, if `NULL`, parameter `sig.level`,
`insig, pch`, `pch.col`, `pch.cex` are invalid.
- `sig.level` is significant level, with default value 0.05. If the
p-value in `p-mat` is bigger than `sig.level`, then the
corresponding correlation coefficient is regarded as insignificant.
If `insig` is `'label_sig'`, `sig.level` can be an increasing vector
of significance levels, in which case `pch` will be used once for
the highest p-value interval and multiple times (e.g. `'*'`, `'**'`,
`'***'`) for each lower p-value interval.
- `insig` Character, specialized insignificant correlation
coefficients, `'pch'` (default), `'p-value'`, `'blank',` `'n'`, or
`'label_sig'`. If `'blank'`, wipe away the corresponding glyphs; if
`'p-value'`, add p-values the corresponding glyphs; if `'pch'`, add
characters (see pch for details) on corresponding glyphs; if `'n'`,
don't take any measures; if `'label_sig'`, mark significant
correlations with `pch` (see `sig.level`).
- `pch` is for adding character on the glyphs of insignificant
correlation coefficients (only valid when insig is `'pch'`). See
`?par` .
About confidence interval:
- `plotCI` is character for the method of plotting confidence
interval. If `'n'`, don't plot confidence interval. If `'rect'`,
plot rectangles whose upper side means upper bound and lower side
means lower bound respectively.
- `lowCI.mat` is the matrix of the lower bound of confidence interval.
- `uppCI.mat` is the Matrix of the upper bound of confidence interval.
We can get p-value matrix and confidence intervals matrix by
`cor.mtest()` which returns a list containing:
- `p` is the p-values matrix.
- `lowCI` is the lower bound of confidence interval matrix.
- `uppCI` is the lower bound of confidence interval matrix.
```{r test}
testRes = cor.mtest(mtcars, conf.level = 0.95)
## specialized the insignificant value according to the significant level
corrplot(M, p.mat = testRes$p, sig.level = 0.10, order = 'hclust', addrect = 2)
## leave blank on non-significant coefficient
## add significant correlation coefficients
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig='blank',
addCoef.col ='black', number.cex = 0.8, order = 'AOE', diag=FALSE)
```
```{r special}
## leave blank on non-significant coefficient
## add all correlation coefficients
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig='blank',
order = 'AOE', diag = FALSE)$corrPos -> p1
text(p1$x, p1$y, round(p1$corr, 2))
```
```{r p-values}
## add p-values on no significant coefficients
corrplot(M, p.mat = testRes$p, insig = 'p-value')
## add all p-values
corrplot(M, p.mat = testRes$p, insig = 'p-value', sig.level = -1)
## add significant level stars
corrplot(M, p.mat = testRes$p, method = 'color', diag = FALSE, type = 'upper',
sig.level = c(0.001, 0.01, 0.05), pch.cex = 0.9,
insig = 'label_sig', pch.col = 'grey20', order = 'AOE')
## add significant level stars and cluster rectangles
corrplot(M, p.mat = testRes$p, tl.pos = 'd', order = 'hclust', addrect = 2,
insig = 'label_sig', sig.level = c(0.001, 0.01, 0.05),
pch.cex = 0.9, pch.col = 'grey20')
```
Visualize confidence interval.
```{r confidence-interval}
# Visualize confidence interval
corrplot(M, lowCI = testRes$lowCI, uppCI = testRes$uppCI, order = 'hclust',
tl.pos = 'd', rect.col = 'navy', plotC = 'rect', cl.pos = 'n')
# Visualize confidence interval and cross the significant coefficients
corrplot(M, p.mat = testRes$p, lowCI = testRes$lowCI, uppCI = testRes$uppCI,
addrect = 3, rect.col = 'navy', plotC = 'rect', cl.pos = 'n')
```
## References
- Michael Friendly (2002). Corrgrams: Exploratory displays for correlation
matrices. The American Statistician, 56, 316--324.
- D.J. Murdoch, E.D. Chow (1996). A graphical display of large correlation
matrices. The American Statistician, 50, 178--180.
- Michael Hahsler, Christian Buchta and Kurt Hornik (2020). seriation: Infrastructure for Ordering
Objects Using Seriation. R package version 1.2-9. https://CRAN.R-project.org/package=seriation
- Hahsler M, Hornik K, Buchta C (2008). "Getting things in order: An introduction to the R package
seriation." _Journal of Statistical Software_, *25*(3), 1-34. ISSN 1548-7660, doi:
10.18637/jss.v025.i03 (URL: https://doi.org/10.18637/jss.v025.i03), .
corrplot/inst/NEWS 0000644 0001762 0000144 00000000000 14703115404 013557 0 ustar ligges users corrplot/README.md 0000644 0001762 0000144 00000004024 14703126567 013410 0 ustar ligges users [](https://github.com/taiyun/corrplot/actions)
[](https://app.codecov.io/github/taiyun/corrplot?branch=master)
[](https://cran.r-project.org/package=corrplot)
[](https://www.r-pkg.org/pkg/corrplot)
## Summary
R package **corrplot** provides a visual exploratory tool on correlation matrix that
supports automatic variable reordering to help detect hidden patterns among variables.
corrplot is very easy to use and provides a rich array of plotting options in
visualization method, graphic layout, color, legend, text labels, etc.
It also provides p-values and confidence intervals to help users determine the
statistical significance of the correlations.
For examples, see its
[online vignette](https://taiyun.github.io/corrplot/).
This package is licensed under the MIT license, and available on CRAN:
.
## Basic example
```r
library(corrplot)
M = cor(mtcars)
corrplot(M, order = 'hclust', addrect = 2)
```

## Download and Install
To download the release version of the package on CRAN, type the following at the R command line:
```r
install.packages('corrplot')
```
To download the development version of the package, type the following at the R command line:
```r
devtools::install_github('taiyun/corrplot', build_vignettes = TRUE)
```
## How to cite
To cite `corrplot` properly, call the R built-in command
`citation('corrplot')` as follows:
```r
citation('corrplot')
```
## Reporting bugs and other issues
If you encounter a clear bug, please file a minimal reproducible example on
[github](https://github.com/taiyun/corrplot/issues).
corrplot/build/ 0000755 0001762 0000144 00000000000 14703127132 013215 5 ustar ligges users corrplot/build/vignette.rds 0000644 0001762 0000144 00000000341 14703127132 015552 0 ustar ligges users mQ
@^uQ$_@]D:t]t+Iweݐn=y6FN
|BI(5iAjy.A(\RzdB6v\
_K 1K/CMϺ,sAq`O+yB^quϏ|E314>s0S#~J6â;/Ƌ:,8*w'K corrplot/man/ 0000755 0001762 0000144 00000000000 14703127123 012671 5 ustar ligges users corrplot/man/corrplot.Rd 0000644 0001762 0000144 00000053654 14703126567 015054 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/corrplot.R
\name{corrplot}
\alias{corrplot}
\title{A visualization of a correlation matrix.}
\usage{
corrplot(
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),
addgrid.col = NULL,
addCoef.col = NULL,
addCoefasPercent = FALSE,
order = c("original", "AOE", "FPC", "hclust", "alphabet"),
hclust.method = c("complete", "ward", "ward.D", "ward.D2", "single", "average",
"mcquitty", "median", "centroid"),
addrect = NULL,
rect.col = "black",
rect.lwd = 2,
tl.pos = NULL,
tl.cex = 1,
tl.col = "red",
tl.offset = 0.4,
tl.srt = 90,
cl.pos = NULL,
cl.length = NULL,
cl.cex = 0.8,
cl.ratio = 0.15,
cl.align.text = "c",
cl.offset = 0.5,
number.cex = 1,
number.font = 2,
number.digits = NULL,
addshade = c("negative", "positive", "all"),
shade.lwd = 1,
shade.col = "white",
transKeepSign = TRUE,
p.mat = NULL,
sig.level = 0.05,
insig = c("pch", "p-value", "blank", "n", "label_sig"),
pch = 4,
pch.col = "black",
pch.cex = 3,
plotCI = c("n", "square", "circle", "rect"),
lowCI.mat = NULL,
uppCI.mat = NULL,
na.label = "?",
na.label.col = "black",
win.asp = 1,
...
)
}
\arguments{
\item{corr}{The correlation matrix to visualize, must be square if
\code{order} is not \code{'original'}. For general matrix, please using
\code{is.corr = FALSE} to convert.}
\item{method}{Character, the visualization method of correlation matrix to be
used. Currently, it supports seven methods, named \code{'circle'}
(default), \code{'square'}, \code{'ellipse'}, \code{'number'},
\code{'pie'}, \code{'shade'} and \code{'color'}. See examples for details.
The areas of circles or squares show the absolute value of corresponding
correlation coefficients. Method \code{'pie'} and \code{'shade'} came from
Michael Friendly's job (with some adjustment about the shade added on), and
\code{'ellipse'} came from D.J. Murdoch and E.D. Chow's job, see in section
References.}
\item{type}{Character, \code{'full'} (default), \code{'upper'} or
\code{'lower'}, display full matrix, lower triangular or upper triangular
matrix.}
\item{col}{Vector, the colors of glyphs. They are distributed uniformly in
\code{col.lim} interval.
If \code{is.corr} is \code{TRUE}, the default value will be \code{COL2('RdBu', 200)}.
If \code{is.corr} is \code{FALSE} and \code{corr} is a non-negative or non-positive matrix,
the default value will be \code{COL1('YlOrBr', 200)};
otherwise (elements are partly positive and partly negative),
the default value will be \code{COL2('RdBu', 200)}.}
\item{col.lim}{The limits \code{(x1, x2)} interval for assigning color by
\code{col}. If \code{NULL},
\code{col.lim} will be \code{c(-1, 1)} when \code{is.corr} is \code{TRUE},
\code{col.lim} will be \code{c(min(corr), max(corr))} when \code{is.corr}
is \code{FALSE}
NOTICE: if you set \code{col.lim} when \code{is.corr} is \code{TRUE}, the assigning colors
are still distributed uniformly in [-1, 1], it only affect the display
on color-legend.}
\item{is.corr}{Logical, whether the input matrix is a correlation matrix or
not. We can visualize the non-correlation matrix by setting
\code{is.corr = FALSE}.}
\item{bg}{The background color.}
\item{title}{Character, title of the graph.}
\item{add}{Logical, if \code{TRUE}, the graph is added to an existing plot,
otherwise a new plot will be created.}
\item{diag}{Logical, whether display the correlation coefficients on the
principal diagonal.}
\item{outline}{Logical or character, whether plot outline of circles, square
and ellipse, or the color of these glyphs. For pie, this represents the
color of the circle outlining the pie. If \code{outline} is \code{TRUE},
the default value is \code{'black'}.}
\item{mar}{See \code{\link{par}}.}
\item{addgrid.col}{The color of the grid. If \code{NA}, don't add grid. If
\code{NULL} the default value is chosen. The default value depends on
\code{method}, if \code{method} is \code{color} or \code{shade}, the color
of the grid is \code{NA}, that is, not draw grid; otherwise \code{'grey'}.}
\item{addCoef.col}{Color of coefficients added on the graph. If \code{NULL}
(default), add no coefficients.}
\item{addCoefasPercent}{Logic, whether translate coefficients into percentage
style for spacesaving.}
\item{order}{Character, the ordering method of the correlation matrix.
\itemize{
\item{\code{'original'} for original order (default).}
\item{\code{'AOE'} for the angular order of the eigenvectors.}
\item{\code{'FPC'} for the first principal component order.}
\item{\code{'hclust'} for the hierarchical clustering order.}
\item{\code{'alphabet'} for alphabetical order.}
}
See function \code{\link{corrMatOrder}} for details.}
\item{hclust.method}{Character, the agglomeration method to be used when
\code{order} is \code{\link{hclust}}. This should be one of \code{'ward'},
\code{'ward.D'}, \code{'ward.D2'}, \code{'single'}, \code{'complete'},
\code{'average'}, \code{'mcquitty'}, \code{'median'} or \code{'centroid'}.}
\item{addrect}{Integer, the number of rectangles draws on the graph according
to the hierarchical cluster, only valid when \code{order} is \code{hclust}.
If \code{NULL} (default), then add no rectangles.}
\item{rect.col}{Color for rectangle border(s), only valid when \code{addrect}
is equal or greater than 1.}
\item{rect.lwd}{Numeric, line width for borders for rectangle border(s), only
valid when \code{addrect} is equal or greater than 1.}
\item{tl.pos}{Character or logical, position of text labels. If character, it
must be one of \code{'lt'}, \code{'ld'}, \code{'td'}, \code{'d'} or
\code{'n'}. \code{'lt'}(default if \code{type=='full'}) means left and top,
\code{'ld'}(default if \code{type=='lower'}) means left and diagonal,
\code{'td'}(default if \code{type=='upper'}) means top and diagonal(near),
\code{'l'} means left,
\code{'d'} means diagonal, \code{'n'} means don't add text-label.}
\item{tl.cex}{Numeric, for the size of text label (variable names).}
\item{tl.col}{The color of text label.}
\item{tl.offset}{Numeric, for text label, see \code{\link{text}}.}
\item{tl.srt}{Numeric, for text label string rotation in degrees, see
\code{\link{text}}.}
\item{cl.pos}{Character or logical, position of color-legend; If character,
it must be one of \code{'r'} (default if \code{type=='upper'} or
\code{'full'}), \code{'b'} (default if \code{type=='lower'}) or \code{'n'},
\code{'n'} means don't draw color-legend.}
\item{cl.length}{Integer, the number of number-text in color-legend, passed to
\code{\link{colorlegend}}. If \code{NULL}, \code{cl.length} is
\code{length(col) + 1} when \code{length(col) <=20}; \code{cl.length} is 11
when \code{length(col) > 20}}
\item{cl.cex}{Numeric, text size of number-label in color-legend, passed to
\code{\link{colorlegend}}.}
\item{cl.ratio}{Numeric, to justify the width of color-legend, 0.1~0.2 is
suggested.}
\item{cl.align.text}{Character, \code{'l'}, \code{'c'} (default) or
\code{'r'}, for number-label in color-legend, \code{'l'} means left,
\code{'c'} means center, and \code{'r'} means right.}
\item{cl.offset}{Numeric, for number-label in color-legend, see
\code{\link{text}}.}
\item{number.cex}{The \code{cex} parameter to send to the call to \code{text}
when writing the correlation coefficients into the plot.}
\item{number.font}{the \code{font} parameter to send to the call to
\code{text} when writing the correlation coefficients into the plot.}
\item{number.digits}{indicating the number of decimal digits to be
added into the plot. Non-negative integer or NULL, default NULL.}
\item{addshade}{Character for shade style, \code{'negative'},
\code{'positive'} or \code{'all'}, only valid when \code{method} is
\code{'shade'}. If \code{'all'}, all correlation coefficients' glyph will
be shaded; if \code{'positive'}, only the positive will be shaded; if
\code{'negative'}, only the negative will be shaded. Note: the angle of
shade line is different, 45 degrees for positive and 135 degrees for
negative.}
\item{shade.lwd}{Numeric, the line width of shade.}
\item{shade.col}{The color of shade line.}
\item{transKeepSign}{Logical, whether or not to keep matrix values' sign when
transforming non-corr matrix for plotting.
Only valid when \code{is.corr = FALSE}. The default value is \code{TRUE}.
NOTE: If \code{FALSE},the non-corr matrix will be}
\item{p.mat}{Matrix of p-value, if \code{NULL}, parameter \code{sig.level},
\code{insig}, \code{pch}, \code{pch.col}, \code{pch.cex} are invalid.}
\item{sig.level}{Significant level, if the p-value in \code{p-mat} is bigger
than \code{sig.level}, then the corresponding correlation coefficient is
regarded as insignificant. If \code{insig} is \code{'label_sig'}, this may
be an increasing vector of significance levels, in which case \code{pch}
will be used once for the highest p-value interval and multiple times
(e.g. '*', '**', '***') for each lower p-value interval.}
\item{insig}{Character, specialized insignificant correlation coefficients,
\code{'pch'} (default), \code{'p-value'}, \code{'blank'}, \code{'n'}, or
\code{'label_sig'}. If \code{'blank'}, wipe away the corresponding glyphs;
if \code{'p-value'}, add p-values the corresponding glyphs;
if \code{'pch'}, add characters (see \code{pch} for details) on
corresponding glyphs; if \code{'n'}, don't take any measures; if
\code{'label_sig'}, mark significant correlations with pch
(see \code{sig.level}).}
\item{pch}{Add character on the glyphs of insignificant correlation
coefficients(only valid when \code{insig} is \code{'pch'}). See
\code{\link{par}}.}
\item{pch.col}{The color of pch (only valid when \code{insig} is
\code{'pch'}).}
\item{pch.cex}{The cex of pch (only valid when \code{insig} is \code{'pch'}).}
\item{plotCI}{Character, method of ploting confidence interval. If
\code{'n'}, don't plot confidence interval. If 'rect', plot rectangles
whose upper side means upper bound and lower side means lower bound,
respectively. If 'circle', first plot a circle with the bigger absolute
bound, and then plot the smaller. Warning: if the two bounds are the same
sign, the smaller circle will be wiped away, thus forming a ring. Method
'square' is similar to 'circle'.}
\item{lowCI.mat}{Matrix of the lower bound of confidence interval.}
\item{uppCI.mat}{Matrix of the upper bound of confidence interval.}
\item{na.label}{Label to be used for rendering \code{NA} cells. Default is
\code{'?'}. If 'square', then the cell is rendered as a square with the
\code{na.label.col} color.}
\item{na.label.col}{Color used for rendering \code{NA} cells. Default is
\code{'black'}.}
\item{win.asp}{Aspect ration for the whole plot. Value other than 1 is
currently compatible only with methods 'circle' and 'square'.}
\item{\dots}{Additional arguments passing to function \code{text} for drawing
text label.}
}
\value{
(Invisibly) returns a \code{list(corr, corrTrans, arg)}.
\code{corr} is a reordered correlation matrix for plotting.
\code{corrPos} is a data frame with \code{xName, yName, x, y, corr} and
\code{p.value}(if p.mat is not NULL)
column, which x and y are the position on the correlation matrix plot.
\code{arg} is a list of some corrplot() input parameters' value.
Now \code{type} is in.
}
\description{
A graphical display of a correlation matrix, confidence interval. The details
are paid great attention to. It can also visualize a general matrix by
setting \code{is.corr = FALSE}.
}
\details{
\code{corrplot} function offers flexible ways to visualize
correlation matrix, lower and upper bound of confidence interval matrix.
}
\note{
\code{Cairo} and \code{cairoDevice} packages is strongly recommended to
produce high-quality PNG, JPEG, TIFF bitmap files, especially for that
\code{method} \code{circle}, \code{ellipse}.
Row- and column names of the input matrix are used as labels rendered
in the corrplot. Plothmath expressions will be used if the name is prefixed
by one of the following characters: \code{:}, \code{=} or \code{$}.
For example \code{':alpha + beta'}.
}
\examples{
data(mtcars)
M = cor(mtcars)
set.seed(0)
## different color series
## COL2: Get diverging colors
## c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu')
## COL1: Get sequential colors
## c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd', 'YlOrRd', 'YlOrBr', 'YlGn')
wb = c('white', 'black')
par(ask = TRUE)
## different color scale and methods to display corr-matrix
corrplot(M, method = 'number', col = 'black', cl.pos = 'n')
corrplot(M, method = 'number')
corrplot(M)
corrplot(M, order = 'AOE')
corrplot(M, order = 'AOE', addCoef.col = 'grey')
corrplot(M, order = 'AOE', cl.length = 21, addCoef.col = 'grey')
corrplot(M, order = 'AOE', col = COL2(n=10), addCoef.col = 'grey')
corrplot(M, order = 'AOE', col = COL2('PiYG'))
corrplot(M, order = 'AOE', col = COL2('PRGn'), addCoef.col = 'grey')
corrplot(M, order = 'AOE', col = COL2('PuOr', 20), cl.length = 21, addCoef.col = 'grey')
corrplot(M, order = 'AOE', col = COL2('PuOr', 10), addCoef.col = 'grey')
corrplot(M, order = 'AOE', col = COL2('RdYlBu', 100))
corrplot(M, order = 'AOE', col = COL2('RdYlBu', 10))
corrplot(M, method = 'color', col = COL2(n=20), cl.length = 21, order = 'AOE',
addCoef.col = 'grey')
corrplot(M, method = 'square', col = COL2(n=200), order = 'AOE')
corrplot(M, method = 'ellipse', col = COL2(n=200), order = 'AOE')
corrplot(M, method = 'shade', col = COL2(n=20), order = 'AOE')
corrplot(M, method = 'pie', order = 'AOE')
## col = wb
corrplot(M, col = wb, order = 'AOE', outline = TRUE, cl.pos = 'n')
## like Chinese wiqi, suit for either on screen or white-black print.
corrplot(M, col = wb, bg = 'gold2', order = 'AOE', cl.pos = 'n')
## mixed methods: It's more efficient if using function 'corrplot.mixed'
## circle + ellipse
corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd')
corrplot(M, add = TRUE, type = 'lower', method = 'ellipse', order = 'AOE',
diag = FALSE, tl.pos = 'n', cl.pos = 'n')
## circle + square
corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd')
corrplot(M, add = TRUE, type = 'lower', method = 'square', order = 'AOE',
diag = FALSE, tl.pos = 'n', cl.pos = 'n')
## circle + colorful number
corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'd')
corrplot(M, add = TRUE, type = 'lower', method = 'number', order = 'AOE',
diag = FALSE, tl.pos = 'n', cl.pos = 'n')
## circle + black number
corrplot(M, order = 'AOE', type = 'upper', tl.pos = 'tp')
corrplot(M, add = TRUE, type = 'lower', method = 'number', order = 'AOE',
col = 'black', diag = FALSE, tl.pos = 'n', cl.pos = 'n')
## order is hclust and draw rectangles
corrplot(M, order = 'hclust')
corrplot(M, order = 'hclust', addrect = 2)
corrplot(M, order = 'hclust', addrect = 3, rect.col = 'red')
corrplot(M, order = 'hclust', addrect = 4, rect.col = 'blue')
corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 4)
## visualize a matrix in [0, 1]
corrplot(abs(M), order = 'AOE', col.lim = c(0, 1))
corrplot(abs(M), order = 'AOE', is.corr = FALSE, col.lim = c(0, 1))
# when is.corr=TRUE, col.lim only affect the color legend
# If you change it, the color is still assigned on [-1, 1]
corrplot(M/2)
corrplot(M/2, col.lim = c(-0.5, 0.5))
# when is.corr=FALSE, col.lim is also used to assign colors
# if the matrix have both positive and negative values
# the matrix transformation keep every values positive and negative
corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2))
corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2) * 2)
corrplot(M*2, is.corr = FALSE, col.lim = c(-2, 2) * 4)
## 0.5~0.6
corrplot(abs(M)/10+0.5, col = COL1('Greens', 10))
corrplot(abs(M)/10+0.5, is.corr = FALSE, col.lim = c(0.5, 0.6), col = COL1('YlGn', 10))
## visualize a matrix in [-100, 100]
ran = round(matrix(runif(225, -100, 100), 15))
corrplot(ran, is.corr = FALSE)
corrplot(ran, is.corr = FALSE, col.lim = c(-100, 100))
## visualize a matrix in [100, 300]
ran2 = ran + 200
# bad color, not suitable for a matrix in [100, 300]
corrplot(ran2, is.corr = FALSE, col.lim = c(100, 300), col = COL2(, 100))
# good color
corrplot(ran2, is.corr = FALSE, col.lim = c(100, 300), col = COL1(, 100))
## text-labels and plot type
corrplot(M, order = 'AOE', tl.srt = 45)
corrplot(M, order = 'AOE', tl.srt = 60)
corrplot(M, order = 'AOE', tl.pos = 'd', cl.pos = 'n')
corrplot(M, order = 'AOE', diag = FALSE, tl.pos = 'd')
corrplot(M, order = 'AOE', type = 'upper')
corrplot(M, order = 'AOE', type = 'upper', diag = FALSE)
corrplot(M, order = 'AOE', type = 'lower', cl.pos = 'b')
corrplot(M, order = 'AOE', type = 'lower', cl.pos = 'b', diag = FALSE)
#### color-legend
corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'l')
corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'c')
corrplot(M, order = 'AOE', cl.ratio = 0.2, cl.align = 'r')
corrplot(M, order = 'AOE', cl.pos = 'b')
corrplot(M, order = 'AOE', cl.pos = 'b', tl.pos = 'd')
corrplot(M, order = 'AOE', cl.pos = 'n')
## deal with missing Values
M2 = M
diag(M2) = NA
corrplot(M2)
corrplot(M2, na.label = 'o')
corrplot(M2, na.label = 'NA')
##the input matrix is not square
corrplot(M[1:8, ])
corrplot(M[, 1:8])
testRes = cor.mtest(mtcars, conf.level = 0.95)
## specialized the insignificant value according to the significant level
corrplot(M, p.mat = testRes$p, sig.level = 0.05, order = 'hclust', addrect = 2)
## leave blank on no significant coefficient
corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig ='blank',
addCoef.col ='black', number.cex = 0.8, order = 'AOE', diag = FALSE)
## add p-values on no significant coefficients
corrplot(M, p.mat = testRes$p, insig = 'p-value')
## add all p-values
corrplot(M, p.mat = testRes$p, insig = 'p-value', sig.level = -1)
## add significant level stars
corrplot(M, p.mat = testRes$p, method = 'color', diag = FALSE, type = 'upper',
sig.level = c(0.001, 0.01, 0.05), pch.cex = 0.9,
insig = 'label_sig', pch.col = 'grey20', order = 'AOE')
## add significant level stars and cluster rectangles
corrplot(M, p.mat = testRes$p, tl.pos = 'd', order = 'hclust', addrect = 2,
insig = 'label_sig', sig.level = c(0.001, 0.01, 0.05),
pch.cex = 0.9, pch.col = 'grey20')
# Visualize confidence interval
corrplot(M, lowCI = testRes$lowCI, uppCI = testRes$uppCI, order = 'hclust',
tl.pos = 'd', rect.col = 'navy', plotC = 'rect', cl.pos = 'n')
# Visualize confidence interval and cross the significant coefficients
corrplot(M, p.mat = testRes$p, lowCI = testRes$lowCI, uppCI = testRes$uppCI,
addrect = 3, rect.col = 'navy', plotC = 'rect', cl.pos = 'n')
res1 = cor.mtest(mtcars, conf.level = 0.95)
res2 = cor.mtest(mtcars, conf.level = 0.99)
## plot confidence interval(0.95), 'circle' method
corrplot(M, low = res1$uppCI, upp = res1$uppCI,
plotCI = 'circle', addg = 'grey20', cl.pos = 'n')
corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
plotCI = 'circle', addg = 'grey20', cl.pos = 'n')
corrplot(M, low = res1$lowCI, upp = res1$uppCI,
col = c('white', 'black'), bg = 'gold2', order = 'AOE',
plotCI = 'circle', cl.pos = 'n', pch.col = 'red')
corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
col = c('white', 'black'), bg = 'gold2', order = 'AOE',
plotCI = 'circle', cl.pos = 'n', pch.col = 'red')
## plot confidence interval(0.95), 'square' method
corrplot(M, low = res1$lowCI, upp = res1$uppCI,
col = c('white', 'black'), bg = 'gold2', order = 'AOE',
plotCI = 'square', addg = NULL, cl.pos = 'n')
corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
col = c('white', 'black'), bg = 'gold2', order = 'AOE', pch.col = 'red',
plotCI = 'square', addg = NULL, cl.pos = 'n')
## plot confidence interval0.95, 0.95, 0.99, 'rect' method
corrplot(M, low = res1$lowCI, upp = res1$uppCI, order = 'hclust',
rect.col = 'navy', plotCI = 'rect', cl.pos = 'n')
corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
order = 'hclust', pch.col = 'red', sig.level = 0.05, addrect = 3,
rect.col = 'navy', plotCI = 'rect', cl.pos = 'n')
corrplot(M, p.mat = res2$p, low = res2$lowCI, upp = res2$uppCI,
order = 'hclust', pch.col = 'red', sig.level = 0.01, addrect = 3,
rect.col = 'navy', plotCI = 'rect', cl.pos = 'n')
## an animation of changing confidence interval in different significance level
## begin.animaton
par(ask = FALSE)
for (i in seq(0.1, 0, -0.005)) {
tmp = cor.mtest(mtcars, conf.level = 1 - i)
corrplot(M, p.mat = tmp$p, low = tmp$lowCI, upp = tmp$uppCI, order = 'hclust',
pch.col = 'red', sig.level = i, plotCI = 'rect', cl.pos = 'n',
mar = c(0, 0, 1, 0),
title = substitute(alpha == x,
list(x = format(i, digits = 3, nsmall = 3))))
Sys.sleep(0.15)
}
## end.animaton
}
\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{
Function \code{plotcorr} in the \code{ellipse} package and
\code{corrgram} in the \code{corrgram} package have some similarities.
Package \code{seriation} offered more methods to reorder matrices, such as
ARSA, BBURCG, BBWRCG, MDS, TSP, Chen and so forth.
}
\author{
Taiyun Wei (weitaiyun@gmail.com)
Viliam Simko (viliam.simko@gmail.com)
Michael Levy (michael.levy@healthcatalyst.com)
}
corrplot/man/corrRect.hclust.Rd 0000644 0001762 0000144 00000004525 14703115404 016251 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/corrRect.hclust.R
\name{corrRect.hclust}
\alias{corrRect.hclust}
\title{Draw rectangles on the correlation matrix graph.}
\usage{
corrRect.hclust(
corr,
k = 2,
col = "black",
lwd = 2,
method = c("complete", "ward", "ward.D", "ward.D2", "single", "average", "mcquitty",
"median", "centroid")
)
}
\arguments{
\item{corr}{Correlation matrix for function \code{corrRect.hclust}. It use
\code{1-corr} as dist in hierarchical clustering (\code{\link{hclust}}).}
\item{k}{Integer, the number of rectangles drawn on the graph according to
the hierarchical cluster, for function \code{corrRect.hclust}.}
\item{col}{Color of rectangles.}
\item{lwd}{Line width of rectangles.}
\item{method}{Character, the agglomeration method to be used for hierarchical
clustering (\code{\link{hclust}}). This should be (an unambiguous
abbreviation of) one of \code{'ward'}, \code{'ward.D'}, \code{'ward.D2'},
\code{'single'}, \code{'complete'}, \code{'average'}, \code{'mcquitty'},
\code{'median'} or \code{'centroid'}.}
}
\description{
Draw rectangles on the correlation matrix graph based on hierarchical cluster
(\code{\link{hclust}}).
}
\examples{
data(mtcars)
M = cor(mtcars)
corrplot(M, order = 'FPC') -> p
corrRect(p, index = c(1, 6, 11))
if(getRversion() >= '4.1.0') {
corrplot(M, order = 'FPC') |> corrRect(index = c(1, 6, 11))
}
(order.hc = corrMatOrder(M, order = 'hclust'))
(order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D2'))
M.hc = M[order.hc, order.hc]
M.hc2 = M[order.hc2, order.hc2]
par(ask = TRUE)
# same as: corrplot(M, order = 'hclust', addrect = 2)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 2)
# same as: corrplot(M, order = 'hclust', addrect = 3)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 3)
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 2)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 2, method = 'ward.D2')
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 3)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 3, method = 'ward.D2')
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D2', addrect = 4)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 4, method = 'ward.D2')
}
\author{
Taiyun Wei
}
\keyword{hplot}
corrplot/man/COL1.Rd 0000644 0001762 0000144 00000005136 14703117147 013670 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/colors.R
\name{COL1}
\alias{COL1}
\title{Get sequential colors}
\usage{
COL1(
sequential = c("Oranges", "Purples", "Reds", "Blues", "Greens", "Greys", "OrRd",
"YlOrRd", "YlOrBr", "YlGn"),
n = 200
)
}
\arguments{
\item{sequential}{Sequential color Palettes}
\item{n}{the number of colors (>= 1) to be in the palette.}
}
\value{
A character vector containing color names
}
\description{
Get sequential colors from palette theme name and n. The color palettes
are from RColorBrewer. Sequential colors are suitable for visualize a non-negative
or non-positive matrix (e.g. matrix in [0, 20], or [-100, -10], or [100, 500]).
}
\examples{
## diverging colors
par(mar = c(0, 0, 0, 0) + 0.1)
plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n')
col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu')
for(i in 1:length(col)) {
colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8)
}
## sequential colors
par(mar = c(0, 0, 0, 0) + 0.1)
plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n')
col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd',
'YlOrRd', 'YlOrBr', 'YlGn')
for(i in 1:length(col)) {
colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2)
}
## other examples to show colorlegend function
par(mar = rep(0, 4))
plot(0, xlim = c(0, 6), ylim = c(-0.5, 1.2), type = 'n')
colorlegend(rainbow(100), 0:9)
colorlegend(heat.colors(100), LETTERS[1:12], xlim = c(1, 2))
colorlegend(terrain.colors(100), 0:9, ratio.colbar = 0.6,
lim.segment = c(0, 0.6), xlim = c(2, 3), align = 'l')
colorlegend(topo.colors(100), 0:9, lim.segment = c(0, 0.6),
xlim = c(3, 4), align = 'l', offset = 0)
colorlegend(cm.colors(100), 1:5, xlim = c(4, 5))
colorlegend(sample(rainbow(12)), labels = LETTERS[1:12],
at = seq(0.05, 0.95, len = 12), xlim = c(5, 6), align = 'r')
colorlegend(colbar = grey(1:100 / 100), 1:10, col = 'red', align = 'l',
xlim = c(0, 6), ylim = c(-0.5, -0.1), vertical = FALSE)
colorlegend(sample(rainbow(12)),
labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12),
xlim = c(0, 6), ylim = c(1.1, 1.2), vertical = FALSE)
}
\seealso{
Function \code{\link{colorRampPalette}}, package \code{RColorBrewer}
}
\keyword{color}
corrplot/man/corrplot.mixed.Rd 0000644 0001762 0000144 00000004265 14703115404 016137 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/corrplot.mixed.R
\name{corrplot.mixed}
\alias{corrplot.mixed}
\title{Using mixed methods to visualize a correlation matrix.}
\usage{
corrplot.mixed(
corr,
lower = "number",
upper = "circle",
tl.pos = c("d", "lt", "n"),
diag = c("n", "l", "u"),
bg = "white",
addgrid.col = "grey",
lower.col = NULL,
upper.col = NULL,
plotCI = c("n", "square", "circle", "rect"),
mar = c(0, 0, 0, 0),
...
)
}
\arguments{
\item{corr}{Matrix, the correlation matrix to visualize.}
\item{lower}{Character, the visualization method for the lower triangular
correlation matrix.}
\item{upper}{Character, the visualization method for the upper triangular
correlation matrix.}
\item{tl.pos}{Character, \code{'lt'}, \code{'d'} or \code{'n'}, giving
position of text labels, \code{'lt'} means left and top, \code{'d'} means
diagonal. If \code{'n'}, add no textlabel.}
\item{diag}{Character, for specifying the glyph on the principal diagonal. It
is one of \code{'n'} (default, draw nothing), \code{'l'} (draw the glyphs
of lower triangular) or \code{'u'} (draw the glyphs of upper triangular).}
\item{bg}{The background color.}
\item{addgrid.col}{See the \code{addgrid.col} parameter in the function
\code{\link{corrplot}}}
\item{lower.col}{Passed as \code{col} parameter to the lower matrix.}
\item{upper.col}{Passed as \code{col} parameter to the upper matrix.}
\item{plotCI}{See the \code{plotCI} parameter in the function
\code{\link{corrplot}}}
\item{mar}{See \code{\link{par}}.}
\item{\dots}{Additional arguments for corrplot's wrappers}
}
\description{
Using mixed methods to visualize a correlation matrix.
}
\examples{
M = cor(mtcars)
ord = corrMatOrder(M, order = 'AOE')
M2 = M[ord, ord]
corrplot.mixed(M2)
corrplot.mixed(M2, lower = 'ellipse', upper = 'circle')
corrplot.mixed(M2, lower = 'square', upper = 'circle')
corrplot.mixed(M2, lower = 'shade', upper = 'circle')
corrplot.mixed(M2, tl.pos = 'lt')
corrplot.mixed(M2, tl.pos = 'lt', diag = 'u')
corrplot.mixed(M2, tl.pos = 'lt', diag = 'l')
corrplot.mixed(M2, tl.pos = 'n')
}
\author{
Taiyun Wei
}
corrplot/man/COL2.Rd 0000644 0001762 0000144 00000005242 14703117147 013667 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/colors.R
\name{COL2}
\alias{COL2}
\title{Get diverging colors}
\usage{
COL2(diverging = c("RdBu", "BrBG", "PiYG", "PRGn", "PuOr", "RdYlBu"), n = 200)
}
\arguments{
\item{diverging}{Diverging color Palettes}
\item{n}{the number of colors (>= 1) to be in the palette.}
}
\value{
A character vector containing color names
}
\description{
Get diverging colors from palette theme name and n. The color palettes
are from RColorBrewer, but with the middle color changing to '#FFFFFF'(white),
thus we can visualize element 0 with white color.
Diverging colors are suitable for visualize a matrix which elements are partly positive and
partly negative (e.g. correlation matrix in [-1, 1], or [-20, 100]).
}
\examples{
## diverging colors
par(mar = c(0, 0, 0, 0) + 0.1)
plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n')
col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu')
for(i in 1:length(col)) {
colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8)
}
## sequential colors
par(mar = c(0, 0, 0, 0) + 0.1)
plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n')
col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd',
'YlOrRd', 'YlOrBr', 'YlGn')
for(i in 1:length(col)) {
colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2)
}
## other examples to show colorlegend function
par(mar = rep(0, 4))
plot(0, xlim = c(0, 6), ylim = c(-0.5, 1.2), type = 'n')
colorlegend(rainbow(100), 0:9)
colorlegend(heat.colors(100), LETTERS[1:12], xlim = c(1, 2))
colorlegend(terrain.colors(100), 0:9, ratio.colbar = 0.6,
lim.segment = c(0, 0.6), xlim = c(2, 3), align = 'l')
colorlegend(topo.colors(100), 0:9, lim.segment = c(0, 0.6),
xlim = c(3, 4), align = 'l', offset = 0)
colorlegend(cm.colors(100), 1:5, xlim = c(4, 5))
colorlegend(sample(rainbow(12)), labels = LETTERS[1:12],
at = seq(0.05, 0.95, len = 12), xlim = c(5, 6), align = 'r')
colorlegend(colbar = grey(1:100 / 100), 1:10, col = 'red', align = 'l',
xlim = c(0, 6), ylim = c(-0.5, -0.1), vertical = FALSE)
colorlegend(sample(rainbow(12)),
labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12),
xlim = c(0, 6), ylim = c(1.1, 1.2), vertical = FALSE)
}
\seealso{
Function \code{\link{colorRampPalette}}, package \code{RColorBrewer}
}
\keyword{color}
corrplot/man/corrMatOrder.Rd 0000644 0001762 0000144 00000004560 14703115404 015567 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/corrMatOrder.R
\name{corrMatOrder}
\alias{corrMatOrder}
\title{Reorder a correlation matrix.}
\usage{
corrMatOrder(
corr,
order = c("AOE", "FPC", "hclust", "alphabet"),
hclust.method = c("complete", "ward", "ward.D", "ward.D2", "single", "average",
"mcquitty", "median", "centroid")
)
}
\arguments{
\item{corr}{Correlation matrix to reorder.}
\item{order}{Character, the ordering method for the correlation matrix.
\itemize{
\item{\code{'AOE'} for the angular order of the eigenvectors.
It is calculated from the order of the angles, \eqn{a_i}:
\deqn{ a_i = arctan (e_{i2} / e_{i1}), if e_{i1} > 0}
\deqn{ a_i = arctan (e_{i2} / e_{i1}) + \pi, otherwise.}
where \eqn{e_1} and \eqn{e_2} are the largest two eigenvalues
of matrix \code{corr}.
See Michael Friendly (2002) for details.}
\item{\code{'FPC'} for the first principal component order.}
\item{\code{'hclust'} for hierarchical clustering order.}
\item{\code{'alphabet'} for alphabetical order.}
}}
\item{hclust.method}{Character, the agglomeration method to be used when
\code{order} is \code{hclust}. This should be one of \code{'ward'},
\code{'ward.D'}, \code{'ward.D2'}, \code{'single'}, \code{'complete'},
\code{'average'}, \code{'mcquitty'}, \code{'median'} or \code{'centroid'}.}
}
\value{
Returns a single permutation vector.
}
\description{
Draw rectangle(s) around the chart of corrrlation matrix based on the number
of each cluster's members.
}
\examples{
M = cor(mtcars)
(order.AOE = corrMatOrder(M, order = 'AOE'))
(order.FPC = corrMatOrder(M, order = 'FPC'))
(order.hc = corrMatOrder(M, order = 'hclust'))
(order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D'))
M.AOE = M[order.AOE, order.AOE]
M.FPC = M[order.FPC, order.FPC]
M.hc = M[order.hc, order.hc]
M.hc2 = M[order.hc2, order.hc2]
par(ask = TRUE)
corrplot(M)
corrplot(M.AOE)
corrplot(M.FPC)
corrplot(M.hc)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 2)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 3)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 2, method = 'ward.D')
}
\seealso{
Package \code{seriation} offers more methods to reorder matrices,
such as ARSA, BBURCG, BBWRCG, MDS, TSP, Chen and so forth.
}
\author{
Taiyun Wei
}
\keyword{hplot}
corrplot/man/corrRect.Rd 0000644 0001762 0000144 00000007126 14703115404 014750 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/corrRect.R
\name{corrRect}
\alias{corrRect}
\title{Draw rectangle(s) on the correlation matrix graph.}
\usage{
corrRect(
corrRes = NULL,
index = NULL,
name = NULL,
namesMat = NULL,
col = "black",
lwd = 2,
...
)
}
\arguments{
\item{corrRes}{List of the \code{corrplot()} returns.}
\item{index}{Vector, variable index of diag rect \code{c(Rect1from, Rect2from,
Rect3from, ..., RectNto)} on the correlation matrix graph.
It works when the colnames are the same as rownames, or both of them is NULL.
It needs \code{corrRes} inputted.}
\item{name}{Vector, variable name of diag rect \code{c(Rect1from, Rect2from,
Rect3from, ..., RectNto)} on the correlation matrix graph.
OIt works when the colnames are the same as rownames.
It needs \code{corrRes} inputted.}
\item{namesMat}{4-length character vector or 4-columns character matrix,
represents the names of xleft, ybottom, xright, ytop correspondingly.
It needs \code{corrRes} inputted.}
\item{col}{Color of rectangles.}
\item{lwd}{Line width of rectangles.}
\item{\dots}{Additional arguments passing to function \code{rect()}.}
}
\value{
(Invisibly) returns input parameter \code{corrRes},
usually \code{list(corr, corrTrans, arg)}.
}
\description{
Draw rectangle(s) after the correlation matrix plotted. SUGGESTION: It's more convenient
to draw rectangle(s) by using pipe operator `|>` since R 4.1.0.
}
\details{
\code{corrRect} needs one of \code{index}, \code{name} and \code{namesMat} inputted.
While \code{corrRect.hclust} can get the members in each cluster
based on hierarchical clustering (\code{\link{hclust}}).
}
\examples{
data(mtcars)
M = cor(mtcars)
r = rbind(c('gear', 'wt', 'qsec', 'carb'),
c('wt', 'gear', 'carb', 'qsec'))
corrplot(M, order = 'AOE') -> p
corrRect(p, namesMat = r)
# same as using pipe operator `|>` if R version >= 4.1.0:
# corrplot(M, order = 'AOE') |> corrRect(namesMat = r)
r = c('gear', 'carb', 'qsec', 'wt')
corrplot(M, order = 'AOE', type='lower') -> p
corrRect(p, namesMat = r)
# same as using pipe operator `|>` if R version >= 4.1.0:
# corrplot(M, order = 'AOE', type='lower') |> corrRect(namesMat = r)
corrplot(M, order = 'hclust', type = 'upper') -> p
corrRect(p, index = c(1, 6, 11))
# same as using pipe operator `|>` if R version >= 4.1.0:
# corrplot(M, order = 'AOE', type='lower') |> corrRect(index = c(1, 6, 11))
corrplot(M, order = 'hclust') -> p
corrRect(p, name = c('carb', 'qsec', 'gear'))
# same as using pipe operator `|>` if R version >= 4.1.0:
# corrplot(M, order = 'hclust') |> corrRect(name = c('carb', 'qsec', 'gear'))
(order.hc = corrMatOrder(M, order = 'hclust'))
(order.hc2 = corrMatOrder(M, order = 'hclust', hclust.method = 'ward.D'))
M.hc = M[order.hc, order.hc]
M.hc2 = M[order.hc2, order.hc2]
par(ask = TRUE)
# same as: corrplot(M, order = 'hclust', addrect = 2)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 2)
# same as: corrplot(M, order = 'hclust', addrect = 3)
corrplot(M.hc)
corrRect.hclust(corr = M.hc, k = 3)
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 2)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 2, method = 'ward.D')
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 3)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 3, method = 'ward.D')
# same as: corrplot(M, order = 'hclust', hclust.method = 'ward.D', addrect = 4)
corrplot(M.hc2)
corrRect.hclust(M.hc2, k = 4, method = 'ward.D')
}
\author{
Taiyun Wei
}
\keyword{hplot}
corrplot/man/colorlegend.Rd 0000644 0001762 0000144 00000006456 14703115404 015467 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/colorlegend.R
\name{colorlegend}
\alias{colorlegend}
\title{Draw color legend.}
\usage{
colorlegend(
colbar,
labels,
at = NULL,
xlim = c(0, 1),
ylim = c(0, 1),
vertical = TRUE,
ratio.colbar = 0.4,
lim.segment = "auto",
align = c("c", "l", "r"),
addlabels = TRUE,
...
)
}
\arguments{
\item{colbar}{Vector, color of colbar.}
\item{labels}{Vector, numeric or character to be written.}
\item{at}{Numeric vector (quantile), the position to put labels. See examples
for details.}
\item{xlim}{See in \code{\link{plot}}}
\item{ylim}{See in \code{\link{plot}}}
\item{vertical}{Logical, whether the colorlegend is vertical or horizon.}
\item{ratio.colbar}{The width ratio of colorbar to the total colorlegend
(including colorbar, segments and labels).}
\item{lim.segment}{Vector (quantile) of length 2, the elements should be in
[0,1], giving segments coordinates ranges. If the value is NULL or 'auto',
then the ranges are derived automatically.}
\item{align}{Character, alignment type of labels, \code{'l'} means left,
\code{'c'} means center and \code{'r'} right.
Only valid when \code{vertical} is \code{TRUE}.}
\item{addlabels}{Logical, whether add text label or not.}
\item{\dots}{Additional arguments, passed to \code{\link{plot}}}
}
\description{
Draw color legend.
}
\examples{
## diverging colors
par(mar = c(0, 0, 0, 0) + 0.1)
plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n')
col = c('RdBu', 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdYlBu')
for(i in 1:length(col)) {
colorlegend(COL2(col[i]), -10:10/10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2, cex = 0.8)
}
## sequential colors
par(mar = c(0, 0, 0, 0) + 0.1)
plot(0, xlim = c(-0.1, 1), ylim = c(0, 1), type = 'n')
col = c('Oranges', 'Purples', 'Reds', 'Blues', 'Greens', 'Greys', 'OrRd',
'YlOrRd', 'YlOrBr', 'YlGn')
for(i in 1:length(col)) {
colorlegend(COL1(col[i]), 0:10, align = 'l', cex = 0.8, xlim = c(0, 1),
ylim = c(i/length(col)-0.1, i/length(col)), vertical = FALSE)
text(-0.01, i/length(col)-0.02, col[i], adj = 0.5, pos = 2)
}
## other examples to show colorlegend function
par(mar = rep(0, 4))
plot(0, xlim = c(0, 6), ylim = c(-0.5, 1.2), type = 'n')
colorlegend(rainbow(100), 0:9)
colorlegend(heat.colors(100), LETTERS[1:12], xlim = c(1, 2))
colorlegend(terrain.colors(100), 0:9, ratio.colbar = 0.6,
lim.segment = c(0, 0.6), xlim = c(2, 3), align = 'l')
colorlegend(topo.colors(100), 0:9, lim.segment = c(0, 0.6),
xlim = c(3, 4), align = 'l', offset = 0)
colorlegend(cm.colors(100), 1:5, xlim = c(4, 5))
colorlegend(sample(rainbow(12)), labels = LETTERS[1:12],
at = seq(0.05, 0.95, len = 12), xlim = c(5, 6), align = 'r')
colorlegend(colbar = grey(1:100 / 100), 1:10, col = 'red', align = 'l',
xlim = c(0, 6), ylim = c(-0.5, -0.1), vertical = FALSE)
colorlegend(sample(rainbow(12)),
labels = LETTERS[1:12], at = seq(0.05, 0.95, len = 12),
xlim = c(0, 6), ylim = c(1.1, 1.2), vertical = FALSE)
}
\author{
Taiyun Wei
}
\keyword{hplot}
corrplot/man/corrplot-package.Rd 0000644 0001762 0000144 00000002334 14703115404 016416 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/corrplot-package.R
\docType{package}
\name{corrplot-package}
\alias{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.
}
\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.
}
\author{
Taiyun Wei (weitaiyun@gmail.com)
Viliam Simko (viliam.simko@gmail.com)
Maintainer: Taiyun Wei (weitaiyun@gmail.com)
}
\keyword{correlation}
\keyword{correlogram}
\keyword{dimensionality}
\keyword{feature}
\keyword{hplot}
\keyword{reduction}
\keyword{selection}
corrplot/man/cor.mtest.Rd 0000644 0001762 0000144 00000002141 14703115404 015073 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cor-mtest.R
\name{cor.mtest}
\alias{cor.mtest}
\title{Significance test which produces p-values and confidence intervals for each
pair of input features.}
\usage{
cor.mtest(mat, ...)
}
\arguments{
\item{mat}{Input matrix of size \code{NxF},
with \code{N} rows that represent samples
and \code{F} columns that represent features.}
\item{\dots}{Additional arguments passed to function \code{\link{cor.test}},
e.g. \code{conf.level = 0.95}.}
}
\value{
Return a list containing:
\item{p}{Square matrix of size \code{FxF} with p-values as cells}
\item{lowCI}{Square matrix of size \code{FxF}, each cell represents the
\emph{lower part} of a confidence interval}
\item{uppCI}{Square matrix of size \code{FxF}, each cell represents the
\emph{upper part} of a confidence interval}
}
\description{
Significance test which produces p-values and confidence intervals for each
pair of input features.
}
\seealso{
Function \code{\link{cor.test}}
}
\keyword{confidence}
\keyword{p-value}
\keyword{significance}
corrplot/DESCRIPTION 0000644 0001762 0000144 00000003102 14703205146 013622 0 ustar ligges users Package: corrplot
Type: Package
Title: Visualization of a Correlation Matrix
Version: 0.95
Date: 2024-10-14
Authors@R: c(
person('Taiyun', 'Wei', email = 'weitaiyun@gmail.com', role = c('cre', 'aut')),
person('Viliam', 'Simko', email = 'viliam.simko@gmail.com', role = 'aut'),
person('Michael', 'Levy', email = 'michael.levy@healthcatalyst.com', role = 'ctb'),
person('Yihui', 'Xie', email = 'xie@yihui.name', role = 'ctb'),
person('Yan', 'Jin', email = 'jyfeather@gmail.com', role = 'ctb'),
person('Jeff', 'Zemla', email = 'zemla@wisc.edu', role = 'ctb'),
person('Moritz', 'Freidank', email = 'freidankm@googlemail.com', role = 'ctb'),
person('Jun', 'Cai', email = 'cai-j12@mails.tsinghua.edu.cn', role = 'ctb'),
person('Tomas', 'Protivinsky', email = 'tomas.protivinsky@gmail.com', role = 'ctb')
)
Maintainer: Taiyun Wei
Suggests: seriation, knitr, RColorBrewer, rmarkdown, magrittr,
prettydoc, testthat
Description: Provides a visual exploratory tool on correlation matrix that supports automatic variable reordering to help detect hidden patterns among variables.
License: MIT + file LICENSE
URL: https://github.com/taiyun/corrplot
BugReports: https://github.com/taiyun/corrplot/issues
VignetteBuilder: knitr
RoxygenNote: 7.2.1
NeedsCompilation: no
Packaged: 2024-10-14 05:38:35 UTC; weita
Author: Taiyun Wei [cre, aut],
Viliam Simko [aut],
Michael Levy [ctb],
Yihui Xie [ctb],
Yan Jin [ctb],
Jeff Zemla [ctb],
Moritz Freidank [ctb],
Jun Cai [ctb],
Tomas Protivinsky [ctb]
Repository: CRAN
Date/Publication: 2024-10-14 12:11:18 UTC