Skip to content

Commit

Permalink
convert status
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmbarrett committed Jan 29, 2024
1 parent b63a8c7 commit 115c896
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 64 deletions.
76 changes: 32 additions & 44 deletions R/status.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,8 @@
#' @param .dag,.tdy_dag input graph, an object of class `tidy_dagitty` or
#' `dagitty`
#' @param ... additional arguments passed to `tidy_dagitty()`
#' @param edge_type a character vector, the edge geom to use. One of:
#' "link_arc", which accounts for directed and bidirected edges, "link",
#' "arc", or "diagonal"
#' @param as_factor treat `status` variable as factor
#' @param node_size size of DAG node
#' @param text_size size of DAG text
#' @param label_size size of label text
#' @param text_col color of DAG text
#' @param label_col color of label text
#' @param node logical. Should nodes be included in the DAG?
#' @param stylized logical. Should DAG nodes be stylized? If so, use
#' `geom_dag_nodes` and if not use `geom_dag_point`
#' @param text logical. Should text be included in the DAG?
#' @param use_labels a string. Variable to use for `geom_dag_label_repel()`.
#' Default is `NULL`.
#' @inheritParams geom_dag
#'
#' @return a `tidy_dagitty` with a `status` column for
#' variable status or a `ggplot`
Expand Down Expand Up @@ -66,41 +53,42 @@ node_status <- function(.dag, as_factor = TRUE, ...) {

#' @rdname status
#' @export
ggdag_status <- function(.tdy_dag, ..., edge_type = "link_arc", 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) {
edge_function <- edge_type_switch(edge_type)

ggdag_status <- function(.tdy_dag, ...,
size = 1, edge_type = c("link_arc", "link", "arc", "diagonal"),
node_size = 16, text_size = 3.88, label_size = text_size,
text_col = "white", label_col = "black",
edge_width = 0.6, edge_cap = 8, arrow_length = 5,
use_edges = TRUE,
use_nodes = TRUE, use_stylized = FALSE, use_text = TRUE,
use_labels = FALSE, text = NULL, label = NULL,
node = deprecated(), stylized = deprecated()) {
p <- if_not_tidy_daggity(.tdy_dag) %>%
node_status(...) %>%
ggplot2::ggplot(ggplot2::aes(x = x, y = y, xend = xend, yend = yend, color = status)) +
edge_function() +
ggplot2::ggplot(dag_aes(color = status)) +
scale_adjusted() +
breaks(c("exposure", "outcome", "latent"))

if (node) {
if (stylized) {
p <- p + geom_dag_node(size = node_size)
} else {
p <- p + geom_dag_point(size = node_size)
}
}

if (text) p <- p + geom_dag_text(col = text_col, size = text_size)
p <- p + geom_dag(
size = size,
edge_type = edge_type,
node_size = node_size,
text_size = text_size,
label_size = label_size,
text_col = text_col,
label_col = label_col,
edge_width = edge_width,
edge_cap = edge_cap,
arrow_length = arrow_length,
use_edges = use_edges,
use_nodes = use_nodes,
use_stylized = use_stylized,
use_text = use_text,
use_labels = use_labels,
text = !!rlang::enquo(text),
label = !!rlang::enquo(label),
node = node,
stylized = stylized
)

if (!is.null(use_labels)) {
p <- p +
geom_dag_label_repel(
ggplot2::aes(
label = !!rlang::sym(use_labels),
fill = status
),
size = text_size,
col = label_col,
show.legend = FALSE
)
}
p
}
69 changes: 49 additions & 20 deletions man/status.Rd

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

0 comments on commit 115c896

Please sign in to comment.