Search code examples
rggplot2sjplot

Set same y-lim for multi panel plot using plot_model()


I have created a multi-panel plot using plot_model(). However, the scale limits on the y_axis came different for each panel.

I first run a model fm then plot the predicted values:

p <-  plot_model(
  fm,
  type = c("pred"),
  terms = c("Trial","CS [-0.9,0,0.9]", "Prof[-10.9,0,10.9]", "Congr"))

Then I wanted to get same y-axis scale (limits) on all panels:

 p + ggplot2::scale_y_continuous(limits = c(5.9, 6.2))

but this didn't work!

Any help is appreciated!

Thanks! That seems to work but only if I minimize the panels, that is having each panel on a separate plot:

Panel1 Panel

When I want to show both panels in the same plot and with the Y-limits desired, nothing changes.

Panels 1&2

Is this because the size of the plot is limited?

Here is a reproducible example:

df <- structure(list(Subject = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), Log_RT = c(5.955837369, 6.228511004, 5.874930731, 
5.84932478, 5.780743516, 5.866468057, 5.424950017, 5.81711116, 
5.899897354, 5.834810737, 5.683579767, 5.655991811, 5.624017506, 
5.459585514, 5.697093487, 5.934894196, 5.802118375, 5.834810737, 
5.789960171, 5.631211782, 5.796057751, 5.669880923, 5.549076085, 
5.81711116, 6.03068526, 6.040254711, 5.81711116, 5.80814249, 
5.863631176, 5.641907071, 6.033086222, 6.021023349, 6.470799504, 
6.380122537, 6.424869024, 6.29156914, 6.061456919, 6.502790046, 
6.282266747, 6.311734809, 6.455198563, 6.259581464, 6.570882962, 
6.371611847, 6.570882962, 6.483107351, 6.333279628, 6.455198563, 
6.469250317, 6.289715571, 6.285998095, 6.442540166, 6.289715571, 
6.395261598, 6.152732695, 6.415096959, 6.352629396, 6.270988432, 
6.210600077, 6.311734809, 6.059123196, 6.208590026), CSC = c(-0.562217385, 
-0.562217385, -0.562217385, -0.562217385, -0.562217385, -0.562217385, 
-0.562217385, -0.562217385, -0.562217385, -0.562217385, -0.562217385, 
-0.562217385, -0.562217385, -0.562217385, -0.562217385, -0.562217385, 
-0.562217385, -0.562217385, -0.562217385, -0.562217385, -0.562217385, 
-0.562217385, -0.562217385, -0.562217385, -0.562217385, -0.562217385, 
-0.562217385, -0.562217385, -0.562217385, -0.562217385, -0.145550719, 
-0.145550719, -0.145550719, -0.145550719, -0.145550719, -0.145550719, 
-0.145550719, -0.145550719, -0.145550719, -0.145550719, -0.145550719, 
-0.145550719, -0.145550719, -0.145550719, -0.145550719, -0.145550719, 
-0.145550719, -0.145550719, -0.145550719, -0.145550719, -0.145550719, 
-0.145550719, -0.145550719, -0.145550719, -0.145550719, -0.145550719, 
-0.145550719, -0.145550719, -0.145550719, -0.145550719, -0.145550719, 
-0.145550719), Trial = c(-14.60970149, -13.60970149, -12.60970149, 
-11.60970149, -10.60970149, -9.609701493, -8.609701493, -7.609701493, 
-6.609701493, -5.609701493, -4.609701493, -3.609701493, -2.609701493, 
-1.609701493, -0.609701493, 0.390298507, 1.390298507, 2.390298507, 
3.390298507, 4.390298507, 6.390298507, 7.390298507, 8.390298507, 
9.390298507, 10.39029851, 11.39029851, 12.39029851, 13.39029851, 
14.39029851, 15.39029851, -15.60970149, -14.60970149, -13.60970149, 
-12.60970149, -11.60970149, -10.60970149, -9.609701493, -8.609701493, 
-7.609701493, -6.609701493, -5.609701493, -4.609701493, -3.609701493, 
-2.609701493, -1.609701493, -0.609701493, 0.390298507, 1.390298507, 
2.390298507, 3.390298507, 4.390298507, 5.390298507, 6.390298507, 
7.390298507, 8.390298507, 9.390298507, 10.39029851, 11.39029851, 
12.39029851, 13.39029851, 14.39029851, 15.39029851), Congr.d = c(1L, 
0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 
1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 
1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L), ProC = c(7.814814815, 
7.814814815, 7.814814815, 7.814814815, 7.814814815, 7.814814815, 
7.814814815, 7.814814815, 7.814814815, 7.814814815, 7.814814815, 
7.814814815, 7.814814815, 7.814814815, 7.814814815, 7.814814815, 
7.814814815, 7.814814815, 7.814814815, 7.814814815, 7.814814815, 
7.814814815, 7.814814815, 7.814814815, 7.814814815, 7.814814815, 
7.814814815, 7.814814815, 7.814814815, 7.814814815, 12.25925926, 
12.25925926, 12.25925926, 12.25925926, 12.25925926, 12.25925926, 
12.25925926, 12.25925926, 12.25925926, 12.25925926, 12.25925926, 
12.25925926, 12.25925926, 12.25925926, 12.25925926, 12.25925926, 
12.25925926, 12.25925926, 12.25925926, 12.25925926, 12.25925926, 
12.25925926, 12.25925926, 12.25925926, 12.25925926, 12.25925926, 
12.25925926, 12.25925926, 12.25925926, 12.25925926, 12.25925926, 
12.25925926)), class = "data.frame", row.names = c(NA, -62L))

For the plot:

library(jtools)
library(sjPlot)
library(pbkrtest)

fm <- lmer(Log_RT ~ Congr.d*CSC*Trial*ProC +
              (1+Congr.d||Subject), data=df)

summ(fm)



plot_model(fm, type = c("pred"),terms = c("Trial","CSC [-0.2,0,0.2]",
 "ProC[-2.2,0,2.2]", "Congr.d[0,1]"),axis.lim=c(6.2,6.8)) 

Plot

You can see that the Y-axis limits is not as indicated in the code.


Solution

  • You could use the ggeffects-package, which is internally used by sjPlot to create effects-plots. ggeffects gives you some more flexibility according to plot-customization. In your case, you can simply use arguments that are passed down to ggplot2::scale_y_continuous(), see details in this vignette:

    library(lme4)
    library(ggeffects)
    
    fm <- lmer(Log_RT ~ Congr.d*CSC*Trial*ProC + (1+Congr.d||Subject), data=df)
    pr <- ggpredict(fm, terms = c("Trial","CSC [-0.2,0,0.2]", "ProC[-2.2,0,2.2]", "Congr.d[0,1]"))
    plot(pr, limits = c(6.0, 7.0))
    

    Created on 2019-12-23 by the reprex package (v0.3.0)