Skip to content

Commit

Permalink
Merge pull request #66 from henningte/main
Browse files Browse the repository at this point in the history
Missing `limit` argument in `dag_paths()` (#65)
  • Loading branch information
malcolmbarrett authored Apr 14, 2022
2 parents 8368429 + 4869f0a commit fd8d668
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggdag (development version)

* Added the `limit` argument to `dag_paths`, `ggdag_paths`, and `ggdag_paths_fan` (see `dagitty::paths`) (#65).

# ggdag 0.2.4
* `tidy_dagitty()` no longer allows the dendogram layout type (#62)
* `scale_adjusted()` now correctly aligns legend types (#61)
Expand Down
33 changes: 19 additions & 14 deletions R/paths.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#' Find Pathways Between Variables
#' Find Open Paths Between Variables
#'
#' `node_paths` finds the pathways between a given exposure and outcome.
#' `ggdag_paths` plots all pathways. See [dagitty::paths()] for details.
#' `dag_paths` finds open paths between a given exposure and outcome.
#' `ggdag_paths` and `ggdag_paths_fan` plot all open paths. See
#' [dagitty::paths()] for details.
#'
#' @inheritParams dagitty::paths
#' @param .dag,.tdy_dag input graph, an object of class `tidy_dagitty` or
#' `dagitty`
#' @param from character vector of length 1, name of exposure variable. Default
Expand All @@ -15,8 +17,8 @@
#' @param paths_only logical. Should only open paths be returned? Default is
#' `FALSE`, which includes every variable and edge in the DAG regardless
#' if they are part of the path.
#' @param shadow logical. Show edges not in path? Ignored if `paths_only` is
#' `TRUE`
#' @param shadow logical. Show edges which are not on an open path? Ignored if
#' `paths_only` is `TRUE`.
#' @param ... additional arguments passed to `tidy_dagitty()`
#' @param node_size size of DAG node
#' @param text_size size of DAG text
Expand All @@ -31,12 +33,14 @@
#' Default is `NULL`.
#' @param spread the width of the fan spread
#'
#' @return a `tidy_dagitty` with a `path` column for path variables
#' and a `set` grouping column or a `ggplot`
#' @export
#' @return a `tidy_dagitty` with a `path` column for path variables and a `set`
#' grouping column or a `ggplot`.
#'
#' @examples
#' confounder_triangle(x_y_associated = TRUE) %>%
#' dag_paths(from = "x", to = "y")
#'
#' confounder_triangle(x_y_associated = TRUE) %>%
#' ggdag_paths(from = "x", to = "y")
#'
#' butterfly_bias(x_y_associated = TRUE) %>%
Expand All @@ -45,15 +49,16 @@
#' @rdname paths
#' @name Pathways
#' @importFrom magrittr %$%
dag_paths <- function(.dag, from = NULL, to = NULL, adjust_for = NULL, directed = FALSE, paths_only = FALSE, ...) {
#' @export
dag_paths <- function(.dag, from = NULL, to = NULL, adjust_for = NULL, limit = 100, directed = FALSE, paths_only = FALSE, ...) {

.tdy_dag <- if_not_tidy_daggity(.dag, ...)

if (is.null(from)) from <- dagitty::exposures(.tdy_dag$dag)
if (is.null(to)) to <- dagitty::outcomes(.tdy_dag$dag)
if (is.null(from) || is.null(to)) stop("`exposure` and `outcome` must be set!")

pathways <- dagitty::paths(.tdy_dag$dag, from, to, Z = adjust_for, directed = directed) %>%
pathways <- dagitty::paths(.tdy_dag$dag, from, to, Z = adjust_for, limit = limit, directed = directed) %>%
dplyr::as_tibble() %>%
dplyr::filter(open) %>%
dplyr::pull(paths)
Expand Down Expand Up @@ -109,13 +114,13 @@ dag_paths <- function(.dag, from = NULL, to = NULL, adjust_for = NULL, directed

#' @rdname paths
#' @export
ggdag_paths <- function(.tdy_dag, from = NULL, to = NULL, adjust_for = NULL, directed = FALSE, shadow = FALSE, ...,
ggdag_paths <- function(.tdy_dag, from = NULL, to = NULL, adjust_for = NULL, limit = 100, directed = FALSE, shadow = FALSE, ...,
node_size = 16, text_size = 3.88, label_size = text_size, text_col = "white", label_col = text_col,
node = TRUE, stylized = FALSE, text = TRUE, use_labels = NULL) {


p <- if_not_tidy_daggity(.tdy_dag, ...) %>%
dag_paths(from = from, to = to, adjust_for = adjust_for, directed = directed, paths_only = !shadow) %>%
dag_paths(from = from, to = to, adjust_for = adjust_for, limit = limit, directed = directed, paths_only = !shadow) %>%
ggplot2::ggplot(ggplot2::aes(x = x, y = y, xend = xend, yend = yend, col = path, alpha = path)) +
geom_dag_edges(ggplot2::aes(edge_alpha = path, edge_colour = path)) +
ggplot2::facet_wrap(~forcats::fct_inorder(as.factor(set), ordered = TRUE)) +
Expand Down Expand Up @@ -144,13 +149,13 @@ ggdag_paths <- function(.tdy_dag, from = NULL, to = NULL, adjust_for = NULL, dir

#' @rdname paths
#' @export
ggdag_paths_fan <- function(.tdy_dag, from = NULL, to = NULL, adjust_for = NULL, directed = FALSE, ..., shadow = FALSE,
ggdag_paths_fan <- function(.tdy_dag, from = NULL, to = NULL, adjust_for = NULL, limit = 100, directed = FALSE, ..., shadow = FALSE,
spread = .7, node_size = 16, text_size = 3.88, label_size = text_size, text_col = "white", label_col = text_col,
node = TRUE, stylized = FALSE, text = TRUE, use_labels = NULL) {


p <- if_not_tidy_daggity(.tdy_dag, ...) %>%
dag_paths(from = from, to = to, adjust_for = adjust_for, directed = directed, paths_only = !shadow) %>%
dag_paths(from = from, to = to, adjust_for = adjust_for, limit = limit, directed = directed, paths_only = !shadow) %>%
ggplot2::ggplot(ggplot2::aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_edges_fan(
ggplot2::aes(edge_colour = set, edge_alpha = path),
Expand Down
3 changes: 1 addition & 2 deletions man/geom_dag_text.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions man/paths.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions man/repel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd8d668

Please sign in to comment.