首页 > 解决方案 > 单个变量中的 R 倍数图

问题描述

嗨,我有下一个代码:

par(mfrow=c(1,3))
plot(BCxyz[,1], BCxyz[,2], main="Bray-Curtis 1:2", pch=20, cex = 3, col=c("blue", "green", "red", "yellow")[Metadata$SampleType])
plot(BCxyz[,1], BCxyz[,3], main="Bray-Curtis 1:3", pch=20, cex = 3, col=c("blue", "green", "red", "yellow")[Metadata$SampleType])
plot(BCxyz[,2], BCxyz[,3], main="Bray-Curtis 2:3", pch=20, cex = 3, col=c("blue", "green", "red", "yellow")[Metadata$SampleType])

通过这种方式,我得到一个包含 3 个图的图形,所以我只想将图形(其中包含 3 个图)添加到一个变量中,例如:

figure1 <- (mfrow=c(1,3)........)

每次我调用figure1时,在一个图中打开3个情节!!!!

谢谢

标签: rplot

解决方案


您可以使用recordPlot保存当前绘图并在以后调用它。

par(mfrow=c(1,3))
plot(1) ; plot(2); plot(3)
figure1 <- recordPlot()
# view then close the plot window, just to prove that redrawing it works

figure1             # redraws it when interactive on the console
replayPlot(figure1) # same thing
print(figure1)      # indirect, calls replayPlot

最后两个命令在控制台上具有相同的结果,但如果您要以编程方式“重放”绘图(例如,在{...}代码块或函数中),您应该replayPlot直接使用该函数。figure1仅在控制台上单独工作的原因(否printreplayPlot)是figure1class "recordedplot",并且 base-R grDevices:::print.recordedplotS3 方法直接调用replayPlot


推荐阅读