Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate multiple graphics from within an R function

Tags:

r

r-faq

ggplot2

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?

like image 932
William Doane Avatar asked Mar 30 '10 17:03

William Doane


People also ask

How do I plot multiple curves in R?

To draw multiple curves in one plot, different functions are created separately and the curve() function is called repeatedly for each curve function. The call for every other curve() function except for the first one should have added an attribute set to TRUE so that multiple curves can be added to the same plot.

What is par function in R?

R par() function R programming has a lot of graphical parameters which control the way our graphs are displayed. The par() function helps us in setting or inquiring about these parameters. For example, you can look at all the parameters and their value by calling the function without any argument.

What is Mfrow R?

The mfrow() parameter allows to split the screen in several panels. Subsequent charts will be drawn in panels. You have to provide a vector of length 2 to mfrow() : number of rows and number of columns.


1 Answers

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.

like image 121
rcs Avatar answered Oct 29 '22 17:10

rcs