Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up appearance of shadows and make them the default #136

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions R/adjustment_sets.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ ggdag_adjustment_set <- function(
exposure = NULL,
outcome = NULL,
...,
shadow = FALSE,
shadow = TRUE,
size = 1,
node_size = 16,
text_size = 3.88,
label_size = text_size,
text_col = "white",
label_col = "black",
edge_width = 0.6,
edge_cap = 8,
edge_cap = 10,
arrow_length = 5,
use_edges = TRUE,
use_nodes = TRUE,
Expand All @@ -111,24 +111,21 @@ ggdag_adjustment_set <- function(
expand_plot(expand_x = expand_x, expand_y = expand_y)

if (shadow) {
p <- p + geom_dag_edges(
ggplot2::aes(edge_alpha = adjusted),
start_cap = ggraph::circle(10, "mm"),
end_cap = ggraph::circle(10, "mm")
)
vals <- c("unadjusted" = "black", "adjusted" = "grey80")
} else {
vals <- c("unadjusted" = "black", "adjusted" = "#FFFFFF00")
p <- p + geom_dag_edges(
ggplot2::aes(edge_colour = adjusted),
show.legend = FALSE
) +
ggraph::scale_edge_colour_manual(
drop = FALSE,
values = vals,
limits = names(vals)
)
}

p <- p + geom_dag_edges(
ggplot2::aes(edge_colour = adjusted),
show.legend = if (shadow) TRUE else FALSE
) +
ggraph::scale_edge_colour_manual(
drop = FALSE,
values = vals,
limits = names(vals)
)

p <- p +
geom_dag(
size = size,
Expand Down Expand Up @@ -266,7 +263,7 @@ ggdag_adjust <- function(
start_cap = ggraph::circle(edge_cap, "mm"),
end_cap = ggraph::circle(edge_cap, "mm")
) +
scale_adjusted() +
scale_adjusted(include_alpha = TRUE) +
expand_plot(expand_y = expansion(c(0.2, 0.2)))

if (collider_lines) p <- p + geom_dag_collider_edges()
Expand Down
3 changes: 1 addition & 2 deletions R/colliders.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ ggdag_collider <- function(
p <- if_not_tidy_daggity(.tdy_dag, ...) %>%
node_collider() %>%
dplyr::mutate(colliders = forcats::fct_rev(colliders)) %>%
ggplot2::ggplot(aes_dag(color = colliders)) +
scale_adjusted()
ggplot2::ggplot(aes_dag(color = colliders))

p <- p + geom_dag(
size = size,
Expand Down
15 changes: 12 additions & 3 deletions R/geom_dag.R
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ aes_dag <- function(...) {
#' you'll find that it is awkward for sophisticated customization. When you hit
#' that point, you should use the underlying geoms directly.
#'
#' @inheritParams ggplot2::geom_point
#' @param size A numeric value scaling the size of all elements in the DAG. This
#' allows you to change the scale of the DAG without changing the proportions.
#' @param edge_type The type of edge, one of "link_arc", "link", "arc",
Expand Down Expand Up @@ -1021,6 +1022,7 @@ aes_dag <- function(...) {
#'
#' @export
geom_dag <- function(
data = NULL,
size = 1,
edge_type = c("link_arc", "link", "arc", "diagonal"),
node_size = 16,
Expand Down Expand Up @@ -1073,6 +1075,7 @@ geom_dag <- function(
start_cap = ggraph::circle(sizes[["cap"]], "mm"),
end_cap = ggraph::circle(sizes[["cap"]], "mm")
),
data = data,
edge_width = sizes[["edge"]],
arrow = grid::arrow(length = grid::unit(sizes[["arrow"]], "pt"), type = "closed")
)
Expand All @@ -1083,9 +1086,9 @@ geom_dag <- function(

if (isTRUE(use_nodes)) {
if (isTRUE(use_stylized)) {
node_geom <- geom_dag_node(size = sizes[["node"]])
node_geom <- geom_dag_node(size = sizes[["node"]], data = data)
} else {
node_geom <- geom_dag_point(size = sizes[["node"]])
node_geom <- geom_dag_point(size = sizes[["node"]], data = data)
}
} else {
node_geom <- NULL
Expand Down Expand Up @@ -1117,7 +1120,12 @@ geom_dag <- function(
mapping <- NULL
}

text_geom <- geom_dag_text(mapping = mapping, col = text_col, size = text_size)
text_geom <- geom_dag_text(
mapping = mapping,
data = data,
col = text_col,
size = text_size
)
} else {
text_geom <- NULL
}
Expand Down Expand Up @@ -1151,6 +1159,7 @@ geom_dag <- function(

label_geom <- geom_dag_label_repel(
ggplot2::aes(label = !!label),
data = data,
size = sizes[["label"]] * 1.1,
col = label_col,
show.legend = FALSE,
Expand Down
42 changes: 31 additions & 11 deletions R/paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
#' @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 which are not on an open path? Ignored if
#' `paths_only` is `TRUE`.
#' @param shadow logical. Show edges which are not on an open path?
#' @param ... additional arguments passed to `tidy_dagitty()`
#' @inheritParams geom_dag
#' @param spread the width of the fan spread
Expand Down Expand Up @@ -112,7 +111,7 @@ ggdag_paths <- function(
adjust_for = NULL,
limit = 100,
directed = FALSE,
shadow = FALSE,
shadow = TRUE,
...,
size = 1,
edge_type = c("link_arc", "link", "arc", "diagonal"),
Expand All @@ -135,21 +134,42 @@ ggdag_paths <- function(
stylized = deprecated()
) {
p <- if_not_tidy_daggity(.tdy_dag, ...) %>%
dag_paths(from = from, to = to, adjust_for = adjust_for, limit = limit, directed = directed, paths_only = !shadow) %>%
ggplot2::ggplot(aes_dag(col = path, alpha = path)) +
geom_dag_edges(ggplot2::aes(edge_alpha = path, edge_colour = path)) +
dag_paths(from = from, to = to, adjust_for = adjust_for, limit = limit, directed = directed) %>%
ggplot2::ggplot(aes_dag(color = path)) +
geom_dag_edges(
ggplot2::aes(edge_colour = path),
show.legend = if (shadow) TRUE else FALSE
) +
ggplot2::facet_wrap(~ forcats::fct_inorder(as.factor(set))) +
ggplot2::scale_alpha_manual(drop = FALSE, values = c("open path" = 1), na.value = .35, breaks = "open path", limits = "open path") +
ggraph::scale_edge_alpha_manual(drop = FALSE, values = c("open path" = 1), na.value = .35, breaks = "open path", limits = "open path") +
ggraph::scale_edge_colour_discrete(drop = FALSE, breaks = "open path") +
ggplot2::scale_color_discrete(drop = FALSE, breaks = "open path") +
breaks("open path") +
expand_plot(
expand_x = expansion(c(0.25, 0.25)),
expand_y = expansion(c(0.1, 0.1))
)

if (shadow) {
vals <- c("unadjusted" = "black", "adjusted" = "grey80")
} else {
vals <- c("unadjusted" = "black", "adjusted" = "#FFFFFF00")
}

p <- p +
ggraph::scale_edge_color_discrete(
drop = FALSE,
na.value = if (shadow) "grey80" else "#FFFFFF00",
na.translate = if (shadow) TRUE else FALSE,
limits = "open path"
) +
ggplot2::scale_color_discrete(
drop = FALSE,
na.value = if (shadow) "grey80" else "#FFFFFF00",
na.translate = if (shadow) TRUE else FALSE,
limits = "open path"
)

p <- p +
geom_dag(
data = if (!shadow) function(x) dplyr::filter(x, path == "open path"),
size = size,
node_size = node_size,
text_size = text_size,
Expand Down Expand Up @@ -183,7 +203,7 @@ ggdag_paths_fan <- function(
limit = 100,
directed = FALSE,
...,
shadow = FALSE,
shadow = TRUE,
spread = .7,
size = 1,
node_size = 16,
Expand Down
8 changes: 5 additions & 3 deletions R/themes.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ theme_dag_gray_grid <- theme_dag_grey_grid
#' whenever [geom_dag_collider_edges()] is used. `scale_dag()` is deprecated in
#' favor of `scale_adjusted()`.
#'
#' @param include_alpha Logical. Include alpha-related scales?
#' @param breaks One of:
#'
#' - NULL for no breaks
Expand All @@ -133,12 +134,13 @@ theme_dag_gray_grid <- theme_dag_grey_grid
#'
#' @export
#' @rdname scale_adjusted
scale_adjusted <- function() {
scale_adjusted <- function(include_alpha = FALSE) {
list(
ggplot2::scale_linetype_manual(name = NULL, values = "dashed"),
ggplot2::scale_shape_manual(drop = FALSE, values = c("adjusted" = 15, "unadjusted" = 19), limits = c("adjusted", "unadjusted")),
ggplot2::scale_alpha_manual(drop = FALSE, values = c("adjusted" = .30, "unadjusted" = 1), limits = c("adjusted", "unadjusted")),
ggraph::scale_edge_alpha_manual(name = NULL, drop = FALSE, values = c("adjusted" = .30, "unadjusted" = 1), limits = c("adjusted", "unadjusted"))
ggplot2::scale_color_discrete(limits = c("adjusted", "unadjusted")),
if (include_alpha) ggplot2::scale_alpha_manual(drop = FALSE, values = c("adjusted" = .30, "unadjusted" = 1), limits = c("adjusted", "unadjusted")),
if (include_alpha) ggraph::scale_edge_alpha_manual(name = NULL, drop = FALSE, values = c("adjusted" = .30, "unadjusted" = 1), limits = c("adjusted", "unadjusted"))
)
}

Expand Down
10 changes: 6 additions & 4 deletions man/adjustment_sets.Rd

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

2 changes: 1 addition & 1 deletion man/aes_dag.Rd

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

6 changes: 4 additions & 2 deletions man/dagify.Rd

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

25 changes: 22 additions & 3 deletions man/geom_dag.Rd

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

2 changes: 1 addition & 1 deletion man/ggdag.Rd

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

7 changes: 3 additions & 4 deletions man/paths.Rd

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

Loading
Loading