从 R 函数中生成多个图形 [英] Generate multiple graphics from within an R function

查看:32
本文介绍了从 R 函数中生成多个图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ggplot 图形从 R 中的函数中生成多个图形窗口...

I'd like to spawn several graphics windows from within a function in R using ggplot graphics...

testf <- function(a, b) {
  devAskNewPage(TRUE)
  qplot(a, b);
  # grid.newpage(recording = TRUE)
  dev.new()
  qplot(a, a+a);
  # grid.newpage(recording = TRUE)
  dev.new()
  qplot(b, b+b);
}

library(ggplot2)

x <- rnorm(50)
y <- rnorm(50)
testf(x, y)

然而,dev.new() 和 grid.newpage() 似乎都没有刷新前面的图.

However, neither dev.new() nor grid.newpage() seems to flush the preceding plot.

我知道,在 R 中,函数通常只产生它们评估的最后一件事,但我想更好地理解这个过程并了解任何可能的解决方法.

I know that, in R, functions normally only produce the last thing they evaluate, but I'd like to understand the process better and to learn of any possible workarounds.

想法?

推荐答案

lattice 和 ggplot2 中基于 grid 的图形函数创建图形对象,但不显示它.图形对象的 print() 方法产生实际的显示,即,

The grid-based graphics functions in lattice and ggplot2 create a graph object, but do not display it. The print() method for the graph object produces the actual display, i.e.,

print(qplot(x, y))

解决问题.

参见 R常见问题 7.22.

这篇关于从 R 函数中生成多个图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