corrplot/0000755000176200001440000000000013171234600012113 5ustar liggesuserscorrplot/inst/0000755000176200001440000000000013171165067013102 5ustar liggesuserscorrplot/inst/CITATION0000644000176200001440000000136113171156546014242 0ustar liggesusersYEAR <- 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/NEWS0000644000176200001440000000531313170415272013576 0ustar liggesusers CHANGES IN corrplot VERSION 0.81 CHANGES * Fixed #79: added parameter `win.asp` to control aspect ratio * Fixed #18: parameter `outline` combined with `method="pie"` controls the color of the otline circle of each pie. * updated vignette CHANGES IN corrplot VERSION 0.80 CHANGES * Fixed #70: Enable to plot a matrix with NA when 'is.corr = F' CHANGES IN corrplot VERSION 0.77 CHANGES * Fixed #58: make sure the margin is correct in corrplot.mixed(). * Revised document. CHANGES IN corrplot VERSION 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 #17: restore par() settings on exit (thanks, #yihui) * 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) CHANGES IN corrplot VERSION 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. CHANGES IN corrplot VERSION 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". CHANGES IN corrplot VERSION 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". CHANGES IN corrplot VERSION 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/doc/0000755000176200001440000000000013171165067013647 5ustar liggesuserscorrplot/inst/doc/corrplot-intro.R0000644000176200001440000002072113171165066016770 0ustar liggesusers## ----setup, include=FALSE------------------------------------------------ set.seed(0) # we need reproducible results knitr::opts_chunk$set( out.extra = 'style="display:block; margin: auto"', fig.align = "center", fig.path = "webimg/", dev = "png") ## ----methods------------------------------------------------------------- library(corrplot) M <- cor(mtcars) corrplot(M, method = "circle") corrplot(M, method = "square") corrplot(M, method = "ellipse") corrplot(M, method = "number") # Display the correlation coefficient corrplot(M, method = "shade") corrplot(M, method = "color") corrplot(M, method = "pie") ## ----layout-------------------------------------------------------------- corrplot(M, type = "upper") corrplot(M, type = "upper") ## ----mixed--------------------------------------------------------------- corrplot.mixed(M) corrplot.mixed(M, lower.col = "black", number.cex = .7) corrplot.mixed(M, lower = "ellipse", upper = "circle") corrplot.mixed(M, lower = "square", upper = "circle", tl.col = "black") ## ----order--------------------------------------------------------------- corrplot(M, order = "AOE") corrplot(M, order = "hclust") corrplot(M, order = "FPC") corrplot(M, order = "alphabet") ## ----rectangles---------------------------------------------------------- corrplot(M, order = "hclust", addrect = 2) corrplot(M, order = "hclust", addrect = 3) ## ----hclust-lightblue---------------------------------------------------- # Change background color to lightblue corrplot(M, type = "upper", order = "hclust", col = c("black", "white"), bg = "lightblue") ## ----color--------------------------------------------------------------- col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white", "cyan", "#007FFF", "blue", "#00007F")) col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061")) col3 <- colorRampPalette(c("red", "white", "blue")) col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F", "cyan", "#007FFF", "blue", "#00007F")) whiteblack <- c("white", "black") ## using these color spectra corrplot(M, order = "hclust", addrect = 2, col = col1(100)) corrplot(M, order = "hclust", addrect = 2, col = col2(50)) corrplot(M, order = "hclust", addrect = 2, col = col3(20)) corrplot(M, order = "hclust", addrect = 2, col = col4(10)) corrplot(M, order = "hclust", addrect = 2, col = whiteblack, bg = "gold2") ## ----hclust-stdcolors---------------------------------------------------- corrplot(M, order = "hclust", addrect = 2, col = heat.colors(100)) corrplot(M, order = "hclust", addrect = 2, col = terrain.colors(100)) corrplot(M, order = "hclust", addrect = 2, col = cm.colors(100)) corrplot(M, order = "hclust", addrect = 2, col = gray.colors(100)) ## ----hclust-rcolorbrewer------------------------------------------------- library(RColorBrewer) corrplot(M, type = "upper", order = "hclust", col = brewer.pal(n = 8, name = "RdBu")) corrplot(M, type = "upper", order = "hclust", col = brewer.pal(n = 8, name = "RdYlBu")) corrplot(M, type = "upper", order = "hclust", col = brewer.pal(n = 8, name = "PuOr")) ## ----color-label--------------------------------------------------------- ## remove color legend and text legend corrplot(M, order = "AOE", cl.pos = "n", tl.pos = "n") ## bottom color legend, diagonal text legend, rotate text label corrplot(M, order = "AOE", cl.pos = "b", tl.pos = "d", tl.srt = 60) ## a wider color legend with numbers right aligned corrplot(M, order = "AOE", cl.ratio = 0.2, cl.align = "r") ## text labels rotated 45 degrees corrplot(M, type = "lower", order = "hclust", tl.col = "black", tl.srt = 45) ## ----non-corr------------------------------------------------------------ corrplot(abs(M),order = "AOE", col = col3(200), cl.lim = c(0, 1)) ## visualize a matrix in [-100, 100] ran <- round(matrix(runif(225, -100,100), 15)) corrplot(ran, is.corr = FALSE, method = "square") ## a beautiful color legend corrplot(ran, is.corr = FALSE, method = "ellipse", cl.lim = c(-100, 100)) ## ----non-corr-asp-------------------------------------------------------- ran <- matrix(rnorm(70), ncol = 7) corrplot(ran, is.corr = FALSE, win.asp = .7, method = "circle") ## ----NAs----------------------------------------------------------------- M2 <- M diag(M2) = NA corrplot(M2) corrplot(M2, na.label = "o") corrplot(M2, na.label = "NA") ## ----plotmath------------------------------------------------------------ M2 <- M[1:5,1:5] colnames(M2) <- c("alpha", "beta", ":alpha+beta", ":a[0]", "=a[beta]") rownames(M2) <- c("alpha", "beta", NA, "$a[0]", "$ a[beta]") corrplot(M2) ## ----test---------------------------------------------------------------- res1 <- cor.mtest(mtcars, conf.level = .95) res2 <- cor.mtest(mtcars, conf.level = .99) ## specialized the insignificant value according to the significant level corrplot(M, p.mat = res1$p, sig.level = .2) corrplot(M, p.mat = res1$p, sig.level = .05) corrplot(M, p.mat = res1$p, sig.level = .01) ## leave blank on no significant coefficient corrplot(M, p.mat = res1$p, insig = "blank") ## add p-values on no significant coefficient corrplot(M, p.mat = res1$p, insig = "p-value") ## add all p-values corrplot(M, p.mat = res1$p, insig = "p-value", sig.level = -1) ## add cross on no significant coefficient corrplot(M, p.mat = res1$p, order = "hclust", insig = "pch", addrect = 3) ## ----ci------------------------------------------------------------------ corrplot(M, low = res1$lowCI, upp = res1$uppCI, order = "hclust", rect.col = "navy", plotC = "rect", cl.pos = "n") corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI, order = "hclust", pch.col = "red", sig.level = 0.01, addrect = 3, rect.col = "navy", plotC = "rect", cl.pos = "n") ## ----ci_with_label------------------------------------------------------- res1 <- cor.mtest(mtcars, conf.level = .95) corrplot(M, p.mat = res1$p, insig = "label_sig", sig.level = c(.001, .01, .05), pch.cex = .9, pch.col = "white") corrplot(M, p.mat = res1$p, method = "color", insig = "label_sig", pch.col = "white") corrplot(M, p.mat = res1$p, method = "color", type = "upper", sig.level = c(.001, .01, .05), pch.cex = .9, insig = "label_sig", pch.col = "white", order = "AOE") corrplot(M, p.mat = res1$p, insig = "label_sig", pch.col = "white", pch = "p<.05", pch.cex = .5, order = "AOE") ## ----pmat---------------------------------------------------------------- # matrix of the p-value of the correlation p.mat <- cor.mtest(mtcars)$p head(p.mat[, 1:5]) # Specialized the insignificant value according to the significant level corrplot(M, type = "upper", order = "hclust", p.mat = p.mat, sig.level = 0.01) # Leave blank on no significant coefficient corrplot(M, type = "upper", order = "hclust", p.mat = p.mat, sig.level = 0.01, insig = "blank") ## ----customized---------------------------------------------------------- col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA")) corrplot(M, method = "color", col = col(200), type = "upper", order = "hclust", number.cex = .7, addCoef.col = "black", # Add coefficient of correlation tl.col = "black", tl.srt = 90, # Text label color and rotation # Combine with significance p.mat = p.mat, sig.level = 0.01, insig = "blank", # hide correlation coefficient on the principal diagonal diag = FALSE) ## ----large_matrix-------------------------------------------------------- # generating large feature matrix (cols=features, rows=samples) num_features <- 60 # how many features num_samples <- 300 # how many samples DATASET <- matrix(runif(num_features * num_samples), nrow = num_samples, ncol = num_features) # setting some dummy names for the features e.g. f23 colnames(DATASET) <- paste0("f", 1:ncol(DATASET)) # let's make 30% of all features to be correlated with feature "f1" num_feat_corr <- num_features * .3 idx_correlated_features <- as.integer(seq(from = 1, to = num_features, length.out = num_feat_corr))[-1] for (i in idx_correlated_features) { DATASET[,i] <- DATASET[,1] + runif(num_samples) # adding some noise } corrplot(cor(DATASET), diag = FALSE, order = "FPC", tl.pos = "td", tl.cex = 0.5, method = "color", type = "upper") corrplot/inst/doc/corrplot-intro.html0000644000176200001440003323417413171165067017553 0ustar liggesusers An Introduction to <strong>corrplot</strong> Package

