简体   繁体   English

R返回corrplot作为对象

[英]R return corrplot as object

corrplot plots a correlation matrix, but it does not return a graphical object (grob) corrplot绘制相关矩阵,但它不返回图形对象(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. 对于普通的情节,我会用grid.arrangegridExtra包。 However since corrplot only prints and does not return an object, I can't see how to do this. 但是由于corrplot只打印并且没有返回对象,我无法看到如何执行此操作。

Is there a workaround or a better alternative to corrplot ? 对于corrplot有解决方法还是更好的替代方案?

The recent gridGraphics package could probably do what you asked: return the plot as a grob. 最近的gridGraphics包可能会按照您的要求执行:将绘图作为gridGraphics返回。

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)

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. 有旧备用par(mfrow=c(x, y))其中x是你想要绘制的行数, y是列数。 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

Not sure if I got your question right, but maybe what you are looking for is simple layout ? 不确定我的问题是否正确,但也许您正在寻找的是简单的layout

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

layout(matrix(1:2))
corrplot(cor(mat))
corrplot(cor(mat))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM