问题
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)
回答1:
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)

回答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)
来源:https://stackoverflow.com/questions/16433966/grid-arrange-with-john-foxs-effects-plots