【问题标题】:R Multiples plot in a single varibleR单个变量中的多个图
【发布时间】:2020-05-04 21:12:42
【问题描述】:

嗨,我有下一个代码:

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)........)

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

谢谢

【问题讨论】:

  • “单图”是什么意思?就外部过程而言,三重图被视为一个单一的图,其中编码了线/点/...用于横向分离的多个数据。此外,通常基本图形不存储在变量中(与基于ggplot2 的图不同,例如),那么一旦将这个三重图存储在其中,您打算/需要对figure1 做什么?
  • figure1 &lt;- function() {par(); plot(); ...}; figure1()

标签: r plot


【解决方案1】:

您可以使用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)是figure1 属于"recordedplot" 类,而base-R grDevices:::print.recordedplot S3 方法直接调用replayPlot

【讨论】:

    猜你喜欢
    • 2013-07-07
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多