2
library("lme4")
data(sleepstudy)             

fit1 <- lmer(Reaction ~ Days + (1|Subject), sleepstudy) 

To visualise the random effect,

library(sjPlot)
plot_model(fit1,type = "re",facet.grid = FALSE) 

In my orginal data, I have three random groups. However, if I want to plot the random effects, they all come in three separate plots. How can I put them in all single plot in 1 X 3 panel or 3 X 1 panel.

89_Simple
  • 3,393
  • 3
  • 39
  • 94

1 Answers1

3

You could use gridExtra::grid.arrange()

fit1 <- lmer(Reaction ~ (1|Days) + (1|Subject), sleepstudy) 

library(sjPlot)
p <- plot_model(fit1, type = "re", facet.grid=FALSE) 

library(gridExtra)
grid.arrange(p[[1]], p[[2]])

Produces:

enter image description here

You also could consider lattice::qqmath().

library(lattice)
p2 <- qqmath(ranef(fit1, condVar=TRUE))
grid.arrange(p2[[1]], p2[[2]])

Produces:

enter image description here

Note: To specify the columns use the ncol option. Compare e.g. grid.arrange(p2[[1]], p2[[2]], ncol=2) vs. grid.arrange(p2[[1]], p2[[2]], ncol=1).

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 1
    There is also a small wrapper around `grid.arrange()` in sjPlot: `plot_grid()`. So, `plot_model(fit1,type = "re") %>% plot_grid()` results in the same output. – Daniel Jun 19 '18 at 12:14