Sarah Chisholm
Sarah Chisholm

Reputation: 11

sjPlot::plot_model - plot a 3-way interaction in a 2x2 panel figure and change the labels of fixed effects

I have a GLMM with a three-way interaction (see 'm' below). The interaction term includes one continuous (A) and two categorical (B &C) fixed factors. Factors B and C each have 2 levels, 0 & 1. The model also includes two random factors (a & b) and a spatial covariance term to account for spatial autocorrelation:

library(glmmTMB)

df$pos <- numFactor(x = df$LONG, y = df$LAT)
df$group <- factor(1)    

m = glmmTMB(y ~ A * B * C + (1|a) + (1|b) + exp(0 + pos|group), data = df, REML = TRUE) 

I'd like to produce a figure that has four panels (2x2), where the continuous fixed effect (A) is plotted along the x-axes of all four panels, the two levels of the first categorial fixed effect (B) are plotted in two columns, and the two levels of the second categorial fixed effect (C) are plotted in two rows. I.e. I'd like the panels to display the interactions of A x B:0 x C:0 (top right), A x B:1 x C:0 (top left), A x B0 x C1 (bottom right), and A x B:1 x C:1 (bottom left).

I've used the following code to plot this interaction using the sjPlot::plot_model function:

library(sjPlot)
library(ggplot2)

# Simplify plot aesthetics
fig <- theme_bw() + theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank(), panel.background=element_blank()) + 
  theme(strip.background=element_blank(), strip.text.y = element_text()) + theme(legend.background=element_blank()) + 
  theme(legend.key=element_blank()) + theme(panel.border = element_rect(colour="black", fill=NA))

plot_model(m, type = "pred", terms =c("A","B","C"),
           show.data = TRUE,
           title = "")+ 
           fig

This produces a 1x2 panel figure, where the two levels of C are plotted in separate columns, and the two levels of B are plotted as two separate slopes in each panel (follow link below to see image):

plot_model output

Q1: How can I modify my plot_model code to produce the 2x2 plot described above?

Q2: I'd also like to change the labels for the two levels of each each categorical factor, i.e. I'd like to display text instead of simply '0' and '1'. I know this can be done by making this change in the dataframe itself and re-running the model, but this isn't realistic for me to do for a few reasons.

Thank you.

Upvotes: 1

Views: 555

Answers (1)

Pablo Bernabeu
Pablo Bernabeu

Reputation: 262

Q1 is hard to answer without a minimal, reproducible example of the model. Q2 can be addressed using a custom function called alias_interaction_plot that is described at https://pablobernabeu.github.io/2022/plotting-two-way-interactions-from-mixed-effects-models-using-alias-variables/

Upvotes: 0

Related Questions