Learn R Programming

corrplot (version 0.54)

corrplot: A visualization of a correlation matrix.

Description

A graphical display of a correlation matrix, confidence interval. The details of graph are paid great attention to. corrplot.circle, corrplot.square, corrplot.ellipse, corrplot.number, corrplot.pie, corrplot.shade, and corrplot.color are convenience wrappers of corrplot that take the same arguments as the corresponding option it replaces such as circle, ellipse etc.

Usage

corrplot(corr,
		method = c("circle", "square", "ellipse", "number", "shade", "color", "pie"),
		type = c("full", "lower", "upper"), add = FALSE,
		col = NULL, assign.col = c("-1to1","min2max","0to1"),
		bg = "white", title = "",
		diag = TRUE, outline = FALSE, mar = c(0,0,0,0),
		addgrid.col = "gray", addCoef.col= NULL,

		order = c("original", "PCA", "hclust", "alphabet"),
		hclust.method = c("complete", "ward", "single", "average",
							"mcquitty", "median", "centroid"),
		addrect = NULL, rect.col="black", rect.lwd = 2,

		addtextlabel = NULL, tl.cex = 1,
		tl.col = "red", tl.offset = 0.4, tl.srt = 90,

		addcolorlabel = c("right", "bottom","no"), cl.range=NULL,
		cl.length=11, cl.cex =0.8, cl.ratio = 0.15, cl.align.text="c",cl.offset=0.5,

		addshade = c("negative", "positive", "all"),
		shade.lwd = 1, shade.col = "white",

		p.mat = NULL, sig.level = 0.05,
		insig = c("pch","blank", "no"),
		pch = 4, pch.col = "black", pch.cex = 3,
		
		plotCI = c("no","square", "circle", "rect"),
		lowCI.mat = NULL, uppCI.mat = NULL, ...)
	
corrplot.circle(corr, ...)
corrplot.square(corr, ...)
corrplot.ellipse(corr,...)
corrplot.number(corr, ...)
corrplot.shade(corr, ...)
corrplot.pie(corr, ...)
corrplot.color(corr, ...)

Arguments

corr
the correlation matrix, must be square if order is not "original".
method
character, the visualization method of correlation matrix to be used. Currently, it supports seven methods, named "circle" (default), "square", "ellipse", "number", "pie", "shade"
type
character, "full" (default), "upper" or "lower", display full matrix, lower triangular or upper triangular of it.
add
logical, if add is TRUE, the graph are added to an existing plot, otherwise a new plot is created.
col
vector, the color of glyphs for correlation coefficients or confidence interval's bounds, and it is distributed uniformly according "assign.col" parameter.
assign.col
character, "-1to1" (default), "min2max" or "0to1", the method of how to assign colors to the elements in the input matrix. See details in the examples.
bg
the background color.
title
character, title of the graph.
diag
logical, whether display the correlation coefficients on the principal diagonal.
outline
logical, whether plot outline of circles, square and ellipse.
mar
see par.
addgrid.col
the color of grid, if NULL, don't add grid.
addCoef.col
color of coefficients added on the glyph circle, square, etc. if NULL (default), add no coefficients. Note: the figures are expressed in terms of a percentage for spacesaving.
order
character, the ordering method of the correlation matrix.
  • "original"for original order.
  • "alphabet"for alphabetical order.
  • "PCA"for principal component analysis.
  • "hclust"
