3

plot.lm has a nice feature of displaying plots one after another, so when specifying

plot(lm(rnorm(100) ~ rnorm(100, 3, 1)))

displays first plot and asks user to

Hit Return to see next plot:

Now I want to generate 30 plots, so displaying them in a grid will make them hard to read, while specifying them one after another is quite cumbersome. I've been wondering if there is a function or a method to imitate plot.lm behaviour? I'm specifically interested in a function that is compatible with ggplot2.

Cœur
  • 37,241
  • 25
  • 195
  • 267
jakes
  • 1,964
  • 3
  • 18
  • 50

1 Answers1

7

Study stats:::plot.lm. It uses devAskNewPage.

Example:

devAskNewPage(TRUE)
for (i in 1:3) plot(i)
devAskNewPage(options("device.ask.default")[[1]])
Roland
  • 127,288
  • 10
  • 191
  • 288
  • 2
    Or `par(ask = TRUE)` – Axeman Jul 17 '18 at 10:56
  • `devAskNewPage(options("device.ask.default"))` doesn't seem to work fine (Error in devAskNewPage(options("device.ask.default")) : incorrect argument 'ask'). Is it safe to use something like `devAskNewPage(FALSE)`? Also, should it work ok with `ggplot2`? – jakes Jul 17 '18 at 11:46
  • Thanks. Do you know any alternative that works with `ggplot2`? – jakes Jul 17 '18 at 15:27
  • 3
    @jakes ; what is wrong with this answer as it seems to extend to `ggplot` okay; change the relevant bit of Roland's code to `for (i in 1:3) print(qplot(main=i))` – user20650 Nov 21 '18 at 19:51
  • @user20650, well, it seems that `print` was necessary here. Could you explain why? What's the diference between `plot` and `qplot` that causes it? – jakes Nov 21 '18 at 21:06
  • 1
    @jakes: https://stackoverflow.com/questions/2547306/generate-multiple-graphics-from-within-an-r-function ; https://stackoverflow.com/questions/21264324/plot-with-ggplot-in-for-loop-doesnt-work ; https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f – user20650 Nov 21 '18 at 21:13