An Introduction to corrplot Package

Introduction

The corrplot package is a graphical display of a correlation matrix, confidence interval. 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.

Visualization methods

There are seven visualization methods (parameter method) in corrplot package, named "circle", "square", "ellipse", "number", "shade", "color", "pie".

Positive correlations are displayed in blue and negative correlations in red color. Color intensity and the size of the circle are proportional to the correlation coefficients.

library(corrplot)
## corrplot 0.84 loaded
M <- cor(mtcars)
corrplot(M, method = "circle")

plot of chunk methods

corrplot(M, method = "square")

plot of chunk methods

corrplot(M, method = "ellipse")

plot of chunk methods

corrplot(M, method = "number") # Display the correlation coefficient

plot of chunk methods

corrplot(M, method = "shade")

plot of chunk methods

corrplot(M, method = "color")

plot of chunk methods

corrplot(M, method = "pie")

plot of chunk methods

Layout

There are three layout types (parameter type):

corrplot(M, type = "upper")
corrplot(M, type = "upper")

plot of chunk layout

corrplot.mixed() is a wrapped function for mixed visualization style.

corrplot.mixed(M)

plot of chunk mixed

corrplot.mixed(M, lower.col = "black", number.cex = .7)

plot of chunk mixed

corrplot.mixed(M, lower = "ellipse", upper = "circle")

plot of chunk mixed

corrplot.mixed(M, lower = "square", upper = "circle", tl.col = "black")

plot of chunk mixed

Reorder a correlation matrix

The correlation matrix can be reordered according to the correlation coefficient. This is important to identify the hidden structure and pattern in the matrix. There are four methods in corrplot (parameter order), named "AOE", "FPC", "hclust", "alphabet". More algorithms can be found in seriation package.

You can also reorder the matrix “manually” via function corrMatOrder().

corrplot(M, order = "AOE")

plot of chunk order

corrplot(M, order = "hclust")

plot of chunk order

corrplot(M, order = "FPC")

plot of chunk order

corrplot(M, order = "alphabet")

plot of chunk order

If using "hclust", corrplot() can draw rectangles around the chart of corrrlation matrix based on the results of hierarchical clustering.

corrplot(M, order = "hclust", addrect = 2)

plot of chunk rectangles

corrplot(M, order = "hclust", addrect = 3)

plot of chunk rectangles

# Change background color to lightblue
corrplot(M, type = "upper", order = "hclust",
         col = c("black", "white"), bg = "lightblue")

plot of chunk hclust-lightblue

Using different color spectra

As shown in the above section, the color of the correlogram can be customized. The function colorRampPalette() is very convenient for generating color spectrum.

col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white",
                           "cyan", "#007FFF", "blue", "#00007F"))
col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582",
                           "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE",
                           "#4393C3", "#2166AC", "#053061"))
col3 <- colorRampPalette(c("red", "white", "blue")) 
col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F",
                           "cyan", "#007FFF", "blue", "#00007F"))
whiteblack <- c("white", "black")

## using these color spectra
corrplot(M, order = "hclust", addrect = 2, col = col1(100))

plot of chunk color

corrplot(M, order = "hclust", addrect = 2, col = col2(50))

plot of chunk color

corrplot(M, order = "hclust", addrect = 2, col = col3(20))

plot of chunk color

corrplot(M, order = "hclust", addrect = 2, col = col4(10))

plot of chunk color

corrplot(M, order = "hclust", addrect = 2, col = whiteblack, bg = "gold2")

plot of chunk color

You can also use the standard color palettes (package grDevices)

corrplot(M, order = "hclust", addrect = 2, col = heat.colors(100))

plot of chunk hclust-stdcolors

corrplot(M, order = "hclust", addrect = 2, col = terrain.colors(100))

plot of chunk hclust-stdcolors

corrplot(M, order = "hclust", addrect = 2, col = cm.colors(100))

plot of chunk hclust-stdcolors

corrplot(M, order = "hclust", addrect = 2, col = gray.colors(100))

plot of chunk hclust-stdcolors

Other option would be to use RcolorBrewer package.

library(RColorBrewer)

corrplot(M, type = "upper", order = "hclust",
         col = brewer.pal(n = 8, name = "RdBu"))

plot of chunk hclust-rcolorbrewer

corrplot(M, type = "upper", order = "hclust",
         col = brewer.pal(n = 8, name = "RdYlBu"))

plot of chunk hclust-rcolorbrewer

corrplot(M, type = "upper", order = "hclust",
         col = brewer.pal(n = 8, name = "PuOr"))

plot of chunk hclust-rcolorbrewer

Changing color and rotation of text labels and legend

Parameter cl.* is for color legend, and tl.* if for text legend. For the text label, tl.col (text label color) and tl.srt (text label string rotation) are used to change text colors and rotations.

Here are some examples.

## remove color legend and text legend 
corrplot(M, order = "AOE", cl.pos = "n", tl.pos = "n")  

plot of chunk color-label

## bottom  color legend, diagonal text legend, rotate text label
corrplot(M, order = "AOE", cl.pos = "b", tl.pos = "d", tl.srt = 60)

plot of chunk color-label

## a wider color legend with numbers right aligned
corrplot(M, order = "AOE", cl.ratio = 0.2, cl.align = "r")

plot of chunk color-label

## text labels rotated 45 degrees
corrplot(M, type = "lower", order = "hclust", tl.col = "black", tl.srt = 45)

plot of chunk color-label

Dealing with a non-correlation matrix

corrplot(abs(M),order = "AOE", col = col3(200), cl.lim = c(0, 1))

plot of chunk non-corr

## visualize a  matrix in [-100, 100]
ran <- round(matrix(runif(225, -100,100), 15))
corrplot(ran, is.corr = FALSE, method = "square")

plot of chunk non-corr

## a beautiful color legend 
corrplot(ran, is.corr = FALSE, method = "ellipse", cl.lim = c(-100, 100))

plot of chunk non-corr

If your matrix is rectangular, you can adjust the aspect ratio with the win.asp parameter to make the matrix rendered as a square.

ran <- matrix(rnorm(70), ncol = 7)
corrplot(ran, is.corr = FALSE, win.asp = .7, method = "circle")

plot of chunk non-corr-asp

Dealing with missing (NA) values

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).

M2 <- M
diag(M2) = NA
corrplot(M2)

plot of chunk NAs

corrplot(M2, na.label = "o")

plot of chunk NAs

corrplot(M2, na.label = "NA")

plot of chunk NAs

Using “plotmath” expressions in labels

Since version 0.78, it is possible to use plotmath expression in variable names. To activate plotmath rendering, prefix your label with one of the characters ":", "=" or "$".

M2 <- M[1:5,1:5]
colnames(M2) <- c("alpha", "beta", ":alpha+beta", ":a[0]", "=a[beta]")
rownames(M2) <- c("alpha", "beta", NA, "$a[0]", "$ a[beta]")
corrplot(M2)

plot of chunk plotmath

Combining correlogram with the significance test

res1 <- cor.mtest(mtcars, conf.level = .95)
res2 <- cor.mtest(mtcars, conf.level = .99)

## specialized the insignificant value according to the significant level
corrplot(M, p.mat = res1$p, sig.level = .2)

plot of chunk test

corrplot(M, p.mat = res1$p, sig.level = .05)

plot of chunk test

corrplot(M, p.mat = res1$p, sig.level = .01)

plot of chunk test

## leave blank on no significant coefficient
corrplot(M, p.mat = res1$p, insig = "blank")

plot of chunk test

## add p-values on no significant coefficient
corrplot(M, p.mat = res1$p, insig = "p-value")

plot of chunk test

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

plot of chunk test

## add cross on no significant coefficient 
corrplot(M, p.mat = res1$p, order = "hclust", insig = "pch", addrect = 3)

plot of chunk test

Visualize confidence interval

corrplot(M, low = res1$lowCI, upp = res1$uppCI, order = "hclust",
         rect.col = "navy", plotC = "rect", cl.pos = "n")

plot of chunk ci

corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
         order = "hclust", pch.col = "red", sig.level = 0.01,
         addrect = 3, rect.col = "navy", plotC = "rect", cl.pos = "n")

plot of chunk ci

