Search code examples
rggplot2plotcorrelation

How to plot a 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:

enter image description here

How can I achieve that?


Solution

  • 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