diff --git a/R/plot.parameters_model.R b/R/plot.parameters_model.R index 884a8110a..99ea3a48e 100644 --- a/R/plot.parameters_model.R +++ b/R/plot.parameters_model.R @@ -613,6 +613,16 @@ plot.see_parameters_model <- function(x, min_ci <- min(x$CI_low, na.rm = TRUE) max_ci <- max(x$CI_high, na.rm = TRUE) + # here we check if all facets have the same scale. If so, we set the scales + # to fixed, otherwise we set them to free_y (in facet_wrap). This removes + # a redundant scale for plots with identical scales. + check_scales <- split(x$Level, x$Group) + if (isTRUE(identical(unname(check_scales[-length(check_scales)]), unname(check_scales[-1])))) { + facet_scales <- "fixed" + } else { + facet_scales <- "free_y" + } + p <- ggplot2::ggplot( x, ggplot2::aes( @@ -641,7 +651,7 @@ plot.see_parameters_model <- function(x, colour = "white", shape = 21 ) + - ggplot2::facet_wrap(~Group, ncol = n_columns) + ggplot2::facet_wrap(~Group, ncol = n_columns, scales = facet_scales) # add coefficients and CIs? if (isTRUE(show_labels)) { diff --git a/tests/testthat/test-plot.parameters_model.R b/tests/testthat/test-plot.parameters_model.R index fa3321224..9896125c4 100644 --- a/tests/testthat/test-plot.parameters_model.R +++ b/tests/testthat/test-plot.parameters_model.R @@ -60,3 +60,23 @@ test_that("`plot.see_parameters_model()` random parameters works", { fig = plot(out, sort = "ascending", show_labels = TRUE, show_intercept = FALSE) ) }) + + +test_that("`plot.see_parameters_model()` random parameters works", { + skip_if_not_installed("vdiffr") + skip_if_not_installed("lme4") + skip_if_not_installed("parameters") + data(sleepstudy, package = "lme4") + + set.seed(12345) + sleepstudy$grp <- sample(1:5, size = 180, replace = TRUE) + model <- lmer( + Reaction ~ Days + (1 | grp) + (1 | Subject), + data = sleepstudy + ) + out <- parameters::model_parameters(model, group_level = TRUE) + vdiffr::expect_doppelganger( + title = "plot.model_parameters_doublerandom", + fig = plot(out) + ) +}) diff --git a/vignettes/parameters.Rmd b/vignettes/parameters.Rmd index 8eb0514eb..1ec2a730f 100644 --- a/vignettes/parameters.Rmd +++ b/vignettes/parameters.Rmd @@ -148,6 +148,13 @@ result <- model_parameters(model2) plot(result) ``` +### Including group levels of random effects + +```{r} +result <- model_parameters(model3, group_level = TRUE) +plot(result) +``` + ### Changing parameter names in the plot By default, `model_parameters()` returns a data frame, where the parameter names are found in the column `Parameter`. These names are used by default in the generated plot: