【问题标题】:R plot without showing the graphic window不显示图形窗口的 R 绘图
【发布时间】:2014-05-20 18:48:17
【问题描述】:

我需要将绘图对象存储在变量中。我知道我能做到:

plot(rnorm(10))
obj = recordPlot()
replayPlot(obj)

但我不想显示图形窗口。所以我正在尝试这样做,但直到现在都没有成功。

win.metafile()
plot(rnorm(10))
obj = recordPlot()
dev.off()
replayPlot(obj) # it shows a null plot

嗯,可能是因为当我在做obj = recordPlot() 时,情节还没有准备好。

【问题讨论】:

  • 我无法重现该问题。打电话给replayPlot(obj) 对我来说情节很好。
  • 你可以用ggplot2做到这一点。
  • 如果您告诉我们为什么需要将绘图存储在变量中,也许我们可以提供更好的帮助。
  • Mattrition,确实是第一个代码。但在第二个中,它显示了一个空图。

标签: r plot


【解决方案1】:

来自?recordPlot

The displaylist can be turned on and off using dev.control. 
Initially recording is on for screen devices, and off for print devices.

所以如果要记录写入文件的情节,则需要打开显示列表:

win.metafile()
dev.control('enable') # enable display list
plot(rnorm(10))
obj = recordPlot()
dev.off()
replayPlot(obj)

【讨论】:

    【解决方案2】:

    您可以使用ggplot2 轻松做到这一点:

    require(ggplot2)
    data = data.frame(x = 1:100, y = rnorm(100))
    p = ggplot(data) + geom_point(aes(x, y)) + theme_classic()
    print(p) # this show the plot
    

    【讨论】:

    • 谢谢费尔南多。有效。但我目前有很多地块,我需要保持标准地块。但是,一旦我得到,我将开始将所有情节转换为 ggplot :)
    • 很容易让它看起来像一个标准情节,只需使用?theme查看示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 2014-05-06
    • 2018-06-28
    • 1970-01-01
    • 2018-03-14
    • 2015-12-17
    相关资源
    最近更新 更多