rggplot2sjplot

How to vertically stack panels of sjPlot in R?


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?


Solution

  • 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