Skip to content

Commit

Permalink
plot.check_dag plots multiple adjustment sets (#354)
Browse files Browse the repository at this point in the history
* plot.check_dag plots multiple adjustment sets

* add tests
  • Loading branch information
strengejacke authored Aug 10, 2024
1 parent fe94bf3 commit 52a0f0d
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
17 changes: 17 additions & 0 deletions R/plot.check_dag.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 52a0f0d

Please sign in to comment.