1

I'd like to combine multiple effect plots in one window with the effects package, but don't know if there is an easy way to do so.

Here's an example that doesn't work:

d1 <-data.frame(x1=rnorm(100,0:10),y1=rnorm(100,0:10),x2=rnorm(100,0:10),y2=rnorm(100,0:10))
require(effects)
require(gridExtra)
plot1 <- plot(allEffects(mod=lm(y1~x1,d1)))
plot2 <- plot(allEffects(mod=lm(y2~x2,d1)))
grid.arrange(plot1,plot2,ncol=2)
Undo
  • 25,519
  • 37
  • 106
  • 129
polcommie
  • 13
  • 4
  • 1
    `plot` uses base graphics. `gridExtra` functions work with `grid` graphics. The `gridBase` package is one way of getting the two different systems to speak. See http://stackoverflow.com/questions/14999802/how-to-use-r-base-plots-in-grid-newpage/15039605#15039605 for example – mnel May 08 '13 at 06:26
  • I also thought (incorrectly) that thsi was a base graphics problem, but it's not. – IRTFM May 08 '13 at 08:01

2 Answers2

2

I think you need to collect the values of allEffects components and then plot them as an 'efflist'. It looked to me that the plotting was base-graphics, but it is in fact 'lattice' if you follow the class-function trail (or if you read: ?plot.efflist )

Try this:

ef1 <-allEffects(mod=lm(y1~x1,d1))[[1]]
ef2 <- allEffects(mod=lm(y2~x2,d1))[[1]]
elist <- list( ef1, ef2 )
class(elist) <- "efflist"
plot(elist, col=2)

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • if, for whatever reason, one really wanted to use `grid.arrange()` here, it is possible to use `grid.grabExpr()` to first capture the output and store it as a grob. – baptiste May 24 '13 at 19:07
2

Interestingly, the result from plotting an efflist (which is the result from allEffects) is not a lattice graphic; it instead builds up a multipanel graphic of lattice graphics using the print.lattice methods. However, if you plot the individual effects, either by taking the elements from allEffects or by using effect, then you do get lattice graphics.

Either like this

p1 <- plot(allEffects(m1)[[1]])
p2 <- plot(allEffects(m2)[[1]])

or like this.

p1 <- plot(effect("x1", m1))
p2 <- plot(effect("x2", m2))

These can be combined with grid.arrange; the catch is that their class is c("plot.eff", "trellis") which grid.arrange doesn't recognize, so they have to be made into simple trellis objects first.

class(p1) <- class(p2) <- "trellis"
grid.arrange(p1, p2, ncol=2)
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142