Skip to content

Commit

Permalink
Fix issue with facet_grids and numeric predictors
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jan 16, 2025
1 parent 0110c57 commit 80e16af
Show file tree
Hide file tree
Showing 15 changed files with 5,333 additions and 4,721 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: modelbased
Title: Estimation of Model-Based Predictions, Contrasts and Means
Version: 0.8.9.29
Version: 0.8.9.30
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
23 changes: 18 additions & 5 deletions R/visualisation_recipe_internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@
}
if (length(by) > 3) {
aes$facet <- NULL
aes$grid <- stats::as.formula(paste(by[3], "~", paste(utils::tail(by, -3), collapse = "*")))
# we have to switch variables 3 and 4, due to regression formula
remaining <- paste(utils::tail(by, -2))
aes$grid <- stats::as.formula(paste(
remaining[2],
"~",
paste(setdiff(remaining, remaining[2]), collapse = "*")
))
}

# CI
Expand Down Expand Up @@ -285,7 +291,7 @@
stroke <- 1
}

list(
out <- list(
geom = geom,
data = rawdata,
aes = list(
Expand All @@ -296,8 +302,15 @@
),
height = 0,
shape = shape,
stroke = stroke,
# set default alpha, it not mapped by aes
alpha = ifelse(is.null(aes$alpha), 1 / 3, NULL)
stroke = stroke
)

# set default alpha, it not mapped by aes
if (is.null(aes$alpha)) {
out$alpha <- 1 / 3
} else {
out$alpha <- NULL
}

out
}
306 changes: 155 additions & 151 deletions tests/testthat/_snaps/plot-facet/plot-interaction-facets-10.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
970 changes: 477 additions & 493 deletions tests/testthat/_snaps/plot-facet/plot-interaction-facets-3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
658 changes: 321 additions & 337 deletions tests/testthat/_snaps/plot-facet/plot-interaction-facets-4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
660 changes: 329 additions & 331 deletions tests/testthat/_snaps/plot-facet/plot-interaction-facets-5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,301 changes: 639 additions & 662 deletions tests/testthat/_snaps/plot-facet/plot-interaction-facets-6.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2,431 changes: 1,311 additions & 1,120 deletions tests/testthat/_snaps/plot-facet/plot-interaction-facets-7.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
992 changes: 496 additions & 496 deletions tests/testthat/_snaps/plot-facet/plot-interaction-facets-8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
656 changes: 320 additions & 336 deletions tests/testthat/_snaps/plot-facet/plot-interaction-facets-9.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
354 changes: 180 additions & 174 deletions tests/testthat/_snaps/plot-facet/plot-interaction-facets-cat-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
631 changes: 308 additions & 323 deletions tests/testthat/_snaps/plot-facet/plot-interaction-facets-cat-2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
569 changes: 277 additions & 292 deletions tests/testthat/_snaps/plot-facet/plot-interaction-facets-cat-3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
483 changes: 483 additions & 0 deletions tests/testthat/_snaps/plot/plot-4way-numeric-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions tests/testthat/test-plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,21 @@ test_that("plots, logistic regression", {
plot(out)
)
})


test_that("plots, 4-way with numeric", {
skip_if_not_installed("ggeffects")
data(efc, package = "ggeffects")
# make categorical
efc <- datawizard::to_factor(efc, c("c161sex", "c172code", "e16sex"))
levels(efc$c172code) <- c("low", "mid", "high")
m <- lm(neg_c_7 ~ e16sex + c161sex + c172code * barthtot + c12hour, data = efc)
by <- c("c12hour", "c161sex", "c172code", "barthtot = [fivenum]")
estim <- estimate_means(m, by = by, backend = "marginaleffects")
set.seed(123)
vdiffr::expect_doppelganger(
"plot-4way-numeric-1",
plot(estim, show_data = FALSE)
)

})

0 comments on commit 80e16af

Please sign in to comment.