taiyun / corrplot

A visual exploratory tool on correlation matrix
https://github.com/taiyun/corrplot
Other
316 stars 86 forks source link

Plotting multiple corrplots on same page #144

Closed cwilso6 closed 3 years ago

cwilso6 commented 5 years ago

I would like to be able to plot multiple corrplots on the same page. Is there a recommended way to plot them using ggpubr or gridExtra packages, preferably ggpubr. Below is an example:

library(ggpubr)
data(mtcars)
M <- cor(mtcars)
set.seed(0)

##  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
plot1=corrplot(M, method = "number", col = "black", cl.pos = "n", title='cutoff')

ggarrange(plot1,plot1)

Which produces the following error:

Warning messages:
1: In as_grob.default(plot) :
  Cannot convert object of class matrix into a grob.
2: In as_grob.default(plot) :
  Cannot convert object of class matrix into a grob.

Again, I want to emphasize that my main goal is to use ggpubr or gridExtra

cwilso6 commented 5 years ago

Is it possible to have both the matrix and the plot be output from corrplot? As of now, when I assign the plot to a variable it is a matrix. If I could access the plot object, then I think would able to use the desired packages.

caijun commented 4 years ago

You can try my R package ggcorrplot2, which currently implemented a subset of features of corrplot using ggplot2. Thus, you can combine multiple corrplots into one plot using cowplot or patchwork. See the following figure.

image

jukicivan commented 4 years ago

@caijun Your package looks great. However, for some reason, I can't install it. I tried various things but I keep getting "not available for 3.6.1..."

caijun commented 4 years ago

I haven't released ggcorrplot2 on CRAN, thus you are unable to install ggcorrplot2 via install.packages("ggcorrplot2") . Try to install ggcorrplot2 from github:

if (!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("caijun/ggcorrplot2")
jukicivan commented 4 years ago

Yes, of course, I realised that. I tried through install_github but then I stumbled upon api problem. This is apparently related to my corporate computer because it now worked on my private mac. However, I still have some issues as it seems that ggcorrplot cant recognise some of the functions that it contains. For example, title, hc.order or lab.

Error in ggcorrplot(cor2, type = "upper", p.mat = p.mat2, hc.order = TRUE, : unused arguments (hc.order = TRUE, title = "Females")

caijun commented 4 years ago

ggcorrplot2 currently has implemented only a subset of features that corrplot has. The parameters hc.order and title are not supported yet. Please see the document of ggcorrplot().

ErickChacon commented 2 years ago

With patchwork and gridGraphics you can plot multiple corrplots easily with the function wrap_elements. You can do something like:

p1 = wrap_elements(~corrplot(M, method = "number", col = "black", cl.pos = "n", title='cutoff'))
p2 = wrap_elements(~corrplot(M))

p1 + p2

See further details at https://patchwork.data-imaginist.com/articles/guides/assembly.html.