2

I run a loop and I need to store some plots in a list. Sometimes they are of different types, because I need to insert dummy plots when I cannot compute the actual plots.

Why is it that base plots cannot be stored in a list?

library(effects)
df=data.frame(response=sample(10,10),predictor=c(1:10))
model0=lm(response~predictor,df)
plot.list=list()
plot.list[[1]]=plot(Effect("predictor",model0))
plot.list[[2]]=plot(predictor~response,df)
plot.list
kdarras
  • 389
  • 1
  • 5
  • 16
  • 4
    Because in base graphics (as opposed to ggplot2 or lattice) you draw directly on an active device. e.g. see [here](http://stackoverflow.com/q/29583849/324364). – joran Aug 16 '16 at 15:01
  • alright, thank you. I could not find that in my searches, you could mark my question as a duplicate. – kdarras Aug 16 '16 at 15:08
  • @kdarras check out `ggplot2` - the graph object is a list, works with grid arrange and can be stored in a list – Chris Aug 16 '16 at 15:32
  • I work a lot with ggplot, but some plots (qPlot, Cook's distance plots, standardized residual plots... are better handled by something else than ggplot) – kdarras Aug 16 '16 at 15:35

1 Answers1

0

try storing functions

x <- list()
x[[1]] <- function(){plot(1)}
x[[2]] <- function(){plot(2)}

then

x[[1]]()
hedgedandlevered
  • 2,314
  • 2
  • 25
  • 54
  • thanks, that works but effectively changes the structure of my list, making it less obvious to work with the different plot types inside my list. for example, grid.arrange does not work with those stored functions. – kdarras Aug 16 '16 at 15:13