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

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

问题描述

  testf< 

我想从R函数中使用ggplot graphics生成几个图形窗口。 ; - 函数(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()似乎都不会刷新



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



想法?

解决方案

>格和ggplot2中的基于图形函数创建一个图形对象,但不显示它。图对象的 print()方法产生实际的显示,即

  print(qplot(x,y))

解决了这个问题。



请参阅 R FAQ 7.22


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)

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

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.

Thoughts?

解决方案

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

solves the problem.

See R FAQ 7.22.

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

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