hclust.method
character, the agglomeration method to be used when order is hclust. This should be one of "ward", "single", "complete", "average", "mcq
addrect
integer, the number of rectangles draws on the graph according to the hierarchical cluster, only valid when order is hclust. If its value is NULL (default), then add no rectangles.
rect.col
color for rectangle border(s), only valid when addrect is not NULL.
rect.lwd
numeric, line width for borders for rectangle border(s), only valid when addrect is not NULL.
addtextlabel
character, "lt", "ld", "td", or "d", giving position of text labels, "lt" means left and top, "ld" means left and diagonal(near), "td" means top and diagonal(ne
tl.cex
numeric, for the text label (variable names).
tl.col
the color of text label.
tl.offset
numeric, for text label, see text.
tl.srt
numeric, for text label string rotation in degrees, see text.
addcolorlabel
character, "right" (default), "bottom" or "no", where to draw colorlabel. If "no", don't draw colorlabel.
cl.range
character, "-1to1", "min2max", or "0to1" style of colorlabel. The default value is the same with "assign.col". See details in the examples.
cl.length
integer, the number of number-text in colorlabel, passed to colorlegend.
cl.cex
cex of number-label in colorlabel, passed to colorlegend.
cl.ratio
numeric, to justify the width of colorlabel, 0.1~0.2 is suggested.
cl.align.text
character, "l", "c" (default) or "r", for number-label in colorlabel, "l" means left, "c" means center, and "r" means right.
cl.offset
numeric, for number-label in colorlabel, see text.
addshade
character for shade style, "negative", "positive" or "all", only valid when method is "shade". If "all", all correlation coefficients' glyph will be shaded; if "positiv
shade.lwd
numeric, the line width of shade.
shade.col
the color of shade line.
p.mat
matrix of p-value, if NULL, arguments sig.level, insig, pch, pch.col, pch.cex is invalid.
sig.level
significant level, if the p-value in p-mat is bigger than sig.level, then the corresponding correlation coefficient is regarded as insignificant.
insig
character, specialized insignificant correlation coefficients, "pch" (default), "blank" or "no". If "blank", wipe away the corresponding glyphs; if "pch", add characters (see pch
pch
add character on the glyphs of insignificant correlation coefficients(only valid when insig is "pch"). See par.
pch.col
the color of pch (only valid when insig is "pch").
pch.cex
the cex of pch (only valid when insig is "pch").
plotCI
character, method to plot confidence interval, if "no", don't plot confidence interval. If "rect", plot rectangles whose upper side means upper bound, lower side means lower bound, and correlation coefficients are also added
lowCI.mat
matrix of the lower bound of confidence interval.
uppCI.mat
matrix of the upper bound of confidence interval.
...
additional arguments for corrplot's wrappers (corrplot.circle, corrplot.square, etc.), passed to corrplot.

Value

  • (Invisibly) returns a single permutation vector.

Details

corrplot function offers flexible ways to visualize correlation matrix, lower and upper bound of confidence interval matrix.

References

Michael Friendly (2002). Corrgrams: Exploratory displays for correlation matrices. The American Statistician, 56, 316--324. D.J. Murdoch, E.D. Chow (1996). A graphical display of large correlation matrices. The American Statistician, 50, 178--180.

See Also

The plotcorr function in the ellipse package and corrgram function in the corrgram package has some similarities. Package seriation offered more methods to reorder matrices, such as ARSA, BBURCG, BBWRCG, MDS, TSP, Chen and so forth.

Examples

Run this code
data(mtcars)
corr <- cor(mtcars)
par(ask = TRUE)

## different methods to display corr-matrix
corrplot(corr, method="number", col="black", addcolorlabel="no")
corrplot(corr, method="number")
corrplot(corr)
corrplot(corr, order ="PCA")
corrplot(corr, method="color", order = "PCA", addCoef.col=grey(0.1))
corrplot(corr, method="square", order = "PCA")
corrplot(corr, method="ellipse", order = "PCA")
corrplot(corr, method="shade", order = "PCA")
corrplot(corr, method="pie", order = "PCA")


## mixed methods
corrplot(corr,order="PCA",type="upper",addtextlabel="no")
corrplot(corr,add=TRUE, type="lower",
	method="ell",order="PCA",addtextlabel="d")

corrplot(corr,order="PCA",type="upper",addtextlabel="no")
corrplot(corr,add=TRUE, type="lower",
	method="square",order="PCA",addtextlabel="d")


## order is hclust and draw rectangles
corrplot(corr, order="hclust")
corrplot(corr, order="hclust", addrect = 2)
corrplot(corr, order="hclust", addrect = 3, rect.col = "red")
corrplot(corr, order="hclust", addrect = 4, rect.col = "blue")
corrplot(corr, order="hclust", hclust.method="ward", addrect = 4)

##  color of matrix
col1 <- colorRampPalette(rev(c("#00007F", "blue", "#007FFF", "cyan",
            "white", "yellow", "#FF7F00", "red", "#7F0000")))
col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7",
			"#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061"))	
col3 <- colorRampPalette(c("red", "white", "blue"))			
wb <- c("white","black")
## col1 
corrplot(corr, order="PCA", col=col1(200))
corrplot(corr, order="PCA", col=col1(20), cl.length=21)
corrplot(corr, order="PCA", col=col1(10))
corrplot(corr, order="PCA", col=col1(8))
## col2
corrplot(corr, order="PCA", col=col2(200))
corrplot(corr, order="PCA", col=col2(10))
corrplot(corr, order="PCA", col=col2(8))
## col3
corrplot(corr, order="PCA", col=col3(100))
corrplot(corr, order="PCA", col=col3(10))
## col=wb
corrplot(corr, col = wb, order="PCA", outline=TRUE, addcolorlabel="no")
## like Chinese wiqi, suit for either on screen or white-black print.
corrplot(corr, col = wb, bg="gold2",  order="PCA", addcolorlabel="no")


## visualize a similarrity matrix in [0, 1]
corrplot(abs(corr),order="PCA",assign.col="0to1")
corrplot(abs(corr),order="PCA",assign.col="0to1", col=col1(20)[11:20])
corrplot(abs(corr),order="PCA",assign.col="0to1", col=col3(200)[101:200])

## text-labels and plot type
corrplot(corr, order="PCA", tl.srt=45)
corrplot(corr, order="PCA", tl.srt=60)
corrplot(corr, order="PCA", addtextlabel="d",addcolorlabel="no")
corrplot(corr, order="PCA", diag=FALSE, addtextlabel="d")
corrplot(corr, order="PCA", diag=FALSE, addtextlabel="d",cl.range="min2max")
corrplot(corr, order="PCA", type="upper")
corrplot(corr, order="PCA", type="upper", diag=FALSE)
corrplot(corr, order="PCA", type="lower", addcolorlabel="b")
corrplot(corr, order="PCA", type="lower", addcolorlabel="b", diag=FALSE)



