在同一张图中绘制多个 corrplots (R)

Plotting multiple corrplots (R) in the same graph

是否可以在一张图中绘制多个 corrplot 图?

可重现的例子:

library(corrplot)
data(mtcars)
M <- cor(mtcars)

col1 <- colorRampPalette(c("#7F0000","red","#FF7F00","yellow","white", "cyan", "#007FFF", "blue","#00007F"))

corrplot(M, method="color", col=col1(20), cl.length=21,order = "AOE", addCoef.col="grey")
corrplot(M, method="square", col=col1(200),order = "AOE")
corrplot(M, method="ellipse", col=col1(200),order = "AOE")
corrplot(M, method="shade", col=col1(20),order = "AOE")

我想知道是否有类似 "Multiple plot function" 的功能适合这个目的。我希望所有这些 corrplots 都在同一个输出中。

当然是。只需在脚本中使用 par(mfrow=c(2,2))

library(corrplot)
data(mtcars)
M <- cor(mtcars)

col1 <- colorRampPalette(c("#7F0000","red","#FF7F00","yellow","white", "cyan", "#007FFF", "blue","#00007F"))

#this is my only addition
par(mfrow=c(2,2))

corrplot(M, method="color", col=col1(20), cl.length=21,order = "AOE", addCoef.col="grey")
corrplot(M, method="square", col=col1(200),order = "AOE")
corrplot(M, method="ellipse", col=col1(200),order = "AOE")
corrplot(M, method="shade", col=col1(20),order = "AOE")

大功告成:

请注意,在 par(mfrow=c(2,2)) 中,第一个 2 代表行,第二个 2 代表列。您可以将其更改为您喜欢的任何内容。我选择像这样显示您的 4 个图表,因为它看起来不错,但是您可以说,如果您愿意,可以使用 par(mfrow=c(1,4))(1 行,4 列)将它们全部放在一行中。