Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
869 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...