#### color-legend
corrplot(corr, order="PCA", cl.ratio=0.2, cl.align="l")
corrplot(corr, order="PCA", cl.ratio=0.2, cl.align="c")
corrplot(corr, order="PCA", cl.ratio=0.2, cl.align="r")
corrplot(corr, order="PCA", addcolorlabel="bottom")
corrplot(corr, order="PCA", addcolorlabel="bottom", cl.range="min2max", addtextlabel="d")
corrplot(corr, order="PCA", addcolorlabel="no")



## addshade, addgrid and addCoef
corrplot.shade(corr, order="PCA", addshade = "negative", addgrid.col=NULL)
corrplot.shade(corr, order="PCA", addshade = "positive", addgrid.col=NULL)
corrplot.shade(corr, order="PCA", addshade = "all", addgrid.col=NULL)
corrplot.color(corr, order="PCA", addgrid.col=NULL)
corrplot.color(corr, order="PCA", addgrid.col="white")
corrplot.color(corr, order="PCA", addgrid.col="white", addCoef.col="grey10")


##the input matrix is not square
corrplot(corr[1:8,])
corrplot(corr[,1:8])



cor.mtest <- function(mat, conf.level = 0.95){
	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(mat[,i], mat[,j], conf.level = conf.level)
			p.mat[i,j] <- p.mat[j,i] <- tmp$p.value
			lowCI.mat[i,j] <- lowCI.mat[j,i] <- tmp$conf.int[1]
			uppCI.mat[i,j] <- uppCI.mat[j,i] <- tmp$conf.int[2]
		}
	}
	return(list(p.mat, lowCI.mat, uppCI.mat))
}

res1 <- cor.mtest(mtcars,0.95)
res2 <- cor.mtest(mtcars,0.99)


## specialized the insignificant value according to the significant level
corrplot(corr, p.mat = res1[[1]], sig.level=0.2)
corrplot(corr, p.mat = res1[[1]], sig.level=0.05)
corrplot(corr, p.mat = res1[[1]], sig.level=0.01)
corrplot(corr, p.mat = res1[[1]], insig = "blank")
corrplot(corr, p.mat = res1[[1]], order="hclust", insig = "blank", addrect=3)
corrplot(corr, p.mat = res1[[1]], order="hclust", insig = "pch", addrect=3)



## plot confidence interval(0.95), "square" method	
corrplot(corr,low=res1[[2]], upp=res1[[3]],
	plotC="circle", addg="grey20",addc="no")
corrplot(corr, p.mat = res1[[1]],low=res1[[2]], upp=res1[[3]],
	plotC="circle", addg="grey20",addc="no")
corrplot(corr, low=res1[[2]], upp=res1[[3]], 
	col=c("white","black"),bg="gold2",order="PCA",
	plotCI="circle",addc="no",pch.col="red")
corrplot(corr, p.mat = res1[[1]], low=res1[[2]], upp=res1[[3]], 
	col=c("white","black"),bg="gold2",order="PCA",
	plotCI="circle",addc="no",pch.col="red")
	
## plot confidence interval(0.95), "square" method	
corrplot(corr, low=res1[[2]], upp=res1[[3]],
	col=c("white","black"),bg="gold2", order="PCA",
	plotCI="square",addg=NULL,addc="no")
corrplot(corr, p.mat = res1[[1]],low=res1[[2]], upp=res1[[3]],
	col=c("white","black"),bg="gold2",order="PCA",pch.col="red", 
	plotC="square", addg=NULL,addc="no")
	
## plot confidence interval(0.95, 0.95, 0.99), "rect" method
corrplot(corr, low=res1[[2]], upp=res1[[3]], order="hclust",
	rect.col="navy", plotC="rect",addc="no")
corrplot(corr, p.mat = res1[[1]], low=res1[[2]], upp=res1[[3]], order="hclust",
	pch.col="red", sig.level = 0.05, addrect=3, rect.col="navy",
	plotC="rect",addc="no")
corrplot(corr, p.mat = res2[[1]], low=res2[[2]], upp=res2[[3]], order="hclust",
	pch.col="red", sig.level = 0.01, addrect=3, rect.col="navy",
	plotC="rect",addc="no")


## an animation of changing confidence interval in different significance level
par(ask=FALSE)
for(i in seq(0.1, 0.001, -0.001)){
	tmp <- cor.mtest(mtcars,1-i)
	corrplot(corr, p.mat = tmp[[1]], low=tmp[[2]], upp=tmp[[3]], order="hclust",
		pch.col="red", sig.level = i, plotC="rect", addc="no",
		mar=c(0,0,1,0), 
		title=substitute(alpha == x,list(x=format(i,digits=3,nsmall=3))))
	Sys.sleep(0.01)
}

Run the code above in your browser using DataLab