Plot correlation matrix into a graph
I have a matrix with some correlation values. Now I want to plot that in a graph that looks more or less like that:
How can I achieve that?
Rather "less" look like, but worth checking (as giving more visual information):
Correlation matrix ellipses: Correlation matrix circles:
Please find more examples in the corrplot vignette referenced by @assylias below.
Quick, dirty, and in the ballpark:
library(lattice)
#Build the horizontal and vertical axis information
hor <- c("214", "215", "216", "224", "211", "212", "213", "223", "226", "225")
ver <- paste("DM1-", hor, sep="")
#Build the fake correlation matrix
nrowcol <- length(ver)
cor <- matrix(runif(nrowcol*nrowcol, min=0.4), nrow=nrowcol, ncol=nrowcol, dimnames = list(hor, ver))
for (i in 1:nrowcol) cor[i,i] = 1
#Build the plot
rgb.palette <- colorRampPalette(c("blue", "yellow"), space = "rgb")
levelplot(cor, main="stage 12-14 array correlation matrix", xlab="", ylab="", col.regions=rgb.palette(120), cuts=100, at=seq(0,1,0.01))
Very easy with lattice::levelplot:
z <- cor(mtcars)
require(lattice)
levelplot(z)
The ggplot2 library can handle this with geom_tile()
. It looks like there may have been some rescaling done in that plot above as there aren't any negative correlations, so take that into consideration with your data. Using the mtcars
dataset:
library(ggplot2)
library(reshape)
z <- cor(mtcars)
z.m <- melt(z)
ggplot(z.m, aes(X1, X2, fill = value)) + geom_tile() +
scale_fill_gradient(low = "blue", high = "yellow")
EDIT:
ggplot(z.m, aes(X1, X2, fill = value)) + geom_tile() +
scale_fill_gradient2(low = "blue", high = "yellow")
allows to specify the colour of the midpoint and it defaults to white so may be a nice adjustment here. Other options can be found on the ggplot website here and here.