res1 <- cor.mtest(mtcars, conf.level = .95)

corrplot(M, p.mat = res1$p, insig = "label_sig",
         sig.level = c(.001, .01, .05), pch.cex = .9, pch.col = "white")

plot of chunk ci_with_label

corrplot(M, p.mat = res1$p, method = "color",
         insig = "label_sig", pch.col = "white")

plot of chunk ci_with_label

corrplot(M, p.mat = res1$p, method = "color", type = "upper",
         sig.level = c(.001, .01, .05), pch.cex = .9,
         insig = "label_sig", pch.col = "white", order = "AOE")

plot of chunk ci_with_label

corrplot(M, p.mat = res1$p, insig = "label_sig", pch.col = "white",
         pch = "p<.05", pch.cex = .5, order = "AOE")

plot of chunk ci_with_label

Customize the correlogram

# matrix of the p-value of the correlation
p.mat <- cor.mtest(mtcars)$p
head(p.mat[, 1:5])
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.000000e+00 6.112687e-10 9.380327e-10 1.787835e-07 1.776240e-05
## [2,] 6.112687e-10 0.000000e+00 1.802838e-12 3.477861e-09 8.244636e-06
## [3,] 9.380327e-10 1.802838e-12 0.000000e+00 7.142679e-08 5.282022e-06
## [4,] 1.787835e-07 3.477861e-09 7.142679e-08 0.000000e+00 9.988772e-03
## [5,] 1.776240e-05 8.244636e-06 5.282022e-06 9.988772e-03 0.000000e+00
## [6,] 1.293959e-10 1.217567e-07 1.222320e-11 4.145827e-05 4.784260e-06
# Specialized the insignificant value according to the significant level
corrplot(M, type = "upper", order = "hclust", 
         p.mat = p.mat, sig.level = 0.01)

plot of chunk pmat

# Leave blank on no significant coefficient
corrplot(M, type = "upper", order = "hclust", 
         p.mat = p.mat, sig.level = 0.01, insig = "blank")

plot of chunk pmat In the above figure, correlations with p-value > 0.01 are considered as insignificant. In this case the correlation coefficient values are leaved blank or crosses are added.

col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot(M, method = "color", col = col(200),
         type = "upper", order = "hclust", number.cex = .7,
         addCoef.col = "black", # Add coefficient of correlation
         tl.col = "black", tl.srt = 90, # Text label color and rotation
         # Combine with significance
         p.mat = p.mat, sig.level = 0.01, insig = "blank", 
         # hide correlation coefficient on the principal diagonal
         diag = FALSE)

plot of chunk customized

Note: Some of the plots were taken from this blog.

Explore Large Feature Matrices

# generating large feature matrix (cols=features, rows=samples)
num_features <- 60 # how many features
num_samples <- 300 # how many samples
DATASET <- matrix(runif(num_features * num_samples),
               nrow = num_samples, ncol = num_features)

# setting some dummy names for the features e.g. f23
colnames(DATASET) <- paste0("f", 1:ncol(DATASET))

# let's make 30% of all features to be correlated with feature "f1"
num_feat_corr <- num_features * .3
idx_correlated_features <- as.integer(seq(from = 1,
                                          to = num_features,
                                          length.out = num_feat_corr))[-1]
for (i in idx_correlated_features) {
  DATASET[,i] <- DATASET[,1] + runif(num_samples) # adding some noise
}

corrplot(cor(DATASET), diag = FALSE, order = "FPC",
         tl.pos = "td", tl.cex = 0.5, method = "color", type = "upper")

plot of chunk large_matrix

