diff --git a/DESCRIPTION b/DESCRIPTION index fea9afb6a..f7ee79635 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: see Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2' -Version: 0.8.5.4 +Version: 0.8.5.5 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/R/plot.check_dag.R b/R/plot.check_dag.R index bf6e8a5d7..747cf6fd1 100644 --- a/R/plot.check_dag.R +++ b/R/plot.check_dag.R @@ -65,6 +65,14 @@ plot.see_check_dag <- function(x, p1 <- p2 <- suppressWarnings(ggdag::dag_adjustment_sets(x)) adjusted_for <- attributes(x)$adjusted + # if we have multiple sets, we only need one for the current model + if (!is.null(p1$data$set) && insight::n_unique(p1$data$set) > 1) { + p1$data <- p1$data[p1$data$set == p1$data$set[1], ] + # rename set-variables + p2$data$set <- gsub("\\{(.*)\\}", "\\1", p2$data$set) + p2$data$set <- paste0("Adjusted for ", p2$data$set) + } + # for current plot, we need to update the "adjusted" column p1$data$adjusted <- "unadjusted" if (!is.null(adjusted_for)) { @@ -142,6 +150,15 @@ plot.see_check_dag <- function(x, common_layers + ggplot2::ggtitle("Required model") + # if we have multiple sets, we want to facet the second plot by sets + if (!is.null(p2$data$set) && insight::n_unique(p2$data$set) > 1) { + plot2 <- plot2 + ggplot2::facet_wrap( + ~set, + scales = "free", + ncol = ceiling(sqrt(insight::n_unique(p2$data$set))) + ) + } + if (which == "all") { # fix legends - remove the legend that has fewer items, so all items # in the legend are shown for the integrated plot diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg new file mode 100644 index 000000000..0aa4206e4 --- /dev/null +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg @@ -0,0 +1,338 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +alertness + +exam + +humor + +mood + +podcast + +prepared + +skills_course + + +Current model + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +alertness + +exam + +humor + +mood + +podcast + +prepared + +skills_course + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +alertness + +exam + +humor + +mood + +podcast + +prepared + +skills_course + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +alertness + +exam + +humor + +mood + +podcast + +prepared + +skills_course + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +alertness + +exam + +humor + +mood + +podcast + +prepared + +skills_course + + + + + + + + + +Adjusted for mood, prepared + + + + + + + + + +Adjusted for mood, skills_course + + + + + + + + + +Adjusted for alertness, prepared + + + + + + + + + +Adjusted for alertness, skills_course + + + + + + +outcome +exposure +adjusted +unadjusted +Required model +plot.check_dag multiple adjustments + + diff --git a/tests/testthat/test-plot.check_dag.R b/tests/testthat/test-plot.check_dag.R index 00cf68bd9..917c1079d 100644 --- a/tests/testthat/test-plot.check_dag.R +++ b/tests/testthat/test-plot.check_dag.R @@ -51,3 +51,22 @@ test_that("`plot()` for check_dag", { fig = plot(dag, which = "required") ) }) + + +test_that("`plot()` for check_dag, multiple adjustment sets", { + set.seed(1234) + dag <- performance::check_dag( + podcast ~ mood + humor + skills_course, + alertness ~ mood, + mood ~ humor, + prepared ~ skills_course, + exam ~ alertness + prepared, + exposure = "podcast", + outcome = "exam", + coords = ggdag::time_ordered_coords() + ) + vdiffr::expect_doppelganger( + title = "plot.check_dag multiple adjustments", + fig = plot(dag) + ) +})