Search code examples
rlme4sjplot

Plotting multiple random effects in single plot mixed models


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.


Solution

  • 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).