Skip to content

Commit

Permalink
hack ggdag a bit to get more consistent output
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmbarrett committed Oct 20, 2023
1 parent 808615e commit 1c9df26
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 7 deletions.
37 changes: 37 additions & 0 deletions R/ggdag-mask.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# this is all a hack to make work with quick plotting
# TODO: when `geom_dag_label_repel2` exists, add to namespace as 1 then delete this first bit
# copied from source to avoid recursion issue in overriding in ggdag namsespace
ggdag_geom_dag_label_repel <- function(
mapping = NULL, data = NULL, parse = FALSE, ...,
box.padding = grid::unit(0.35,"lines"), label.padding = grid::unit(0.25, "lines"),
point.padding = grid::unit(1.5, "lines"), label.r = grid::unit(0.15, "lines"),
label.size = 0.25, segment.color = "grey50", segment.size = 0.5, arrow = NULL,
force = 1, max.iter = 2000, nudge_x = 0, nudge_y = 0, na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE) {
ggplot2::layer(data = data, mapping = mapping, stat = ggdag:::StatNodesRepel,
geom = ggrepel::GeomLabelRepel, position = "identity",
show.legend = show.legend, inherit.aes = inherit.aes,
params = list(parse = parse, box.padding = box.padding,
label.padding = label.padding, point.padding = point.padding,
label.r = label.r, label.size = label.size, segment.colour = segment.color %||%
segment.colour, segment.size = segment.size,
arrow = arrow, na.rm = na.rm, force = force, max.iter = max.iter,
nudge_x = nudge_x, nudge_y = nudge_y, segment.alpha = 1, ...))
}

geom_dag_label_repel_internal <- function(..., seed = 10) {
ggdag_geom_dag_label_repel(
mapping = aes(x, y, label = label),
# TODO: make sure this looks ok. slightly different from above
box.padding = 2,
max.overlaps = Inf,
inherit.aes = FALSE,
family = getOption("book.base_family"),
seed = seed,
label.size = NA,
label.padding = 0.1
)
}

# apply to quick functions as well
assignInNamespace("geom_dag_label_repel", geom_dag_label_repel_internal, ns = "ggdag")
2 changes: 1 addition & 1 deletion R/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ theme_dag <- function() {
}

geom_dag_label_repel <- function(..., seed = 10) {
ggdag::geom_dag_label_repel(
ggdag_geom_dag_label_repel(
aes(x, y, label = label),
box.padding = 3.5,
inherit.aes = FALSE,
Expand Down
4 changes: 2 additions & 2 deletions _freeze/chapters/chapter-05/execute-results/html.json

Large diffs are not rendered by default.

Binary file modified _freeze/chapters/chapter-05/figure-html/fig-dag-podcast-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _freeze/chapters/chapter-05/figure-html/fig-paths-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _freeze/chapters/chapter-05/figure-html/unnamed-chunk-12-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _freeze/chapters/chapter-05/figure-html/unnamed-chunk-13-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _freeze/chapters/chapter-05/figure-html/unnamed-chunk-14-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _freeze/chapters/chapter-05/figure-html/unnamed-chunk-16-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions chapters/00-setup.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
```{r}
#| include: false
source(here::here("R/ggdag-mask.R"))
source(here::here("R/setup.R"))
library(tidyverse)
```
22 changes: 18 additions & 4 deletions chapters/chapter-05.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ theme_set(
You don't need to specify coordinates to ggdag. If you don't, it uses algorithms designed for automatic layouts. There are many such algorithms, and they focus on different aspects of the layout, e.g. the shape, the space between the nodes, minimizing how many edges cross, etc. These layout algorithms usually have a component of randomness, so it's good to use a seed if you want to get the same result.

```{r}
#| fig-width: 4
#| fig-height: 4
#| fig-align: center
# no coordinates specified
set.seed(123)
pod_dag <- dagify(
Expand All @@ -376,13 +379,19 @@ pod_dag |>
We can also ask for a specific layout, e.g. the popular Sugiyama algorithm for DAGs

```{r}
#| fig-width: 4
#| fig-height: 4
#| fig-align: center
pod_dag |>
ggdag(layout = "sugiyama", text_size = 2.8)
```

For causal DAGs, the time-ordered layout algorithm is often best, which we can specify with `time_ordered_coords()` or `layout = "time_ordered"`. We'll discuss time ordering in greater detail in @sec-time-ordered

```{r}
#| fig-width: 4
#| fig-height: 4
#| fig-align: center
pod_dag |>
ggdag(layout = "time_ordered", text_size = 2.8)
```
Expand All @@ -398,11 +407,18 @@ Algorithmic layouts are often nice for fast visualization of DAGs or particularl
#| label: fig-paths
# TODO: Why aren't okabe-ito colors propgating here and other spots in ggdag?
podcast_dag |>
ggdag_paths(shadow = TRUE, text_size = 2.8)
ggdag_paths(shadow = TRUE, text = FALSE, use_labels = "label")
```

```{r}
ggdag_adjustment_set(podcast_dag, text_size = 2.8)
#| fig-width: 4
#| fig-height: 4
#| fig-align: center
ggdag_adjustment_set(
podcast_dag,
text = FALSE,
use_labels = "label"
)
```


Expand All @@ -411,8 +427,6 @@ dagitty::adjustmentSets(podcast_dag)
```


- building
- popout: coordinates. show manual. mention dagitty.net. mention `time_ordered`.
- paths
- adjustment sets
- with ggplot
Expand Down

0 comments on commit 1c9df26

Please sign in to comment.