0

I would like to draw a correlation matrix graph, similar to what is output with corrplot.

However, I estimated my correlation matrix using a multivariate mixed model with MCMCglmm (because I wanted to account for a few fixed effects and some missing data points) so I would like to be able to input my own correlation matrix estimates (and associated confidence intervals) for corrplot to use. As far as I can tell, corrplot estimates the correlation matrix for you from your data - which I do not want it to do.

As an alternative to corrplot, I know I can build a correlation heatmap using ggplot and geom_tile. But I really like the 'ellipse' option that comes with corrplot.

library(reshape2)
library(corrplot)
library(ggplot2)

#my correlation matrix looks something like this
car.cor <- cor(mtcars)
melted_cormat <- melt(car.cor)
melted_cormat$upper <- melted_cormat$value+0.10
melted_cormat$lower <- melted_cormat$value-0.10

# with corrplot
car.cor <- cor(mtcars)
corrplot.mixed(car.cor, upper = "ellipse")

# with ggplot and geom_tile
ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) + 
  geom_tile()

So my questions are - how can I input my own correlation matrix for corrplot to use, or alternatively, how can I get the nice little ellipses using ggplot?

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
klaskowski
  • 11
  • 3
  • 1
    This is the line that computes the correlation matrix: `car.cor <- cor(mtcars)`. It is not computed in the function. Use the `lowCI.mat` and `uppCI.mat` arguments to give your own confidence intervals.. – Axeman Jan 22 '18 at 13:58
  • Could you give me some more details on what you mean? `corrplot` gives me an error if I do not first compute the correlation matrix with `cor` – klaskowski Jan 22 '18 at 15:02
  • 1
    No I can't, because you haven't given us a good minimal reproducible example of what you actually want to do. So I can't know what is causing your error. See: [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269) We'd need to see what kind of object you are trying to give to `corrplot`. – Axeman Jan 22 '18 at 15:03
  • Apologies, I should have been more clear. I was using the mtcars data as my example. I simply meant that if you run the code without first estimating the correlation matrix as: `corrplot.mixed(mtcars, upper = "ellipse")` then the following error is returned: `Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, length of 'dimnames' [2] not equal to array extent` But in any case, the code below looks like what I need. – klaskowski Jan 23 '18 at 06:30
  • Sure but you say you have correlations already, not a normal data.frame. But I can't tell what exactly you do have. – Axeman Jan 23 '18 at 06:32
  • Ah, gotcha. I edited my question above. – klaskowski Jan 23 '18 at 06:39

1 Answers1

1

EDIT

Given your data

library(tidyverse)
library(reshape2)
library(corrplot)
library(magrittr)

car.cor <- cor(mtcars)
melted_cormat <- melt(car.cor)
melted_cormat$upper <- melted_cormat$value + 0.10
melted_cormat$lower <- melted_cormat$value - 0.10

you could do the following to generate a list of three matrices to be able to use corrplot

M <- melted_cormat %>% 
  gather(key, r, 3:5) %>% 
  spread(key = Var2, value = r) %>% 
  split(.$key) %>%
  map(., .f = set_rownames, value = NULL) %>%
  map(., .f = column_to_rownames, var = 'Var1') %>%
  map(., .f = select, -key) %>% 
  map(., as.matrix)

Now you can call corrplot with argument upp and low.

corrplot(M$value, low = M$lower, upp = M$upper, method = "ellipse")

enter image description here

It seems that this method does not display the confidence intervals. Compare this to the following plot.

corrplot(M$value, low = M$lower, upp = M$upper)

enter image description here

After some experimentation (n = 1) with the function corrplot.mixed things got out of hand

corrplot.mixed(corr = M$value, lower = "number", upper = "ellipse", low = M$lower, upp = M$upper)

enter image description here

I hope this helps.

markus
  • 25,843
  • 5
  • 39
  • 58