Combining Multiple ggpredict Plots in One Using R and patchwork Package

Combining Multiple ggpredict Plots in One

When working with linear mixed effects models, it’s common to want to visualize the predictions made by the model. The ggpredict function from the broom package is a convenient tool for this purpose. However, when you have multiple variables that you’d like to predict, using ggpredict separately for each one can become cumbersome.

In this article, we’ll explore how to combine multiple ggpredict plots into a single figure, making it easier to compare the predictions made by your model for different input variables.

Introduction to ggpredict

Before we dive into combining multiple plots, let’s take a brief look at what ggpredict does. The broom package provides a suite of functions that help with modeling, including ggplot2 integration. ggpredict is one such function that generates a residual plot for a linear mixed effects model.

When you call ggpredict(m1, "variable"), it returns a dataframe containing the predicted values and residuals for the specified variable. The resulting plot shows the relationship between the response variable and the predictor(s).

Using ggplot2 to Create Plots

The patchwork package is an extension of the popular grid.arrange function in gridGraphics. It provides a more elegant way to arrange multiple plots into a single figure.

To use patchwork, you need to first install it using:

install.packages("patchwork")

Once installed, you can create a multi-panel plot using the pipe operator (|) like so:

library(patchwork)
p1 | p2 | p3

However, this example doesn’t show how to actually combine plots. We’ll get to that later.

Combining Multiple ggpredict Plots

One way to combine multiple ggpredict plots is by using the patchwork package to arrange them in a multi-panel figure.

Let’s consider our original problem again:

Suppose we have three linear mixed effects models (m1, m2, and m3) with different input variables. We can use ggpredict to create a plot for each model like this:

p1 <- ggpredict(m1, "variable1")
p2 <- ggpredict(m1, "variable2")
p3 <- ggpredict(m1, "variable3")

However, we’d like to display all three plots in one figure. To do that, we can use the patchwork package as follows:

library(patchwork)
plot(p1, main = "Prediction for variable 1", 
     subtitle = "Mixed effects model with lmer")
plot(p2, main = "Prediction for variable 2", 
     subtitle = "Mixed effects model with lmer")
plot(p3, main = "Prediction for variable 3", 
     subtitle = "Mixed effects model with lmer")

p1|p2|p3

This code creates three separate plots using ggpredict and then combines them into a single figure using the pipe operator (|). The resulting plot will have multiple panels, each containing one of our desired prediction plots.

However, this approach has some limitations. For instance, if we wanted to add additional features to the plots (like grid lines or labels), we’d need to duplicate that code for each individual plot.

A better approach is to create a single dataframe with all three predictions and then use patchwork to arrange them into a multi-panel figure. Here’s how:

library(patchwork)
p1 <- ggpredict(m1, "variable1")
p2 <- ggpredict(m1, "variable2")
p3 <- ggpredict(m1, "variable3")

df <- data.frame(
  variable = c("Variable 1", "Variable 2", "Variable 3"),
  variable1 = p1$fit.values,
  variable2 = p2$fit.values,
  variable3 = p3$fit.values
)

plot(df, main = "Prediction for Multiple Variables",
     type = "multiline")
p1|p2|p3

This code first generates three ggpredict objects and stores them in a dataframe. It then passes that dataframe to the plot() function from patchwork, which arranges the plots into a single figure with multiple panels.

Customizing the Plot

One of the greatest advantages of using patchwork is its flexibility when it comes to customizing the plot. Let’s consider how we could add additional features like labels, grid lines, and colors to our plot.

For instance, if we wanted to add a color gradient to each panel, we can use the scale_color_brewer() function from ggplot2. Here’s an example:

library(patchwork)
p1 <- ggpredict(m1, "variable1")
p2 <- ggpredict(m1, "variable2")
p3 <- ggpredict(m1, "variable3")

df <- data.frame(
  variable = c("Variable 1", "Variable 2", "Variable 3"),
  variable1 = p1$fit.values,
  variable2 = p2$fit.values,
  variable3 = p3$fit.values
)

plot(df, main = "Prediction for Multiple Variables",
     type = "multiline") +
  scale_color_brewer(palette = "Set1")
p1|p2|p3

This code adds a color gradient to each panel using the scale_color_brewer() function.

Conclusion

In this article, we explored how to combine multiple ggpredict plots in one figure. We showed that using the patchwork package is an elegant way to arrange multiple plots into a single plot, making it easier to compare predictions made by different models for different input variables.

By following these steps and examples, you should be able to create beautiful and informative multi-panel plots using ggplot2 and patchwork.


Last modified on 2025-02-22