-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgeom_dag.R
1191 lines (1140 loc) · 31 KB
/
geom_dag.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' DAG Nodes
#'
#' `geom_dag_node` and `geom_dag_point` are very similar to
#' [ggplot2::geom_point] but with a few defaults changed. `geom_dag_node` is
#' slightly stylized and includes an internal white circle, while
#' `geom_dag_point` plots a single point.
#'
#' @export
#'
#' @inheritParams ggplot2::geom_point
#'
#' @section Aesthetics: `geom_dag_node` and `geom_dag_point` understand the
#' following aesthetics (required aesthetics are in bold):
#'
#' - **x**
#' - **y**
#' - alpha
#' - colour
#' - fill
#' - shape
#' - size
#' - stroke
#' - filter
#'
#' `geom_dag_node` also accepts:
#'
#' - internal_colour
#'
#' @examples
#' library(ggplot2)
#' g <- dagify(m ~ x + y, y ~ x)
#' p <- g %>%
#' tidy_dagitty() %>%
#' ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
#' geom_dag_edges() +
#' theme_dag()
#'
#' p +
#' geom_dag_node() +
#' geom_dag_text()
#'
#' p +
#' geom_dag_point() +
#' geom_dag_text()
#' @rdname node_point
#' @name Nodes
geom_dag_node <- function(
mapping = NULL,
data = NULL,
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
ggplot2::layer(
data = data,
mapping = mapping,
stat = StatNodes,
geom = GeomDagNode,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
...
)
)
}
#' @export
#' @rdname node_point
geom_dag_point <- function(
mapping = NULL,
data = NULL,
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
ggplot2::layer(
data = data,
mapping = mapping,
stat = StatNodes,
geom = GeomDagPoint,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
...
)
)
}
#' Node text
#'
#' @inheritParams ggplot2::geom_text
#'
#' @section Aesthetics:
#' `geom_dag_text` understand the following aesthetics (required aesthetics are in bold):
#'
#' - **x**
#' - **y**
#' - **label**
#' - alpha
#' - angle
#' - colour
#' - family
#' - fontface
#' - group
#' - hjust
#' - lineheight
#' - size
#' - vjust
#'
#' @export
#'
#' @examples
#' library(ggplot2)
#' g <- dagify(m ~ x + y, y ~ x)
#' g %>%
#' tidy_dagitty() %>%
#' ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
#' geom_dag_point() +
#' geom_dag_edges() +
#' geom_dag_text() +
#' theme_dag()
geom_dag_text <- function(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
parse = FALSE,
nudge_x = 0,
nudge_y = 0,
check_overlap = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
if (!missing(nudge_x) || !missing(nudge_y)) {
if (!missing(position)) {
stop("Specify either `position` or `nudge_x`/`nudge_y`", call. = FALSE)
}
position <- ggplot2::position_nudge(nudge_x, nudge_y)
}
if (is.null(mapping)) mapping <- ggplot2::aes(label = name)
if (is.null(mapping$label)) mapping$label <- substitute(name)
ggplot2::layer(
data = data,
mapping = mapping,
stat = StatNodes,
geom = GeomDagText,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
parse = parse,
check_overlap = check_overlap,
na.rm = na.rm,
...
)
)
}
#' Node text labels
#'
#' @inheritParams ggplot2::geom_label
#'
#' @section Aesthetics:
#' `geom_dag_label` understand the following aesthetics (required aesthetics are in bold):
#'
#' - **x**
#' - **y**
#' - **label**
#' - alpha
#' - angle
#' - colour
#' - family
#' - fontface
#' - group
#' - hjust
#' - lineheight
#' - size
#' - vjust
#'
#' @export
#'
#' @examples
#' library(ggplot2)
#' library(ggraph)
#' g <- dagify(m ~ x + y, y ~ x)
#'
#' ggdag(g, text = FALSE) + geom_dag_label()
#'
#' g %>%
#' tidy_dagitty() %>%
#' ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
#' geom_dag_edges(aes(
#' start_cap = label_rect(name, padding = margin(2.5, 2.5, 2.5, 2.5, "mm")),
#' end_cap = label_rect(name, padding = margin(2.5, 2.5, 2.5, 2.5, "mm"))
#' )) +
#' geom_dag_label(size = 5, fill = "black", color = "white") +
#' theme_dag()
geom_dag_label <- function(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
parse = FALSE,
nudge_x = 0,
nudge_y = 0,
check_overlap = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
if (!missing(nudge_x) || !missing(nudge_y)) {
if (!missing(position)) {
stop("Specify either `position` or `nudge_x`/`nudge_y`", call. = FALSE)
}
position <- ggplot2::position_nudge(nudge_x, nudge_y)
}
if (is.null(mapping)) mapping <- ggplot2::aes(label = name)
if (is.null(mapping$label)) mapping$label <- substitute(name)
ggplot2::layer(
data = data,
mapping = mapping,
stat = StatNodes,
geom = ggplot2::GeomLabel,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
parse = parse,
na.rm = na.rm,
...
)
)
}
#' Repulsive textual annotations
#'
#' These functions are minor modifications of those in the ggrepel package.
#' `geom_dag_text_repel()` adds text directly to the plot.
#' `geom_dag_label_repel()` draws a rectangle underneath the text, making it
#' easier to read. The text labels repel away from each other and away from the
#' data points. `geom_dag_label_repel2()` is a slightly stylized version of
#' geom_dag_label_repel()` that often looks better on DAGs.
#'
#' @inheritParams ggrepel::geom_text_repel
#' @inheritParams ggrepel::geom_label_repel
#' @param fontface A character vector. Default is "bold"
#' @param segment.color,segment.size See [ggrepel::geom_text_repel()]
#'
#' @importFrom purrr %||%
#' @export
#'
#' @examples
#' library(ggplot2)
#' g <- dagify(
#' m ~ x + y,
#' y ~ x,
#' exposure = "x",
#' outcome = "y",
#' latent = "m",
#' labels = c("x" = "Exposure", "y" = "Outcome", "m" = "Collider")
#' )
#'
#' g %>%
#' tidy_dagitty() %>%
#' ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
#' geom_dag_edges() +
#' geom_dag_point() +
#' geom_dag_text_repel(aes(label = name), show.legend = FALSE) +
#' theme_dag()
#'
#' g %>%
#' tidy_dagitty() %>%
#' dag_label(labels = c(
#' "x" = "This is the exposure",
#' "y" = "Here's the outcome",
#' "m" = "Here is where they collide"
#' )) %>%
#' ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
#' geom_dag_edges() +
#' geom_dag_point() +
#' geom_dag_text() +
#' geom_dag_label_repel(
#' aes(label = label, fill = label),
#' col = "white",
#' show.legend = FALSE
#' ) +
#' theme_dag()
#'
#' @rdname repel
#' @name ggrepel functions
geom_dag_text_repel <- function(
mapping = NULL,
data = NULL,
parse = FALSE,
...,
box.padding = 1.25,
point.padding = 1.5,
segment.color = "#666666",
fontface = "bold",
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 = StatNodesRepel,
geom = ggrepel::GeomTextRepel,
position = "identity",
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
parse = parse,
na.rm = na.rm,
box.padding = box.padding,
point.padding = point.padding,
segment.colour = segment.color %||% segment.colour,
segment.size = segment.size,
fontface = fontface,
arrow = arrow,
force = force,
max.iter = max.iter,
nudge_x = nudge_x,
nudge_y = nudge_y,
segment.alpha = 1,
...
)
)
}
#' @rdname repel
#' @export
#'
#' @importFrom purrr %||%
geom_dag_label_repel <- function(
mapping = NULL,
data = NULL,
parse = FALSE,
...,
box.padding = grid::unit(1.25, "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 = 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,
...
)
)
}
#' @rdname repel
#' @export
geom_dag_label_repel2 <- function(
mapping = NULL,
data = NULL,
box.padding = 2,
max.overlaps = Inf,
label.size = NA,
...) {
geom_dag_label_repel(
mapping = mapping,
data = data,
box.padding = box.padding,
max.overlaps = max.overlaps,
label.size = label.size,
...
)
}
filter_direction <- function(.direction) {
function(x) {
x <- dplyr::filter(x, direction == .direction)
if ("collider_line" %in% names(x)) x <- dplyr::filter(x, !collider_line)
x
}
}
#' Directed and bidirected DAG edges
#'
#' @param mapping Set of aesthetic mappings created by aes() or aes_(). If
#' specified and inherit.aes = TRUE (the default), it is combined with the
#' default mapping at the top level of the plot. You must supply mapping if
#' there is no plot mapping.
#' @param data_directed,data_bidirected The data to be displayed in this layer.
#' There are three options: If NULL, the default, the data is inherited from
#' the plot data as specified in the call to ggplot(). A data.frame, or other
#' object, will override the plot data. All objects will be fortified to
#' produce a data frame. See fortify() for which variables will be created. A
#' function will be called with a single argument, the plot data. The return
#' value must be a data.frame., and will be used as the layer data.
#' @param curvature The bend of the curve. 1 approximates a halfcircle while 0
#' will give a straight line. Negative number will change the direction of the
#' curve. Only used if layout circular = FALSE.
#' @param arrow_directed,arrow_bidirected specification for arrow heads, as
#' created by arrow()
#' @param position Position adjustment, either as a string, or the result of a
#' call to a position adjustment function.
#' @param na.rm If FALSE (the default), removes missing values with a warning.
#' If TRUE silently removes missing values
#' @param show.legend logical. Should this layer be included in the legends? NA,
#' the default, includes if any aesthetics are mapped. FALSE never includes,
#' and TRUE always includes. It can also be a named logical vector to finely
#' select the aesthetics to display.
#' @param inherit.aes If FALSE, overrides the default aesthetics, rather than
#' combining with them. This is most useful for helper functions that define
#' both data and aesthetics and shouldn't inherit behaviour from the default
#' plot specification, e.g. borders().
#' @param fold Logical. Should arcs appear on the same side of the nodes despite
#' different directions. Default to FALSE.
#' @param ... Other arguments passed to ggraph::geom_edge_*()
#'
#' @section Aesthetics:
#' `geom_dag_edges` understand the following aesthetics. Bold aesthetics are
#' required.
#'
#' - **x**
#' - **y**
#' - **xend**
#' - **yend**
#' - edge_colour
#' - edge_width
#' - edge_linetype
#' - edge_alpha
#' - start_cap
#' - end_cap
#' - label
#' - label_pos
#' - label_size
#' - angle
#' - hjust
#' - vjust
#' - family
#' - fontface
#' - lineheight
#'
#' `geom_dag_edges` also uses `geom_dag_edges_arc`, which requires the
#' **circular** aesthetic, but this is automatically set.
#'
#' @export
#'
#' @examples
#' library(ggplot2)
#' dagify(
#' y ~ x + z2 + w2 + w1,
#' x ~ z1 + w1,
#' z1 ~ w1 + v,
#' z2 ~ w2 + v,
#' w1 ~ ~w2
#' ) %>%
#' ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
#' geom_dag_edges() +
#' geom_dag_point() +
#' geom_dag_text() +
#' theme_dag()
#'
geom_dag_edges <- function(
mapping = NULL,
data_directed = filter_direction("->"),
data_bidirected = filter_direction("<->"),
curvature = 0.3,
arrow_directed = grid::arrow(length = grid::unit(5, "pt"), type = "closed"),
arrow_bidirected = grid::arrow(length = grid::unit(5, "pt"), ends = "both", type = "closed"),
position = "identity",
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
fold = FALSE,
...) {
list(
geom_dag_edges_link(
mapping,
data = data_directed,
arrow = arrow_directed,
position = position,
na.rm = na.rm,
show.legend = show.legend,
inherit.aes = inherit.aes,
...
),
geom_dag_edges_arc(
mapping,
data = data_bidirected,
arrow = arrow_bidirected,
curvature = curvature,
position = position,
na.rm = na.rm,
show.legend = show.legend,
inherit.aes = inherit.aes,
fold = fold,
...
)
)
}
#' Directed DAG edges
#'
#' @param mapping Set of aesthetic mappings created by aes() or aes_(). If
#' specified and inherit.aes = TRUE (the default), it is combined with the
#' default mapping at the top level of the plot. You must supply mapping if
#' there is no plot mapping.
#' @param data The data to be displayed in this layer.
#' There are three options: If NULL, the default, the data is inherited from
#' the plot data as specified in the call to ggplot(). A data.frame, or other
#' object, will override the plot data. All objects will be fortified to
#' produce a data frame. See fortify() for which variables will be created. A
#' function will be called with a single argument, the plot data. The return
#' value must be a data.frame., and will be used as the layer data.
#' @param curvature The bend of the curve. 1 approximates a halfcircle while 0
#' will give a straight line. Negative number will change the direction of the
#' curve. Only used if layout circular = FALSE.
#' @param arrow specification for arrow heads, as created by arrow()
#' @param position Position adjustment, either as a string, or the result of a
#' call to a position adjustment function.
#' @param na.rm If FALSE (the default), removes missing values with a warning.
#' If TRUE silently removes missing values
#' @param show.legend logical. Should this layer be included in the legends? NA,
#' the default, includes if any aesthetics are mapped. FALSE never includes,
#' and TRUE always includes. It can also be a named logical vector to finely
#' select the aesthetics to display.
#' @param inherit.aes If FALSE, overrides the default aesthetics, rather than
#' combining with them. This is most useful for helper functions that define
#' both data and aesthetics and shouldn't inherit behaviour from the default
#' plot specification, e.g. borders().
#' @param fold Logical. Should arcs appear on the same side of the nodes despite
#' different directions. Default to FALSE.
#' @param ... Other arguments passed to ggraph::geom_edge_*()
#'
#' @section Aesthetics:
#' `geom_dag_edges_link`, `geom_dag_edges_arc`, `geom_dag_edges_diagonal`, and
#' `geom_dag_edges_fan` understand the following aesthetics. Bold aesthetics are
#' required.
#'
#' - **x**
#' - **y**
#' - **xend**
#' - **yend**
#' - edge_colour
#' - edge_width
#' - edge_linetype
#' - edge_alpha
#' - start_cap
#' - end_cap
#' - label
#' - label_pos
#' - label_size
#' - angle
#' - hjust
#' - vjust
#' - family
#' - fontface
#' - lineheight
#'
#' `geom_dag_edges_arc` and `geom_dag_edges_diagonal` also require
#' **circular**, but this is automatically set.
#'
#' `geom_dag_edges_fan` requires **to** and **from**, but these are also
#' automatically set.
#'
#' @export
#'
#' @examples
#' library(ggplot2)
#' p <- dagify(
#' y ~ x + z2 + w2 + w1,
#' x ~ z1 + w1,
#' z1 ~ w1 + v,
#' z2 ~ w2 + v,
#' L ~ w1 + w2
#' ) %>%
#' ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
#' geom_dag_point() +
#' geom_dag_text() +
#' theme_dag()
#'
#' p + geom_dag_edges_link()
#' p + geom_dag_edges_arc()
#' p + geom_dag_edges_diagonal()
#' p + geom_dag_edges_fan()
#'
#' @rdname geom_dag_edge_functions
#' @name DAG Edges
geom_dag_edges_link <- function(
mapping = NULL,
data = NULL,
arrow = grid::arrow(length = grid::unit(5, "pt"), type = "closed"),
position = "identity",
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
...) {
ggplot2::layer(
mapping = mapping,
geom = GeomDAGEdgePath,
data = data,
stat = StatEdgeLink,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
check.aes = FALSE,
params = list(arrow = arrow, interpolate = FALSE, na.rm = na.rm, ...)
)
}
#' @rdname geom_dag_edge_functions
#' @export
geom_dag_edges_arc <- function(
mapping = NULL,
data = NULL,
curvature = .5,
arrow = grid::arrow(length = grid::unit(5, "pt"), type = "closed"),
position = "identity",
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
fold = FALSE,
n = 100,
lineend = "butt",
linejoin = "round",
linemitre = 1,
label_colour = "black",
label_alpha = 1,
label_parse = FALSE,
check_overlap = FALSE,
angle_calc = "rot",
force_flip = TRUE,
label_dodge = NULL,
label_push = NULL,
...) {
if (is.null(mapping)) {
mapping <- ggplot2::aes(circular = circular)
} else if (is.null(mapping$circular)) {
mapping$circular <- substitute(circular)
}
ggplot2::layer(
mapping = mapping,
geom = GeomDAGEdgePath,
data = data,
stat = StatEdgeArc,
check.aes = FALSE,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
arrow = arrow,
strength = curvature,
interpolate = FALSE,
fold = fold,
na.rm = na.rm,
n = n,
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre,
label_colour = label_colour,
label_alpha = label_alpha,
label_parse = label_parse,
check_overlap = check_overlap,
angle_calc = angle_calc,
force_flip = force_flip,
label_dodge = label_dodge,
label_push = label_push,
...
)
)
}
#' @inheritParams ggraph::geom_edge_diagonal
#'
#' @rdname geom_dag_edge_functions
#' @export
geom_dag_edges_diagonal <- function(
mapping = NULL,
data = NULL,
position = "identity",
arrow = grid::arrow(length = grid::unit(5, "pt"), type = "closed"),
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
curvature = 1,
n = 100,
lineend = "butt",
linejoin = "round",
linemitre = 1,
label_colour = "black",
label_alpha = 1,
label_parse = FALSE,
check_overlap = FALSE,
angle_calc = "rot",
force_flip = TRUE,
label_dodge = NULL,
label_push = NULL,
...) {
if (is.null(mapping)) {
mapping <- ggplot2::aes(circular = circular)
} else if (is.null(mapping$circular)) {
mapping$circular <- substitute(circular)
}
ggplot2::layer(
data = data,
mapping = mapping,
stat = StatEdgeDiagonal,
geom = GeomDAGEdgePath,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
arrow = arrow,
na.rm = na.rm,
interpolate = FALSE,
n = n,
lineend = lineend,
flipped = FALSE,
strength = curvature,
linejoin = linejoin,
linemitre = linemitre,
label_colour = label_colour,
label_alpha = label_alpha,
label_parse = label_parse,
check_overlap = check_overlap,
angle_calc = angle_calc,
force_flip = force_flip,
label_dodge = label_dodge,
label_push = label_push,
...
)
)
}
#' @inheritParams ggraph::geom_edge_fan
#'
#' @rdname geom_dag_edge_functions
#' @export
geom_dag_edges_fan <- function(
mapping = NULL,
data = NULL,
position = "identity",
arrow = grid::arrow(length = grid::unit(5, "pt"), type = "closed"),
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
spread = .7,
n = 100,
lineend = "butt",
linejoin = "round",
linemitre = 1,
label_colour = "black",
label_alpha = 1,
label_parse = FALSE,
check_overlap = FALSE,
angle_calc = "rot",
force_flip = TRUE,
label_dodge = NULL,
label_push = NULL,
...) {
if (is.null(mapping)) {
mapping <- ggplot2::aes(from = name, to = to)
} else if (is.null(mapping$from)) {
mapping$from <- substitute(name)
mapping$to <- substitute(to)
}
ggplot2::layer(
data = data,
mapping = mapping,
stat = StatEdgeFan,
geom = GeomDAGEdgePath,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
arrow = arrow,
na.rm = na.rm,
interpolate = FALSE,
n = n,
lineend = lineend,
strength = spread,
linejoin = linejoin,
linemitre = linemitre,
label_colour = label_colour,
label_alpha = label_alpha,
label_parse = label_parse,
check_overlap = check_overlap,
angle_calc = angle_calc,
force_flip = force_flip,
label_dodge = label_dodge,
label_push = label_push,
...
)
)
}
#' Edges for paths activated by stratification on colliders
#'
#' Adjusting for a collider activates pathways between the parent of the
#' collider. This geom adds a curved edge between any such parent nodes.
#'
#' @inheritParams ggplot2::geom_curve
#' @param linewidth a numeric vector of length 1. Edge width
#' @param size deprecated. Please use `linewidth`.
#'
#' @export
#'
#' @examples
#' library(dagitty)
#' library(ggplot2)
#' dagify(m ~ a + b, x ~ a, y ~ b) %>%
#' tidy_dagitty() %>%
#' control_for("m") %>%
#' ggplot(aes(x = x, y = y, xend = xend, yend = yend, shape = adjusted)) +
#' geom_dag_edges() +
#' geom_dag_collider_edges() +
#' geom_dag_point() +
#' geom_dag_text() +
#' theme_dag() +
#' scale_adjusted()
geom_dag_collider_edges <- function(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
linewidth = .6,
size = NULL,
curvature = 0.5,
angle = 90,
ncp = 5,
arrow = NULL,
lineend = "butt",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
if (is.null(data)) data <- function(x) dplyr::filter(x, direction == "<->", collider_line)
if (is.null(mapping)) mapping <- ggplot2::aes(linetype = factor(collider_line, levels = TRUE, "activated by \nadjustment \nfor collider"))
if (is.null(mapping$linetype)) mapping$linetype <- substitute(factor(collider_line, levels = TRUE, "activated by \nadjustment \nfor collider"))
if (!is.null(size)) {
warning("`size` is deprecated for lines. Please use `linewidth`")
linewidth <- size
}
params <- list(
arrow = arrow,
curvature = curvature,
angle = angle,
ncp = ncp,
lineend = lineend,
na.rm = na.rm,
...
)
if (ggplot2_version() >= "3.3.6.9000") {
params$linewidth <- linewidth
} else {
params$size <- linewidth
}
ggplot2::layer(
data = data,
mapping = mapping,
stat = stat,
geom = ggplot2::GeomCurve,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = params
)
}
#' Define Aesthetics for Directed Acyclic Graphs (DAGs)
#'
#' `aes_dag()` is a wrapper around `aes()` that specifies `x`, `y`, `xend`, and
#' `yend`, which are required for most DAG visualizations. It merges any
#' additional aesthetics, e.g. `color` or `shape`, with the default aesthetic
#' mappings.
#'
#' @param ... Additional aesthetic mappings passed as arguments. These can
#' include any aesthetic supported by ggplot2 (e.g., color, size, shape).
#'
#' @return A `ggplot2` aesthetic mapping object that includes both the default
#' DAG aesthetics and any user-specified aesthetics.
#'
#' @examples
#' library(ggplot2)
#' confounder_triangle() %>%
#' dag_adjustment_sets() %>%
#' ggplot(aes_dag(color = adjusted)) +
#' geom_dag() +
#' facet_wrap(~set)
#'
#' @export
aes_dag <- function(...) {
addtl_aes <- ggplot2::aes(...)
default_aes <- ggplot2::aes(
x = x,
y = y,
xend = xend,
yend = yend
)
default_aes[names(addtl_aes)] <- addtl_aes
default_aes
}
#' Add common DAG layers to a ggplot
#'
#' `geom_dag()` is a helper function that adds common DAG layers to a ggplot.
#' The purpose of `geom_dag()` is to simplify making custom DAGs. Most custom
#' DAGs need the same basic layers, and so this function greatly reduces typing.
#' It is not a true geom in that it adds many types of geoms to the plot (by
#' default, edges, nodes, and text). While the underlying layers, all available
#' in ggdag, are true geoms, we usually need a consistent set of layers to make
#' a DAG. `geom_dag()` provides this. Because `geom_dag()` is not a true geom,
#' 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",
#' "diagonal".
#' @param node_size The size of the nodes.
#' @param text_size The size of the text.
#' @param label_size The size of the labels.
#' @param text_col The color of the text.
#' @param label_col The color of the labels.
#' @param edge_width The width of the edges.
#' @param edge_cap The size of edge caps (the distance between the arrowheads
#' and the node borders).
#' @param arrow_length The length of arrows on edges.
#' @param use_edges A logical value. Include a `geom_dag_edges*()` function? If
#' `TRUE`, which is determined by `edge_type`.
#' @param use_nodes A logical value. Include `geom_dag_point()`?
#' @param use_stylized A logical value. Include `geom_dag_node()`?
#' @param use_text A logical value. Include `geom_dag_text()`?
#' @param use_labels A logical value. Include `geom_dag_label_repel()`?
#' @param label The bare name of a column to use for `geom_dag_label_repel()`.
#' If `use_labels = TRUE`, the default is to use `label`.
#' @param text The bare name of a column to use for `geom_dag_text()`. If
#' `use_text = TRUE`, the default is to use `name`.
#' @param node Deprecated.
#' @param stylized Deprecated.
#'
#' @return A list of ggplot2 layer elements
#'
#' @examples
#' # Basic usage with ggdag