R return corrplot as object
Asked Answered
C

3

8

corrplot plots a correlation matrix, but it does not return a graphical object (grob)

I would like to plot several correlation matrices on a single page. For normal plots, I would use grid.arrange from the gridExtra package. However since corrplot only prints and does not return an object, I can't see how to do this.

Is there a workaround or a better alternative to corrplot ?

Conchita answered 13/1, 2015 at 18:56 Comment(0)
F
8

There's the old standby par(mfrow=c(x, y)) where x is the number of rows you wish to plot and y the numberof columns. It then posts across and then down as you call the plots.

par(mfrow = c(2, 2))
corrplot(cor(mat1))
corrplot(cor(mat2))
corrplot(cor(mat3))
corrplot(cor(mat4))

par(mfrow = c(1, 1)) #To clear layout

Will plot as

Mat1 | Mat2
-----------
Mat3 | Mat4
Falk answered 13/1, 2015 at 20:15 Comment(0)
D
11

The recent gridGraphics package could probably do what you asked: return the plot as a grob.

mat <- matrix(rnorm(100), ncol=10)
library(corrplot)
corrplot(cor(mat))

library(gridGraphics)
grab_grob <- function(){
  grid.echo()
  grid.grab()
}

g <- grab_grob()
library(gridExtra)
grid.newpage()
grid.arrange(g,g,g,g)
Durable answered 14/1, 2015 at 17:17 Comment(0)
F
8

There's the old standby par(mfrow=c(x, y)) where x is the number of rows you wish to plot and y the numberof columns. It then posts across and then down as you call the plots.

par(mfrow = c(2, 2))
corrplot(cor(mat1))
corrplot(cor(mat2))
corrplot(cor(mat3))
corrplot(cor(mat4))

par(mfrow = c(1, 1)) #To clear layout

Will plot as

Mat1 | Mat2
-----------
Mat3 | Mat4
Falk answered 13/1, 2015 at 20:15 Comment(0)
I
2

Not sure if I got your question right, but maybe what you are looking for is simple layout ?

mat <- matrix(rnorm(100), ncol=10)

layout(matrix(1:2))
corrplot(cor(mat))
corrplot(cor(mat))
Indefatigable answered 13/1, 2015 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.