17  Using sjPlot to plot models

An alternative tool for making plots of various models is to use the sjPlot package. Below are some examples of how to use sjPlot to visualize mixed-effects models fitted with lme4.

We first fit a few models to some fake data:

head( df )
# A tibble: 6 × 7
  cluster treatment    cov1 female  SPED Essay      Y
  <fct>       <int>   <dbl> <fct>  <int> <dbl>  <dbl>
1 1               0 -0.376  1          0  80.5 -0.366
2 1               1 -0.562  1          0  76.2  0.995
3 1               0 -0.344  0          0  74.3 -0.516
4 1               1  0.0905 0          0  73.9 -0.420
5 1               1  1.60   1          0  82.9  3.40 
6 1               0 -0.0886 1          0  60.0  1.87 
m1 <- lmer(Y ~ treatment + (1 | cluster), data = df)
m2 <- lmer(Y ~ treatment + cov1 + (1 | cluster), data = df)
m3 <- lmer(Y ~ treatment * cov1 + (1 | cluster), data = df)

We next look at some of the characteristics of these models and data, using sjPlot. We load the library:

library(sjPlot)

Now off we go!

17.1 Frequency Plot for Binary Variable

plot_frq(df$SPED)

17.2 Boxplot by Group

sjPlot has some general data viz stuff:

plot_grpfrq(var.cnt = df$Essay, var.grp = df$female, type = "box")
Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
ℹ Please use tidy evaluation idioms with `aes()`.
ℹ See also `vignette("ggplot2-in-packages")` for more information.
ℹ The deprecated feature was likely used in the sjPlot package.
  Please report the issue at <https://github.com/strengejacke/sjPlot/issues>.
Ignoring unknown labels:
• colour : "female"
Warning in rq.fit.br(wx, wy, tau = tau, ...): Solution may be nonunique
Warning in rq.fit.br(wx, wy, tau = tau, ...): Solution may be nonunique
Warning in rq.fit.br(wx, wy, tau = tau, ...): Solution may be nonunique
Warning in rq.fit.br(wx, wy, tau = tau, ...): Solution may be nonunique
Warning in rq.fit.br(wx, wy, tau = tau, ...): Solution may be nonunique
Warning in rq.fit.br(wx, wy, tau = tau, ...): Solution may be nonunique

17.3 Fixed Effects Plot

Coefficent plots (see prior chapter):

plot_model(m3, type = "est")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
ℹ The deprecated feature was likely used in the sjPlot package.
  Please report the issue at <https://github.com/strengejacke/sjPlot/issues>.

17.4 Marginal Effects (Main Effects)

plot_model(m2, type = "eff", terms = c("cov1", "treatment"))

17.5 Interaction Effects Plot

plot_model(m3, type = "int", terms = c("cov1", "treatment"))

17.6 Random Slopes Plot

plot_model(m3, type = "re", sort.est = TRUE) 
Sorting each group of random effects ('sort.all') is not possible when 'facets = TRUE'.

17.7 Predicted Values by Covariate

plot_model(m3, type = "pred", terms = "cov1")

17.8 ggeffects Plot of Marginal Effects

plot(ggpredict(m3, terms = c("cov1", "treatment")))