jacob-long / jtools

Tools for summarizing/visualizing regressions and other helpful stuff
https://jtools.jacob-long.com
GNU General Public License v3.0
162 stars 22 forks source link

Using facet_wrap with glmmTMB model #147

Closed 5tokesy closed 1 month ago

5tokesy commented 7 months ago

I’ve created a model using the glmmTMB package which produces acceptable results. My model uses several years of data. The years are included as a random intercept.

I have plotted the model using jtools. I’m happy with the combined year plot. Next, I want to plot the individual years using GGPLOT’s facet_wrap function.

library(jtools)
library(ggplot2)
library(glmmTMB)
data(mpg)
fit <- glmmTMB(cty ~ displ + (1 | year), data = mpg)
jtools::effect_plot(fit, pred = displ, 
                    interval = TRUE, 
                    groups = list(year = unique(mpg$year)))

I've tried a few variations on the following code:

effect_plot(
  fit, 
  pred = displ, 
  interval = TRUE, 
  groups = unique(mpg$year))

I've also tried using '+ facet_wrap(~year, ncol = 2)' but it produces the same plot given in my example code.

Thanks for any suggestions.

jacob-long commented 1 month ago

I thought it would be good if effect_plot() could do this out of the box, so I've added a facet.by argument to implement basically what you're looking for. It should work on the current development version of jtools and I expect the update to be on CRAN within a week or so.