1

I got a meaningful four-way interaction to plot using sjPlot in R, here is an example:

colnames(iris) <- c("y", "a", "b", "c", "d")

m0 <- lm(y ~ a * b * c * d, data = iris)

sjPlot::plot_model(m0, type = "pred", terms = c("a", "b", "c", "d")) 

Which plots: enter image description here I want to stack them vertically by d.

sjpPlot is ggplot2 based, but it does not simply take ggplot commends. Does anyone know how to do it?

Juan_814
  • 51
  • 8

2 Answers2

3

From the image of your plot (which is already a multi-panel figure) I would guess that you have installed the see package, i.e. if not you will get a list of single plots and the warning:

Package see needed to plot multiple panels in one integrated figure. Please install it by typing install.packages("see", dependencies = TRUE) into the console.

will pop up.

If see is installed sjPlot::plot_model will return a multi-panel plot which uses patchwork under the hood, i.e. the returned object is no longer a list of ggplots but a patchwork object instead. (As a consequence applying patchwork::wrap_plots as suggested in the (now deleted) answer by @jared_mamrot will not have any effect.)

Instead, for this case you could set the number of columns via patchwork::plot_layout.

Note: My guess is that there is or should be an argument to control which type of object is returned or the number of columns in case of multi-panel plots. Unfortunately I wasn't able to find something on that in the documentation. (:

library(patchwork)
require(see)

p <- sjPlot::plot_model(m0, type = "pred", terms = c("a", "b", "c", "d")) 

p +
  plot_layout(ncol = 1)

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • @jared_mamrot IMHO you could still undelete your answer as it provides a solution for the non-"see" case. To make it more "complete" I would probably add some more tweaks like getting rid of the duplicated titles and collecting the guides. Overall personally I would prefer having the option to get a list of plots which I can tweak more easily than manipulating a patchwork object. – stefan Apr 20 '23 at 07:43
2

I think @stefan's answer is the best solution to the problem, however, if you don't have the "see" package installed (the function warns you if this is the case), plot_model() generates a list of plots instead of a single figure. A potential solution for this scenario is to use the patchwork library, which accepts a list of plots as input, e.g.

library(sjPlot)
library(ggplot2)
library(patchwork)

colnames(iris) <- c("y", "a", "b", "c", "d")
  
m0 <- lm(y ~ a * b * c * d, data = iris)
  
list_of_plots <- plot_model(m0, type = "pred", terms = c("a", "b", "c", "d"))
#> Warning: Package `see` needed to plot multiple panels in one integrated figure.
#>   Please install it by typing `install.packages("see", dependencies =
#>   TRUE)` into the console.

# You can edit the plots to suit, e.g. remove titles from plots 2 and 3
list_of_plots[[2]] <- list_of_plots[[2]] + labs(title = "")
list_of_plots[[3]] <- list_of_plots[[3]] + labs(title = "")

wrap_plots(list_of_plots, ncol = 1, guides = "collect")

Created on 2023-04-20 with reprex v2.0.2

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46