h
corrplot/inst/doc/corrplot-intro.Rmd0000644000176200001440000003002713171156546017314 0ustar liggesusers An Introduction to **corrplot** Package ======================================= ```{r setup, include=FALSE} set.seed(0) # we need reproducible results knitr::opts_chunk$set( out.extra = 'style="display:block; margin: auto"', fig.align = "center", fig.path = "webimg/", dev = "png") ``` Introduction ------------ The **corrplot** package is a graphical display of a correlation matrix, confidence interval. 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. Visualization methods ---------------------------- There are seven visualization methods (parameter `method`) in **corrplot** package, named `"circle"`, `"square"`, `"ellipse"`, `"number"`, `"shade"`, `"color"`, `"pie"`. > Positive correlations are displayed in blue and negative correlations in red > color. Color intensity and the size of the circle are proportional to the > correlation coefficients. ```{r methods} library(corrplot) M <- cor(mtcars) corrplot(M, method = "circle") corrplot(M, method = "square") corrplot(M, method = "ellipse") corrplot(M, method = "number") # Display the correlation coefficient corrplot(M, method = "shade") corrplot(M, method = "color") corrplot(M, method = "pie") ``` Layout ----------------------------- There are three layout types (parameter `type`): - `"full"` (default) : display full **correlation matrix** - `"upper"` : display upper triangular of the **correlation matrix** - `"lower"` : display lower triangular of the **correlation matrix** ```{r layout} corrplot(M, type = "upper") corrplot(M, type = "upper") ``` `corrplot.mixed()` is a wrapped function for mixed visualization style. ```{r mixed} corrplot.mixed(M) corrplot.mixed(M, lower.col = "black", number.cex = .7) corrplot.mixed(M, lower = "ellipse", upper = "circle") corrplot.mixed(M, lower = "square", upper = "circle", tl.col = "black") ``` Reorder a correlation matrix ---------------------------- The correlation matrix can be reordered according to the correlation coefficient. This is important to identify the hidden structure and pattern in the matrix. There are four methods in corrplot (parameter `order`), named `"AOE"`, `"FPC"`, `"hclust"`, `"alphabet"`. More algorithms can be found in [seriation](cran.r-project.org/package=seriation) package. You can also reorder the matrix "manually" via function `corrMatOrder()`. - `"AOE"` is for the angular order of the eigenvectors. It is calculated from the order of the angles $a_i$, $$ a_i = \begin{cases} \tan (e_{i2}/e_{i1}), & \text{if $e_{i1}>0$;} \newline \tan (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"`, `"single"`, `"complete"`, `"average"`, `"mcquitty"`, `"median"` or `"centroid"`. - `"alphabet"` for alphabetical order. ```{r order} corrplot(M, order = "AOE") corrplot(M, order = "hclust") corrplot(M, order = "FPC") corrplot(M, order = "alphabet") ``` If using `"hclust"`, `corrplot()` can draw rectangles around the chart of corrrlation matrix based on the results of hierarchical clustering. ```{r rectangles} corrplot(M, order = "hclust", addrect = 2) corrplot(M, order = "hclust", addrect = 3) ``` ```{r hclust-lightblue} # Change background color to lightblue corrplot(M, type = "upper", order = "hclust", col = c("black", "white"), bg = "lightblue") ``` Using different color spectra ------------------------------ As shown in the above section, the color of the correlogram can be customized. The function `colorRampPalette()` is very convenient for generating color spectrum. ```{r color} col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white", "cyan", "#007FFF", "blue", "#00007F")) col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061")) col3 <- colorRampPalette(c("red", "white", "blue")) col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F", "cyan", "#007FFF", "blue", "#00007F")) whiteblack <- c("white", "black") ## using these color spectra corrplot(M, order = "hclust", addrect = 2, col = col1(100)) corrplot(M, order = "hclust", addrect = 2, col = col2(50)) corrplot(M, order = "hclust", addrect = 2, col = col3(20)) corrplot(M, order = "hclust", addrect = 2, col = col4(10)) corrplot(M, order = "hclust", addrect = 2, col = whiteblack, bg = "gold2") ``` You can also use the standard color palettes (package `grDevices`) ```{r hclust-stdcolors} corrplot(M, order = "hclust", addrect = 2, col = heat.colors(100)) corrplot(M, order = "hclust", addrect = 2, col = terrain.colors(100)) corrplot(M, order = "hclust", addrect = 2, col = cm.colors(100)) corrplot(M, order = "hclust", addrect = 2, col = gray.colors(100)) ``` Other option would be to use `RcolorBrewer` package. ```{r hclust-rcolorbrewer} library(RColorBrewer) corrplot(M, type = "upper", order = "hclust", col = brewer.pal(n = 8, name = "RdBu")) corrplot(M, type = "upper", order = "hclust", col = brewer.pal(n = 8, name = "RdYlBu")) corrplot(M, type = "upper", order = "hclust", col = brewer.pal(n = 8, name = "PuOr")) ``` Changing color and rotation of text labels and legend ----------------------------------------------------- Parameter `cl.*` is for color legend, and `tl.*` if for text legend. For the text label, `tl.col` (text label color) and `tl.srt` (text label string rotation) are used to change text colors and rotations. Here are some examples. ```{r color-label} ## remove color legend and text legend corrplot(M, order = "AOE", cl.pos = "n", tl.pos = "n") ## bottom color legend, diagonal text legend, rotate text label corrplot(M, order = "AOE", cl.pos = "b", tl.pos = "d", tl.srt = 60) ## a wider color legend with numbers right aligned corrplot(M, order = "AOE", cl.ratio = 0.2, cl.align = "r") ## text labels rotated 45 degrees corrplot(M, type = "lower", order = "hclust", tl.col = "black", tl.srt = 45) ``` Dealing with a non-correlation matrix ------------------------------------- ```{r non-corr} corrplot(abs(M),order = "AOE", col = col3(200), cl.lim = c(0, 1)) ## visualize a matrix in [-100, 100] ran <- round(matrix(runif(225, -100,100), 15)) corrplot(ran, is.corr = FALSE, method = "square") ## a beautiful color legend corrplot(ran, is.corr = FALSE, method = "ellipse", cl.lim = c(-100, 100)) ``` 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-asp} ran <- matrix(rnorm(70), ncol = 7) corrplot(ran, is.corr = FALSE, win.asp = .7, method = "circle") ``` Dealing with missing (NA) values -------------------------------- 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). ```{r NAs} M2 <- M diag(M2) = NA corrplot(M2) corrplot(M2, na.label = "o") corrplot(M2, na.label = "NA") ``` Using "plotmath" expressions in labels -------------------------------------- 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 one of the characters `":"`, `"="` or `"$"`. ```{r plotmath} M2 <- M[1:5,1:5] colnames(M2) <- c("alpha", "beta", ":alpha+beta", ":a[0]", "=a[beta]") rownames(M2) <- c("alpha", "beta", NA, "$a[0]", "$ a[beta]") corrplot(M2) ``` Combining correlogram with the significance test ------------------------------------------------ ```{r test} res1 <- cor.mtest(mtcars, conf.level = .95) res2 <- cor.mtest(mtcars, conf.level = .99) ## specialized the insignificant value according to the significant level corrplot(M, p.mat = res1$p, sig.level = .2) corrplot(M, p.mat = res1$p, sig.level = .05) corrplot(M, p.mat = res1$p, sig.level = .01) ## leave blank on no significant coefficient corrplot(M, p.mat = res1$p, insig = "blank") ## add p-values on no significant coefficient corrplot(M, p.mat = res1$p, insig = "p-value") ## add all p-values corrplot(M, p.mat = res1$p, insig = "p-value", sig.level = -1) ## add cross on no significant coefficient corrplot(M, p.mat = res1$p, order = "hclust", insig = "pch", addrect = 3) ``` Visualize confidence interval ----------------------------- ```{r ci} corrplot(M, low = res1$lowCI, upp = res1$uppCI, order = "hclust", rect.col = "navy", plotC = "rect", cl.pos = "n") corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI, order = "hclust", pch.col = "red", sig.level = 0.01, addrect = 3, rect.col = "navy", plotC = "rect", cl.pos = "n") ``` ```{r ci_with_label} res1 <- cor.mtest(mtcars, conf.level = .95) corrplot(M, p.mat = res1$p, insig = "label_sig", sig.level = c(.001, .01, .05), pch.cex = .9, pch.col = "white") corrplot(M, p.mat = res1$p, method = "color", insig = "label_sig", pch.col = "white") corrplot(M, p.mat = res1$p, method = "color", type = "upper", sig.level = c(.001, .01, .05), pch.cex = .9, insig = "label_sig", pch.col = "white", order = "AOE") corrplot(M, p.mat = res1$p, insig = "label_sig", pch.col = "white", pch = "p<.05", pch.cex = .5, order = "AOE") ``` Customize the correlogram ------------------------- ```{r pmat} # matrix of the p-value of the correlation p.mat <- cor.mtest(mtcars)$p head(p.mat[, 1:5]) # Specialized the insignificant value according to the significant level corrplot(M, type = "upper", order = "hclust", p.mat = p.mat, sig.level = 0.01) # Leave blank on no significant coefficient corrplot(M, type = "upper", order = "hclust", p.mat = p.mat, sig.level = 0.01, insig = "blank") ``` In the above figure, correlations with **p-value > 0.01** are considered as insignificant. In this case the correlation coefficient values are leaved blank or crosses are added. ```{r customized} col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA")) corrplot(M, method = "color", col = col(200), type = "upper", order = "hclust", number.cex = .7, addCoef.col = "black", # Add coefficient of correlation tl.col = "black", tl.srt = 90, # Text label color and rotation # Combine with significance p.mat = p.mat, sig.level = 0.01, insig = "blank", # hide correlation coefficient on the principal diagonal diag = FALSE) ``` **Note:** Some of the plots were taken from [this blog]. [this blog]: http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram Explore Large Feature Matrices ------------------------------ ```{r large_matrix} # generating large feature matrix (cols=features, rows=samples) num_features <- 60 # how many features num_samples <- 300 # how many samples DATASET <- matrix(runif(num_features * num_samples), nrow = num_samples, ncol = num_features) # setting some dummy names for the features e.g. f23 colnames(DATASET) <- paste0("f", 1:ncol(DATASET)) # let's make 30% of all features to be correlated with feature "f1" num_feat_corr <- num_features * .3 idx_correlated_features <- as.integer(seq(from = 1, to = num_features, length.out = num_feat_corr))[-1] for (i in idx_correlated_features) { DATASET[,i] <- DATASET[,1] + runif(num_samples) # adding some noise } corrplot(cor(DATASET), diag = FALSE, order = "FPC", tl.pos = "td", tl.cex = 0.5, method = "color", type = "upper") ``` corrplot/tests/0000755000176200001440000000000013170415272013262 5ustar liggesuserscorrplot/tests/testthat.R0000644000176200001440000000007413170415272015246 0ustar liggesuserslibrary(testthat) library(corrplot) test_check("corrplot") corrplot/tests/testthat/0000755000176200001440000000000013171234600015115 5ustar liggesuserscorrplot/tests/testthat/test-colorlegend.R0000644000176200001440000000347113170415272020524 0ustar liggesuserscontext("Color legend") # suppress generating any PDFs pdf(NULL) test_that("Basic usage of colorlegend", { plot(0, type = "n") expect_silent(colorlegend(rainbow(100), 0:9)) expect_silent(colorlegend(rainbow(100), 0:9, 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") expect_silent(colorlegend(rainbow(100), 0:9, lim.segment = c(0,1))) }) test_that("Parameter `at` should be between 0 and 1", { plot(0, type = "n") expect_error(colorlegend(rainbow(100), 0:2, at = c(-1,.5,.8)), regexp = "should be between 0 and 1") expect_silent(colorlegend(rainbow(100), 0:2, at = c(0,.5,.8))) }) corrplot/tests/testthat/test-corrplot.R0000644000176200001440000001732013170415272020071 0ustar liggesuserscontext("Visualization of a correlation matrix") # Tests ========== test_that("Testing 'cl.pos' parameter", { M <- cor(mtcars) expect_silent(corrplot(M, cl.pos = TRUE)) expect_silent(corrplot(M, cl.pos = FALSE)) expect_silent(corrplot(M, cl.pos = "r")) expect_silent(corrplot(M, 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 'cl.lim' parameter", { M <- cor(mtcars) expect_silent(corrplot(M, cl.lim = c(-1, 1))) expect_error(corrplot(M, cl.lim = c(0, 1)), regexp = "color limits should cover matrix") }) 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") 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), M) }) test_that("Issue #70: Enable to plot a matrix with NA when 'is.corr = FALSE'", { M <- matrix(0, ncol = 5, nrow = 5) M[1,1] <- NA expect_true(is.matrix(corrplot(M, is.corr = FALSE))) }) 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"), M) expect_equal(corrplot.mixed(M, lower = "number", upper = "circle", lowCI = L, uppCI = U, plotCI = "rect"), M) expect_equal(corrplot.mixed(M, lower = "circle", upper = "square", lowCI = L, uppCI = U, plotCI = "rect"), M) expect_equal(corrplot.mixed(M, lower = "ellipse", upper = "square", lowCI = L, uppCI = U, plotCI = "rect"), M) expect_equal(corrplot.mixed(M, lower = "pie", upper = "square", lowCI = L, uppCI = U, plotCI = "rect"), M) }) test_that("Issue #43: Return value should be the same as corrplot function", { M <- cor(mtcars) expect_equal(corrplot.mixed(M), corrplot(M)) }) 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))) }) test_that("Try different ordering", { M <- cor(mtcars) expect_true(identical(M, corrplot(M))) 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)) expect_silent(corrplot(M, method = "number", number.digits = 200000)) 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")) 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) ^ .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(.001, .1, .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 = .5, order = "AOE")) }) corrplot/tests/testthat/test-cor-mtest.R0000644000176200001440000000123213170415272020135 0ustar liggesuserscontext("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/NAMESPACE0000644000176200001440000000035313170415272013340 0ustar liggesusers# Generated by roxygen2: do not edit by hand export(colorlegend) export(cor.mtest) export(corrMatOrder) export(corrRect) export(corrRect.hclust) export(corrplot) export(corrplot.mixed) import(grDevices) import(graphics) import(stats) corrplot/R/0000755000176200001440000000000013171156546012330 5ustar liggesuserscorrplot/R/cor-mtest.R0000644000176200001440000000272013170415272014362 0ustar liggesusers#' Significance test which produces p-values and confidence intervals for each #' pair of input features. #' #' @param mat Input matrix of size \code{FxS}, with \code{F} columns that #' represent features and \code{S} rows that represent samples. #' @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] } } } list( p = p.mat, lowCI = lowCI.mat, uppCI = uppCI.mat ) } corrplot/R/corrRect.R0000644000176200001440000000147513170415272014236 0ustar liggesusers#' Draw rectangle(s) on the correlation matrix graph. #' #' Draw rectangle(s) around the chart of corrrlation matrix. #' #' \code{corrRect} needs the number(parameter \code{clus}) of each cluster's #' members, while \code{corrRect.hclust} can get the members in each cluster #' based on hierarchical clustering (\code{\link{hclust}}). #' #' @param clus Vector, the number of each cluster's members. #' @param col Color of rectangles. #' @param lwd Line width of rectangles. #' #' @example vignettes/example-corrRect.R #' @keywords hplot #' @author Taiyun Wei #' @export corrRect <- function(clus, col = "black", lwd = 2) { hc <- length(clus) cu <- c(0, cumsum(clus)) rect(cu[-(hc + 1)] + 0.5, sum(clus) - cu[-(hc + 1)] + 0.5, cu[-1] + 0.5, sum(clus) - cu[-1] + 0.5, border = col, lwd = lwd) } corrplot/R/corrplot-package.R0000644000176200001440000000253613170415272015707 0ustar liggesusers#' @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/corrMatOrder.R0000644000176200001440000000533613170415272015056 0ustar liggesusers#' 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 = tan (e_{i2} / e_{i1}), if e_{i1} > 0} #' \deqn{ a_i = tan (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. #' @note pure function #' @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. #' @note pure function #' @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). #' @note pure function #' @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/corrplot.R0000644000176200001440000010243513171156546014324 0ustar liggesusers#' 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 add Logical, if \code{TRUE}, the graph is added to an existing plot, #' otherwise a new plot is created. #' #' @param col Vector, the color of glyphs. It is distributed uniformly in #' \code{cl.lim}. If NULL, \code{col} will be #' \code{colorRampPalette(col2)(200)}, see example about col2. #' #' @param bg The background color. #' #' @param title Character, title of the graph. #' #' @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 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{"d"} means diagonal, \code{"n"} means don't add textlabel. #' #' @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 labels; 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 colorlabel. #' #' @param cl.lim The limits \code{(x1, x2)} in the colorlabel. #' #' @param cl.length Integer, the number of number-text in colorlabel, 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, cex of number-label in colorlabel, passed to #' \code{\link{colorlegend}}. #' #' @param cl.ratio Numeric, to justify the width of colorlabel, 0.1~0.2 is #' suggested. #' #' @param cl.align.text Character, \code{"l"}, \code{"c"} (default) or #' \code{"r"}, for number-label in colorlabel, \code{"l"} means left, #' \code{"c"} means center, and \code{"r"} means right. #' #' @param cl.offset Numeric, for number-label in colorlabel, 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 p.mat Matrix of p-value, if \code{NULL}, arguments \code{sig.level}, #' \code{insig}, \code{pch}, \code{pch.col}, \code{pch.cex} is 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, and meanwhile correlation coefficients are also added on the #' rectangles. 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 lable. #' #' @return (Invisibly) returns a reordered correlation matrix. #' #' @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"), add = FALSE, col = NULL, bg = "white", title = "", is.corr = TRUE, 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.lim = 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", 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 (any(corr < cl.lim[1]) || any(corr > cl.lim[2])) { stop("color limits should cover matrix") } if (is.null(cl.lim)) { if (is.corr) { # if the matrix is expected to be a correlation matrix # it MUST be within the interval [-1,1] cl.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 corr_tmp <- corr diag(corr_tmp) <- ifelse(diag, diag(corr_tmp), NA) cl.lim <- c(min(corr_tmp, na.rm = TRUE), max(corr_tmp, na.rm = TRUE)) } } intercept <- 0 zoom <- 1 if (!is.corr) { c_max <- max(corr, na.rm = TRUE) c_min <- min(corr, na.rm = TRUE) # The following if-elseif-else code should exhaustively cover all 9 # combinations of c_min and c_max variables. Each variable can be either # zero (0), positive (+) or negative (-). # c_min c_max # 00 # -0 # +0 # -- # 0- if (c_max <= 0) { intercept <- -cl.lim[2] zoom <- 1 / (diff(cl.lim)) } # ++ # +- # 0+ else if (c_min >= 0) { intercept <- -cl.lim[1] zoom <- 1 / (diff(cl.lim)) } # -+ else { # expression from the original code as a sanity check stopifnot(c_max * c_min < 0) # newly derived expression which covers the single remainig case stopifnot(c_min < 0 && c_max > 0) intercept <- 0 zoom <- 1 / max(abs(cl.lim)) } # now, the zoom might still be Inf when cl.lim were both zero if (zoom == Inf) { stopifnot(cl.lim[1] == 0 && cl.lim[2] == 0) # check the assumption zoom <- 0 } corr <- (intercept + corr) * zoom } cl.lim2 <- (intercept + cl.lim) * zoom int <- intercept * zoom 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 ^ .75 || max(corr, na.rm = TRUE) > 1 + .Machine$double.eps ^ .75 ) { stop("The matrix is not in [-1, 1]!") } } if (is.null(col)) { col <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061"))(200) } n <- nrow(corr) m <- ncol(corr) min.nm <- min(n,m) ord <- seq_len(min.nm) if (order != "original") { ord <- corrMatOrder(corr, order = order, hclust.method = hclust.method) corr <- corr[ord, ord] } ## set up variable names if (is.null(rownames(corr))) { rownames(corr) <- seq_len(n) } if (is.null(colnames(corr))) { colnames(corr) <- seq_len(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 return(list(Pos, Dat)) } # 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) } Pos <- getPos.Dat(corr)[[1]] # 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) { newcorr <- (dat + 1) / 2 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 = "white") 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) + c(-1,0) * grepl("l", tl.pos) # margin between text and grid 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) ) + c(-0.15, 0) + c(0, -1) * (type == "upper" && tl.pos != "n") + # nasty hack c(0,1) * grepl("d", tl.pos) # margin between text and grid # note: the nasty hack above is related to multiple issues # (e.g. #96, #94, #102) 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)) } 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)) { # TODO: this expression might be confusing 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") { text(Pos[,1], Pos[,2], font = number.font, col = col.fill, labels = round((DAT - int) * ifelse(addCoefasPercent, 100, 1) / zoom, 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]] if (!method %in% c("circle", "square")) { stop("Method shoud be circle or square if drawing confidence intervals.") } 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) } } if (!is.null(p.mat) && insig != "n") { if (order != "original") { p.mat <- p.mat[ord, ord] } pos.pNew <- getPos.Dat(p.mat)[[1]] pNew <- getPos.Dat(p.mat)[[2]] 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],2), 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 = cl.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(cl.lim[1], cl.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 { 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 numbers if (!is.null(addCoef.col) && method != "number") { text(Pos[,1], Pos[,2], col = addCoef.col, labels = round((DAT - int) * ifelse(addCoefasPercent, 100, 1) / zoom, number.digits), cex = number.cex, font = number.font) } ## 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) } invisible(corr) # reordered correlation matrix } #' @note pure function #' @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) } #' @note pure function #' @noRd draw_method_color <- function(coords, fg, bg) { symbols(coords, squares = rep(1, nrow(coords)), fg = fg, bg = bg, add = TRUE, inches = FALSE) } #' @note pure function #' @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/R/corrRect.hclust.R0000644000176200001440000000302113170415272015524 0ustar liggesusers#' 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.R0000644000176200001440000000676613170415272014760 0ustar liggesusers#' 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. #' @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 (is.null(lim.segment) || 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)) switch(align, l = text(pos.xlabel, y = at, labels = labels, pos = 4, ...), r = text(xlim[2], y = at, labels = labels, pos = 2, ...), c = text((pos.xlabel + xlim[2]) / 2, y = at, labels = labels, ...), stop("programming error - should not have reached this line!") ) } } 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)) switch(align, l = text(x = at, y = pos.ylabel, labels = labels, pos = 1, ...), r = text(x = at, y = ylim[1], labels = labels, pos = 2, ...), c = text(x = at, y = (pos.ylabel + ylim[1]) / 2, labels = labels, ...), stop("programming error - should not have reached this line!") ) } } } corrplot/R/corrplot.mixed.R0000644000176200001440000000550513170415272015422 0ustar liggesusers#' 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) 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 = "white") on.exit(par(oldpar), add = TRUE) corrplot(corr, type = "upper", method = upper, diag = TRUE, tl.pos = tl.pos, plotCI = plotCI_upper, col = upper.col, mar = mar, ...) corrplot(corr, add = TRUE, type = "lower", method = lower, diag = (diag == "l"), tl.pos = "n", cl.pos = "n", plotCI = plotCI_lower, col = lower.col, mar = mar, ...) 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)) } # fixes issue #43 # return value should be the same as in the corrplot function invisible(corr) } corrplot/vignettes/0000755000176200001440000000000013171165067014135 5ustar liggesuserscorrplot/vignettes/example-corrRect.hclust.R0000644000176200001440000000164613170415272020777 0ustar liggesusersdata(mtcars) M <- cor(mtcars) corrplot(M, method = "circle", order = "FPC") corrRect(c(5,6)) (order.hc <- corrMatOrder(M, order = "hclust")) (order.hc2 <- corrMatOrder(M, order = "hclust", hclust.method = "ward")) 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", addrect = 2) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 2, method = "ward") # same as: corrplot(M, order = "hclust", hclust.method = "ward", addrect = 3) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 3, method = "ward") # same as: corrplot(M, order = "hclust", hclust.method = "ward", addrect = 4) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 4, method = "ward") corrplot/vignettes/example-corrRect.R0000644000176200001440000000164613170415272017476 0ustar liggesusersdata(mtcars) M <- cor(mtcars) corrplot(M, method = "circle", order = "FPC") corrRect(c(5,6)) (order.hc <- corrMatOrder(M, order = "hclust")) (order.hc2 <- corrMatOrder(M, order = "hclust", hclust.method = "ward")) 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", addrect = 2) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 2, method = "ward") # same as: corrplot(M, order = "hclust", hclust.method = "ward", addrect = 3) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 3, method = "ward") # same as: corrplot(M, order = "hclust", hclust.method = "ward", addrect = 4) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 4, method = "ward") corrplot/vignettes/example-corrMatOrder.R0000644000176200001440000000113513170415272020307 0ustar liggesusersM <- 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")) 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") corrplot/vignettes/example-corrplot.R0000644000176200001440000001625313170415272017557 0ustar liggesusersdata(mtcars) M <- cor(mtcars) set.seed(0) ## different color series col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white", "cyan", "#007FFF", "blue","#00007F")) col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061")) col3 <- colorRampPalette(c("red", "white", "blue")) col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F", "cyan", "#007FFF", "blue", "#00007F")) 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", col = col1(20), cl.length = 21, addCoef.col = "grey") corrplot(M, order = "AOE", col = col1(10), addCoef.col = "grey") corrplot(M, order = "AOE", col = col2(200)) corrplot(M, order = "AOE", col = col2(200), addCoef.col = "grey") corrplot(M, order = "AOE", col = col2(20), cl.length = 21, addCoef.col = "grey") corrplot(M, order = "AOE", col = col2(10), addCoef.col = "grey") corrplot(M, order = "AOE", col = col3(100)) corrplot(M, order = "AOE", col = col3(10)) corrplot(M, method = "color", col = col1(20), cl.length = 21, order = "AOE", addCoef.col = "grey") corrplot(M, method = "square", col = col2(200), order = "AOE") corrplot(M, method = "ellipse", col = col1(200), order = "AOE") corrplot(M, method = "shade", col = col3(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", cl.lim = c(0,1)) corrplot(abs(M), order = "AOE", col = col1(20), cl.lim = c(0,1)) corrplot(abs(M), order = "AOE", col = col3(200), cl.lim = c(0,1)) ## visualize a matrix in [-100, 100] ran <- round(matrix(runif(225, -100,100), 15)) corrplot(ran, is.corr = FALSE) corrplot(ran, is.corr = FALSE, cl.lim = c(-100, 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 = .2, cl.align = "l") corrplot(M, order = "AOE", cl.ratio = .2, cl.align = "c") corrplot(M, order = "AOE", cl.ratio = .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]) res1 <- cor.mtest(mtcars, conf.level = 0.95) res2 <- cor.mtest(mtcars, conf.level = 0.99) ## specialized the insignificant value according to the significant level corrplot(M, p.mat = res1$p, sig.level = 0.2) corrplot(M, p.mat = res1$p, sig.level = 0.05) corrplot(M, p.mat = res1$p, sig.level = 0.01) corrplot(M, p.mat = res1$p, insig = "blank") corrplot(M, p.mat = res1$p, insig = "p-value") corrplot(M, p.mat = res1$p, insig = "p-value", sig.level = -1) ## add all p-values corrplot(M, p.mat = res1$p, order = "hclust", insig = "blank", addrect = 3) corrplot(M, p.mat = res1$p, order = "hclust", insig = "pch", addrect = 3) ## plot confidence interval(0.95), "square" 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 interval(0.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.Rmd0000644000176200001440000003002713171156546017602 0ustar liggesusers An Introduction to **corrplot** Package ======================================= ```{r setup, include=FALSE} set.seed(0) # we need reproducible results knitr::opts_chunk$set( out.extra = 'style="display:block; margin: auto"', fig.align = "center", fig.path = "webimg/", dev = "png") ``` Introduction ------------ The **corrplot** package is a graphical display of a correlation matrix, confidence interval. 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. Visualization methods ---------------------------- There are seven visualization methods (parameter `method`) in **corrplot** package, named `"circle"`, `"square"`, `"ellipse"`, `"number"`, `"shade"`, `"color"`, `"pie"`. > Positive correlations are displayed in blue and negative correlations in red > color. Color intensity and the size of the circle are proportional to the > correlation coefficients. ```{r methods} library(corrplot) M <- cor(mtcars) corrplot(M, method = "circle") corrplot(M, method = "square") corrplot(M, method = "ellipse") corrplot(M, method = "number") # Display the correlation coefficient corrplot(M, method = "shade") corrplot(M, method = "color") corrplot(M, method = "pie") ``` Layout ----------------------------- There are three layout types (parameter `type`): - `"full"` (default) : display full **correlation matrix** - `"upper"` : display upper triangular of the **correlation matrix** - `"lower"` : display lower triangular of the **correlation matrix** ```{r layout} corrplot(M, type = "upper") corrplot(M, type = "upper") ``` `corrplot.mixed()` is a wrapped function for mixed visualization style. ```{r mixed} corrplot.mixed(M) corrplot.mixed(M, lower.col = "black", number.cex = .7) corrplot.mixed(M, lower = "ellipse", upper = "circle") corrplot.mixed(M, lower = "square", upper = "circle", tl.col = "black") ``` Reorder a correlation matrix ---------------------------- The correlation matrix can be reordered according to the correlation coefficient. This is important to identify the hidden structure and pattern in the matrix. There are four methods in corrplot (parameter `order`), named `"AOE"`, `"FPC"`, `"hclust"`, `"alphabet"`. More algorithms can be found in [seriation](cran.r-project.org/package=seriation) package. You can also reorder the matrix "manually" via function `corrMatOrder()`. - `"AOE"` is for the angular order of the eigenvectors. It is calculated from the order of the angles $a_i$, $$ a_i = \begin{cases} \tan (e_{i2}/e_{i1}), & \text{if $e_{i1}>0$;} \newline \tan (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"`, `"single"`, `"complete"`, `"average"`, `"mcquitty"`, `"median"` or `"centroid"`. - `"alphabet"` for alphabetical order. ```{r order} corrplot(M, order = "AOE") corrplot(M, order = "hclust") corrplot(M, order = "FPC") corrplot(M, order = "alphabet") ``` If using `"hclust"`, `corrplot()` can draw rectangles around the chart of corrrlation matrix based on the results of hierarchical clustering. ```{r rectangles} corrplot(M, order = "hclust", addrect = 2) corrplot(M, order = "hclust", addrect = 3) ``` ```{r hclust-lightblue} # Change background color to lightblue corrplot(M, type = "upper", order = "hclust", col = c("black", "white"), bg = "lightblue") ``` Using different color spectra ------------------------------ As shown in the above section, the color of the correlogram can be customized. The function `colorRampPalette()` is very convenient for generating color spectrum. ```{r color} col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white", "cyan", "#007FFF", "blue", "#00007F")) col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061")) col3 <- colorRampPalette(c("red", "white", "blue")) col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F", "cyan", "#007FFF", "blue", "#00007F")) whiteblack <- c("white", "black") ## using these color spectra corrplot(M, order = "hclust", addrect = 2, col = col1(100)) corrplot(M, order = "hclust", addrect = 2, col = col2(50)) corrplot(M, order = "hclust", addrect = 2, col = col3(20)) corrplot(M, order = "hclust", addrect = 2, col = col4(10)) corrplot(M, order = "hclust", addrect = 2, col = whiteblack, bg = "gold2") ``` You can also use the standard color palettes (package `grDevices`) ```{r hclust-stdcolors} corrplot(M, order = "hclust", addrect = 2, col = heat.colors(100)) corrplot(M, order = "hclust", addrect = 2, col = terrain.colors(100)) corrplot(M, order = "hclust", addrect = 2, col = cm.colors(100)) corrplot(M, order = "hclust", addrect = 2, col = gray.colors(100)) ``` Other option would be to use `RcolorBrewer` package. ```{r hclust-rcolorbrewer} library(RColorBrewer) corrplot(M, type = "upper", order = "hclust", col = brewer.pal(n = 8, name = "RdBu")) corrplot(M, type = "upper", order = "hclust", col = brewer.pal(n = 8, name = "RdYlBu")) corrplot(M, type = "upper", order = "hclust", col = brewer.pal(n = 8, name = "PuOr")) ``` Changing color and rotation of text labels and legend ----------------------------------------------------- Parameter `cl.*` is for color legend, and `tl.*` if for text legend. For the text label, `tl.col` (text label color) and `tl.srt` (text label string rotation) are used to change text colors and rotations. Here are some examples. ```{r color-label} ## remove color legend and text legend corrplot(M, order = "AOE", cl.pos = "n", tl.pos = "n") ## bottom color legend, diagonal text legend, rotate text label corrplot(M, order = "AOE", cl.pos = "b", tl.pos = "d", tl.srt = 60) ## a wider color legend with numbers right aligned corrplot(M, order = "AOE", cl.ratio = 0.2, cl.align = "r") ## text labels rotated 45 degrees corrplot(M, type = "lower", order = "hclust", tl.col = "black", tl.srt = 45) ``` Dealing with a non-correlation matrix ------------------------------------- ```{r non-corr} corrplot(abs(M),order = "AOE", col = col3(200), cl.lim = c(0, 1)) ## visualize a matrix in [-100, 100] ran <- round(matrix(runif(225, -100,100), 15)) corrplot(ran, is.corr = FALSE, method = "square") ## a beautiful color legend corrplot(ran, is.corr = FALSE, method = "ellipse", cl.lim = c(-100, 100)) ``` 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-asp} ran <- matrix(rnorm(70), ncol = 7) corrplot(ran, is.corr = FALSE, win.asp = .7, method = "circle") ``` Dealing with missing (NA) values -------------------------------- 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). ```{r NAs} M2 <- M diag(M2) = NA corrplot(M2) corrplot(M2, na.label = "o") corrplot(M2, na.label = "NA") ``` Using "plotmath" expressions in labels -------------------------------------- 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 one of the characters `":"`, `"="` or `"$"`. ```{r plotmath} M2 <- M[1:5,1:5] colnames(M2) <- c("alpha", "beta", ":alpha+beta", ":a[0]", "=a[beta]") rownames(M2) <- c("alpha", "beta", NA, "$a[0]", "$ a[beta]") corrplot(M2) ``` Combining correlogram with the significance test ------------------------------------------------ ```{r test} res1 <- cor.mtest(mtcars, conf.level = .95) res2 <- cor.mtest(mtcars, conf.level = .99) ## specialized the insignificant value according to the significant level corrplot(M, p.mat = res1$p, sig.level = .2) corrplot(M, p.mat = res1$p, sig.level = .05) corrplot(M, p.mat = res1$p, sig.level = .01) ## leave blank on no significant coefficient corrplot(M, p.mat = res1$p, insig = "blank") ## add p-values on no significant coefficient corrplot(M, p.mat = res1$p, insig = "p-value") ## add all p-values corrplot(M, p.mat = res1$p, insig = "p-value", sig.level = -1) ## add cross on no significant coefficient corrplot(M, p.mat = res1$p, order = "hclust", insig = "pch", addrect = 3) ``` Visualize confidence interval ----------------------------- ```{r ci} corrplot(M, low = res1$lowCI, upp = res1$uppCI, order = "hclust", rect.col = "navy", plotC = "rect", cl.pos = "n") corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI, order = "hclust", pch.col = "red", sig.level = 0.01, addrect = 3, rect.col = "navy", plotC = "rect", cl.pos = "n") ``` ```{r ci_with_label} res1 <- cor.mtest(mtcars, conf.level = .95) corrplot(M, p.mat = res1$p, insig = "label_sig", sig.level = c(.001, .01, .05), pch.cex = .9, pch.col = "white") corrplot(M, p.mat = res1$p, method = "color", insig = "label_sig", pch.col = "white") corrplot(M, p.mat = res1$p, method = "color", type = "upper", sig.level = c(.001, .01, .05), pch.cex = .9, insig = "label_sig", pch.col = "white", order = "AOE") corrplot(M, p.mat = res1$p, insig = "label_sig", pch.col = "white", pch = "p<.05", pch.cex = .5, order = "AOE") ``` Customize the correlogram ------------------------- ```{r pmat} # matrix of the p-value of the correlation p.mat <- cor.mtest(mtcars)$p head(p.mat[, 1:5]) # Specialized the insignificant value according to the significant level corrplot(M, type = "upper", order = "hclust", p.mat = p.mat, sig.level = 0.01) # Leave blank on no significant coefficient corrplot(M, type = "upper", order = "hclust", p.mat = p.mat, sig.level = 0.01, insig = "blank") ``` In the above figure, correlations with **p-value > 0.01** are considered as insignificant. In this case the correlation coefficient values are leaved blank or crosses are added. ```{r customized} col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA")) corrplot(M, method = "color", col = col(200), type = "upper", order = "hclust", number.cex = .7, addCoef.col = "black", # Add coefficient of correlation tl.col = "black", tl.srt = 90, # Text label color and rotation # Combine with significance p.mat = p.mat, sig.level = 0.01, insig = "blank", # hide correlation coefficient on the principal diagonal diag = FALSE) ``` **Note:** Some of the plots were taken from [this blog]. [this blog]: http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram Explore Large Feature Matrices ------------------------------ ```{r large_matrix} # generating large feature matrix (cols=features, rows=samples) num_features <- 60 # how many features num_samples <- 300 # how many samples DATASET <- matrix(runif(num_features * num_samples), nrow = num_samples, ncol = num_features) # setting some dummy names for the features e.g. f23 colnames(DATASET) <- paste0("f", 1:ncol(DATASET)) # let's make 30% of all features to be correlated with feature "f1" num_feat_corr <- num_features * .3 idx_correlated_features <- as.integer(seq(from = 1, to = num_features, length.out = num_feat_corr))[-1] for (i in idx_correlated_features) { DATASET[,i] <- DATASET[,1] + runif(num_samples) # adding some noise } corrplot(cor(DATASET), diag = FALSE, order = "FPC", tl.pos = "td", tl.cex = 0.5, method = "color", type = "upper") ``` corrplot/vignettes/example-corrplot.mixed.R0000644000176200001440000000064113170415272020656 0ustar liggesusersM <- 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-colorlegend.R0000644000176200001440000000157013170415272020204 0ustar liggesuserspar(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/MD50000644000176200001440000000365513171234600012434 0ustar liggesusersd950ec5e1665ad4fecf5237d52500c23 *DESCRIPTION abf8ab64fd2c3ad1f713f5d17ace44d6 *NAMESPACE f82a4d3961c25ee7328f71a9e65c877d *R/colorlegend.R 240cc0c842f7198fe196223fb75c10fe *R/cor-mtest.R c20babd9c89a59284efa19e3cb69edc4 *R/corrMatOrder.R 63ce2261d480ce9ddfab90a8f94ac7e6 *R/corrRect.R 3f3554a7d8e59f9ac4e57ff39941bb08 *R/corrRect.hclust.R e62135ad8e29ff8c11f9225e64025cdf *R/corrplot-package.R 95e9bea63897724a1fcd1f9e0d6a063d *R/corrplot.R b8e1c93becc8c8dbba7384a5ed1e5504 *R/corrplot.mixed.R abcc1bf093b64f9e3bc2f5b7c5963280 *build/vignette.rds 0c4ac6e644265b9d782fc5cc1ea3ec27 *inst/CITATION e84b9b3660e1b815ae4ad474746887ff *inst/NEWS 7d47249ab35ef11c5b3932c8a3a794f2 *inst/doc/corrplot-intro.R 346b3f1f8c5886bf94ec5172479ad35d *inst/doc/corrplot-intro.Rmd 49e13edd732ffdfe44c414b0a3cbece2 *inst/doc/corrplot-intro.html 1c7c1ecc1faa92ab2df4adbbd08c9578 *man/colorlegend.Rd 237d503a32aadde9cb47e15a98901ada *man/cor.mtest.Rd 45f16b7b58cd03c95ee924d93ae3dcca *man/corrMatOrder.Rd a1747a3469766f08acc3f592e504d071 *man/corrRect.Rd af3008ea9839ed97e656ee4d6e330bc3 *man/corrRect.hclust.Rd bac43ecba33b26d9717efe5c753f2068 *man/corrplot-package.Rd 7a00b2cd51912f2664560b3749b96a0b *man/corrplot.Rd 1330cc4c7a859bcbcb663bab26fb1dac *man/corrplot.mixed.Rd 54775cdb9c7717be24118eda97d9d35e *tests/testthat.R ea615464ff55cd4a224ff929b9dee940 *tests/testthat/test-colorlegend.R 7be5610f5814490fee7c78264f2ed779 *tests/testthat/test-cor-mtest.R 29738d39ad84d5f86ff2b38de3c292bf *tests/testthat/test-corrplot.R 346b3f1f8c5886bf94ec5172479ad35d *vignettes/corrplot-intro.Rmd fe32a6e300326ebd97f39c61499d3d29 *vignettes/example-colorlegend.R a8fd0b04b2e83ae15838107f93bd598e *vignettes/example-corrMatOrder.R 48dff9a6b15705f1ab4c5d1955f357de *vignettes/example-corrRect.R 48dff9a6b15705f1ab4c5d1955f357de *vignettes/example-corrRect.hclust.R e10dc9081c4d81036d9a0038256ff2a4 *vignettes/example-corrplot.R b6d2696b045effbdda7a9bee8183d565 *vignettes/example-corrplot.mixed.R corrplot/build/0000755000176200001440000000000013171165067013224 5ustar liggesuserscorrplot/build/vignette.rds0000644000176200001440000000034513171165067015565 0ustar liggesusers‹mQÍ‚0 ?þ@bBävó$ï`BHŒC Suggests: knitr, RColorBrewer, testthat Description: A graphical display of a correlation matrix 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. License: GPL URL: https://github.com/taiyun/corrplot BugReports: https://github.com/taiyun/corrplot/issues VignetteBuilder: knitr RoxygenNote: 6.0.1 NeedsCompilation: no Packaged: 2017-10-16 17:19:52 UTC; cloud Author: Taiyun Wei [cre, aut], Viliam Simko [aut], Michael Levy [ctb], Yihui Xie [ctb], Yan Jin [ctb], Jeff Zemla [ctb] Repository: CRAN Date/Publication: 2017-10-16 22:58:08 UTC corrplot/man/0000755000176200001440000000000013170415272012673 5ustar liggesuserscorrplot/man/cor.mtest.Rd0000644000176200001440000000210013170415272015071 0ustar liggesusers% 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{FxS}, with \code{F} columns that represent features and \code{S} rows that represent samples.} \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/man/corrplot.mixed.Rd0000644000176200001440000000413013170415272016131 0ustar liggesusers% 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/corrRect.hclust.Rd0000644000176200001440000000421213170415272016245 0ustar liggesusers% 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, method = "circle", order = "FPC") corrRect(c(5,6)) (order.hc <- corrMatOrder(M, order = "hclust")) (order.hc2 <- corrMatOrder(M, order = "hclust", hclust.method = "ward")) 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", addrect = 2) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 2, method = "ward") # same as: corrplot(M, order = "hclust", hclust.method = "ward", addrect = 3) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 3, method = "ward") # same as: corrplot(M, order = "hclust", hclust.method = "ward", addrect = 4) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 4, method = "ward") } \author{ Taiyun Wei } \keyword{hplot} corrplot/man/corrplot-package.Rd0000644000176200001440000000226413170415272016423 0ustar liggesusers% 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/corrMatOrder.Rd0000644000176200001440000000442413170415272015571 0ustar liggesusers% 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 = tan (e_{i2} / e_{i1}), if e_{i1} > 0} \deqn{ a_i = tan (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")) 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") } \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/colorlegend.Rd0000644000176200001440000000430713170415272015463 0ustar liggesusers% 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.} \item{addlabels}{Logical, whether add text label or not.} \item{\dots}{Additional arguments, passed to \code{\link{plot}}} } \description{ Draw color legend. } \examples{ 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.Rd0000644000176200001440000004512713170415272015037 0ustar liggesusers% 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"), add = FALSE, col = NULL, bg = "white", title = "", is.corr = TRUE, 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.lim = 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", 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{add}{Logical, if \code{TRUE}, the graph is added to an existing plot, otherwise a new plot is created.} \item{col}{Vector, the color of glyphs. It is distributed uniformly in \code{cl.lim}. If NULL, \code{col} will be \code{colorRampPalette(col2)(200)}, see example about col2.} \item{bg}{The background color.} \item{title}{Character, title of the graph.} \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{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{"d"} means diagonal, \code{"n"} means don't add textlabel.} \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 labels; 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 colorlabel.} \item{cl.lim}{The limits \code{(x1, x2)} in the colorlabel.} \item{cl.length}{Integer, the number of number-text in colorlabel, 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, cex of number-label in colorlabel, passed to \code{\link{colorlegend}}.} \item{cl.ratio}{Numeric, to justify the width of colorlabel, 0.1~0.2 is suggested.} \item{cl.align.text}{Character, \code{"l"}, \code{"c"} (default) or \code{"r"}, for number-label in colorlabel, \code{"l"} means left, \code{"c"} means center, and \code{"r"} means right.} \item{cl.offset}{Numeric, for number-label in colorlabel, 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{p.mat}{Matrix of p-value, if \code{NULL}, arguments \code{sig.level}, \code{insig}, \code{pch}, \code{pch.col}, \code{pch.cex} is 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, and meanwhile correlation coefficients are also added on the rectangles. 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 lable.} } \value{ (Invisibly) returns a reordered correlation matrix. } \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 col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white", "cyan", "#007FFF", "blue","#00007F")) col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061")) col3 <- colorRampPalette(c("red", "white", "blue")) col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F", "cyan", "#007FFF", "blue", "#00007F")) 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", col = col1(20), cl.length = 21, addCoef.col = "grey") corrplot(M, order = "AOE", col = col1(10), addCoef.col = "grey") corrplot(M, order = "AOE", col = col2(200)) corrplot(M, order = "AOE", col = col2(200), addCoef.col = "grey") corrplot(M, order = "AOE", col = col2(20), cl.length = 21, addCoef.col = "grey") corrplot(M, order = "AOE", col = col2(10), addCoef.col = "grey") corrplot(M, order = "AOE", col = col3(100)) corrplot(M, order = "AOE", col = col3(10)) corrplot(M, method = "color", col = col1(20), cl.length = 21, order = "AOE", addCoef.col = "grey") corrplot(M, method = "square", col = col2(200), order = "AOE") corrplot(M, method = "ellipse", col = col1(200), order = "AOE") corrplot(M, method = "shade", col = col3(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", cl.lim = c(0,1)) corrplot(abs(M), order = "AOE", col = col1(20), cl.lim = c(0,1)) corrplot(abs(M), order = "AOE", col = col3(200), cl.lim = c(0,1)) ## visualize a matrix in [-100, 100] ran <- round(matrix(runif(225, -100,100), 15)) corrplot(ran, is.corr = FALSE) corrplot(ran, is.corr = FALSE, cl.lim = c(-100, 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 = .2, cl.align = "l") corrplot(M, order = "AOE", cl.ratio = .2, cl.align = "c") corrplot(M, order = "AOE", cl.ratio = .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]) res1 <- cor.mtest(mtcars, conf.level = 0.95) res2 <- cor.mtest(mtcars, conf.level = 0.99) ## specialized the insignificant value according to the significant level corrplot(M, p.mat = res1$p, sig.level = 0.2) corrplot(M, p.mat = res1$p, sig.level = 0.05) corrplot(M, p.mat = res1$p, sig.level = 0.01) corrplot(M, p.mat = res1$p, insig = "blank") corrplot(M, p.mat = res1$p, insig = "p-value") corrplot(M, p.mat = res1$p, insig = "p-value", sig.level = -1) ## add all p-values corrplot(M, p.mat = res1$p, order = "hclust", insig = "blank", addrect = 3) corrplot(M, p.mat = res1$p, order = "hclust", insig = "pch", addrect = 3) ## plot confidence interval(0.95), "square" 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 interval(0.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.Rd0000644000176200001440000000316513170415272014752 0ustar liggesusers% 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(clus, col = "black", lwd = 2) } \arguments{ \item{clus}{Vector, the number of each cluster's members.} \item{col}{Color of rectangles.} \item{lwd}{Line width of rectangles.} } \description{ Draw rectangle(s) around the chart of corrrlation matrix. } \details{ \code{corrRect} needs the number(parameter \code{clus}) of each cluster's members, while \code{corrRect.hclust} can get the members in each cluster based on hierarchical clustering (\code{\link{hclust}}). } \examples{ data(mtcars) M <- cor(mtcars) corrplot(M, method = "circle", order = "FPC") corrRect(c(5,6)) (order.hc <- corrMatOrder(M, order = "hclust")) (order.hc2 <- corrMatOrder(M, order = "hclust", hclust.method = "ward")) 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", addrect = 2) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 2, method = "ward") # same as: corrplot(M, order = "hclust", hclust.method = "ward", addrect = 3) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 3, method = "ward") # same as: corrplot(M, order = "hclust", hclust.method = "ward", addrect = 4) corrplot(M.hc2) corrRect.hclust(M.hc2, k = 4, method = "ward") } \author{ Taiyun Wei } \keyword{hplot}