Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot correlation matrix into a graph

People also ask

How do you graph a correlation matrix?

Plotting Correlation Matrix First, find the correlation between each variable available in the dataframe using the corr() method. The corr() method will give a matrix with the correlation values between each variable. What is this? Now, set the background gradient for the correlation data.

How do you show correlation on a graph?

Such a graphical representation is called a scatterplot. A scatterplot shows the relationship between two quantitative variables measured for the same individuals. The values of one variable appear on the horizontal axis, and the values of the other variable appear on the vertical axis.

How do you visualize a large correlation matrix?

You can use qgraph to visualize a correlation matrix as a network. This will plot variables as nodes and correlations as edges connecting the nodes. Green edges indicate positive correlations and red edges indicate negative correlations. The wider and more saturated the edges the stronger the absolute correlation.


Rather "less" look like, but worth checking (as giving more visual information):

Correlation matrix ellipses: Correlation matrix ellipsesCorrelation matrix circles: 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))

enter image description here


Very easy with lattice::levelplot:

z <- cor(mtcars)
require(lattice)
levelplot(z)

enter image description here


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

enter image description here

EDIT:

ggplot(z.m, aes(X1, X2, fill = value)) + geom_tile() + 
scale_fill_gradient2(low = "blue",  high = "yellow")

enter image description here

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.


Use the corrplot package:

library(corrplot)
data(mtcars)
M <- cor(mtcars)
##  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", addcolorlabel="no")
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")

if(TRUE){

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, addcolorlabel="no")
## like Chinese wiqi, suit for either on screen or white-black print.
corrplot(M, col = wb, bg="gold2",  order="AOE", addcolorlabel="no")
}

For example:

enter image description here

Rather elegant IMO