96

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?

0

13 Answers 13

60

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

Correlation matrix ellipses: Correlation matrix ellipses Correlation matrix circles: Correlation matrix circles

Please find more examples in the corrplot vignette referenced by @assylias below.

5
58

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

4
  • 2
    It looks very similar to example from OP (fonts, colors, layout). Looks like original was created with lattice too. Great detailed answer, +1.
    – Marek
    Mar 28, 2011 at 5:16
  • 2
    Thank you for the answer. Many people are used to correlation plots in which the diagonal containing 1-s runs from the top left to the bottom right square (see the example figure in the question), rather than from the bottom left to the top right square, as in your solution. Here's how to fix this problem: cor_reversed <- apply(cor, 2, rev); levelplot(t(cor_reversed),...)
    – skip
    Jan 11, 2016 at 14:47
  • @bill_080 why copy-pasting your code wont print the correlation matrix? Jan 9, 2017 at 22:09
  • @Pavlos When I copy/paste the code, it provides the same basic chart above.
    – bill_080
    Oct 15, 2021 at 13:21
43

Very easy with lattice::levelplot:

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

enter image description here

31

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.

4
  • nice (+1)! Though I would add a manual-break scale (e.g: c(-1, -0.6, -0.3, 0, 0.3, 0.6, 1)) with "white" in the middle to let the colors reflect the symmetry of the correlation efficient.
    – daroczig
    Mar 28, 2011 at 0:33
  • @Daroczig - Good point. It looks like scale_fill_gradient2() achieves the functionality you describe automatically. I didn't know that existed.
    – Chase
    Mar 28, 2011 at 1:52
  • 1
    adding to this: p <- ggplot(.....) + ... + ....; library(plotly); ggplotly(p) will make it interactive
    – schlusie
    Jul 18, 2016 at 15:47
  • To make the diagonal 1's go from top left to bottom right, reversal of factor levels is required for X1 using: z.m$X1 <- factor(z.m$X1, levels = rev(levels( z.m$X1 )))
    – arun
    Nov 4, 2016 at 4:19
12

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

9

That type of graph is called a "heat map" among other terms. Once you've got your correlation matrix, plot it using one of the various tutorials out there.

Using base graphics: http://flowingdata.com/2010/01/21/how-to-make-a-heatmap-a-quick-and-easy-solution/

Using ggplot2: http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/

2
  • I'm not sure if calling it a 'heatmap' is a fairly modern invention. It seems to make sense if you are trying to show 'hotspots' by using a red-orange-yellow colour scheme, but in general its just an image plot, or a matrix plot, or a raster plot. I'll be interested to find the oldest reference that calls it a 'heatmap'. tldr; "[citation needed]"
    – Spacedman
    Mar 28, 2011 at 7:04
  • I think you're right that heat map isn't necessarily the earliest name for it. Wikipedia lists a 1957 paper, but I checked that paper and the term "heat map" appears nowhere in it (nor do the graphics look exactly like the current form). Mar 28, 2011 at 11:48
5

I have been working on something similar to the visualization posted by @daroczig, with code posted by @Ulrik using the plotcorr() function of the ellipse package. I like the use of ellipses to represent correlations, and the use of colors to represent negative and positive correlation. However, I wanted the eye-catching colors to stand out for correlations close to 1 and -1, not for those close to 0.

I created an alternative in which white ellipses are overlaid on colored circles. Each white ellipse is sized so that the proportion of the colored circle visible behind it is equal to the squared correlation. When the correlation is near 1 and -1, the white ellipse is small, and much of the colored circle is visible. When the correlation is near 0, the white ellipse is large, and little of the colored circle is visible.

The function, plotcor(), is available at https://github.com/JVAdams/jvamisc/blob/master/R/plotcor.r.

An example of the resulting plot using the mtcars dataset is shown below.

library(plotrix)
library(seriation)
library(MASS)
plotcor(cor(mtcars), mar=c(0.1, 4, 4, 0.1))

result of call to plotcor() function

3

The corrplot() function from corrplot R package can be also used to plot a correlogram.

library(corrplot)  
M<-cor(mtcars) # compute correlation matrix
corrplot(M, method="circle")

several articles describing how to compute and visualize correlation matrix are published here:

3

I realise that it's been a while, but new readers might be interested in rplot() from the corrr package (https://cran.rstudio.com/web/packages/corrr/index.html), which can produce the sorts of plots @daroczig mentions, but design for a data pipeline approach:

install.packages("corrr")
library(corrr)
mtcars %>% correlate() %>% rplot()

enter image description here

mtcars %>% correlate() %>% rearrange() %>% rplot()

enter image description here

mtcars %>% correlate() %>% rearrange() %>% rplot(shape = 15)

enter image description here

mtcars %>% correlate() %>% rearrange() %>% shave() %>% rplot(shape = 15)

enter image description here

mtcars %>% correlate() %>% rearrange(absolute = FALSE) %>% rplot(shape = 15)

enter image description here

1

Another solution I recently learned about is an interactive heatmap created with the qtlcharts package.

install.packages("qtlcharts")
library(qtlcharts)
iplotCorr(mat=mtcars, group=mtcars$cyl, reorder=TRUE)

Below is a static image of the resulting plot. enter image description here

You can see the interactive version on my blog. Hover over the heatmap to see the row, column, and cell values. Click on a cell to see a scatterplot with symbols colored by group (in this example, the number of cylinders, 4 is red, 6 is green, and 8 is blue). Hovering over the points in the scatterplot gives the name of the row (in this case the make of the car).

1

Another option is using the GGally package with ggcorr function like this:

library(GGally)
ggcorr(mtcars, method = c("everything", "pearson"), label = TRUE)

ggcorr(mtcars, method = c("everything", "pearson"), label = TRUE, geom = "circle")

Created on 2022-08-20 with reprex v2.0.2

Check the links above for a lot of more options.

0

Since I cannot comment, I have to give my 2c to the answer by daroczig as an anwser...

The ellipse scatter plot is indeed from the ellipse package and generated with:

corr.mtcars <- cor(mtcars)
ord <- order(corr.mtcars[1,])
xc <- corr.mtcars[ord, ord]
colors <- c("#A50F15","#DE2D26","#FB6A4A","#FCAE91","#FEE5D9","white",
            "#EFF3FF","#BDD7E7","#6BAED6","#3182BD","#08519C")   
plotcorr(xc, col=colors[5*xc + 6])

(from the man page)

The corrplot package may also - as suggested - be useful with pretty images found here

0

This is a textbook example for a hierarchical clustering heatmap (with dendrogram). Using gplots heatmap.2 because it's superior to the base heatmap, but the idea is the same. colorRampPalette helps generating 50 (transitional) colors.

library(gplots)

heatmap.2(cor(mtcars), trace="none", col=colorRampPalette(c("blue2","white","red3"))(50))

heatmap.2 correlation values blue white red

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.