grid.arrange with John Fox's effects plots

末鹿安然 提交于 2019-12-23 01:58:42

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!