diff --git a/NAMESPACE b/NAMESPACE index 17cd659..3cfaef4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -73,6 +73,7 @@ export(geom_dag_edges_fan) export(geom_dag_edges_link) export(geom_dag_label) export(geom_dag_label_repel) +export(geom_dag_label_repel2) export(geom_dag_node) export(geom_dag_point) export(geom_dag_text) diff --git a/R/geom_dag.R b/R/geom_dag.R index 663fa04..264ddaf 100644 --- a/R/geom_dag.R +++ b/R/geom_dag.R @@ -237,11 +237,12 @@ geom_dag_label <- function(mapping = NULL, data = NULL, #' 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. +#' 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 @@ -291,7 +292,7 @@ geom_dag_text_repel <- function(mapping = NULL, data = NULL, parse = FALSE, ..., - box.padding = 0.35, + box.padding = 1.25, point.padding = 1.5, segment.color = "#666666", fontface = "bold", @@ -325,6 +326,7 @@ geom_dag_text_repel <- function(mapping = NULL, max.iter = max.iter, nudge_x = nudge_x, nudge_y = nudge_y, + segment.alpha = 1, ... ) ) @@ -337,7 +339,7 @@ geom_dag_text_repel <- function(mapping = NULL, geom_dag_label_repel <- function(mapping = NULL, data = NULL, parse = FALSE, ..., - box.padding = grid::unit(0.35, "lines"), + 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"), @@ -375,11 +377,26 @@ geom_dag_label_repel <- function(mapping = NULL, data = NULL, 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) diff --git a/man/repel.Rd b/man/repel.Rd index c34d2af..7a6e06b 100644 --- a/man/repel.Rd +++ b/man/repel.Rd @@ -4,6 +4,7 @@ \alias{ggrepel functions} \alias{geom_dag_text_repel} \alias{geom_dag_label_repel} +\alias{geom_dag_label_repel2} \title{Repulsive textual annotations} \usage{ geom_dag_text_repel( @@ -11,7 +12,7 @@ geom_dag_text_repel( data = NULL, parse = FALSE, ..., - box.padding = 0.35, + box.padding = 1.25, point.padding = 1.5, segment.color = "#666666", fontface = "bold", @@ -31,7 +32,7 @@ geom_dag_label_repel( data = NULL, parse = FALSE, ..., - box.padding = grid::unit(0.35, "lines"), + 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"), @@ -47,6 +48,15 @@ geom_dag_label_repel( show.legend = NA, inherit.aes = TRUE ) + +geom_dag_label_repel2( + mapping = NULL, + data = NULL, + box.padding = 2, + max.overlaps = Inf, + label.size = NA, + ... +) } \arguments{ \item{mapping}{Set of aesthetic mappings created by \code{\link[ggplot2]{aes}} or @@ -117,13 +127,19 @@ to 0.15. (Default unit is lines, but other units can be specified by passing \code{unit(x, "units")}).} \item{label.size}{Size of label border, in mm.} + +\item{max.overlaps}{Exclude text labels when they overlap too many other +things. For each text label, we count how many other text labels or other +data points it overlaps, and exclude the text label if it has too many overlaps. +Defaults to 10.} } \description{ -These functions are minor modifications of those in the \code{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. +These functions are minor modifications of those in the ggrepel package. +\code{geom_dag_text_repel()} adds text directly to the plot. +\code{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. \code{geom_dag_label_repel2()} is a slightly stylized version of +geom_dag_label_repel()` that often looks better on DAGs. } \examples{ library(ggplot2) diff --git a/tests/testthat/_snaps/geom_dag/geom-dag-edges-arc-is-arcy.svg b/tests/testthat/_snaps/geom_dag/geom-dag-edges-arc-is-arcy.svg index f781663..c0d3b15 100644 --- a/tests/testthat/_snaps/geom_dag/geom-dag-edges-arc-is-arcy.svg +++ b/tests/testthat/_snaps/geom_dag/geom-dag-edges-arc-is-arcy.svg @@ -21,71 +21,71 @@ - - + + - - - - - - - - - - -L -v -w1 -w2 -x -y -z1 -z2 - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + +L +v +w1 +w2 +x +y +z1 +z2 + + + + + + + + + + + + + + + + + + + + + + + + + --2 --1 -0 -1 - - - - - - - --1 -0 -1 -x +0 +1 +2 + + + + + + + +-1 +0 +1 +2 +x y -geom_dag_edges_arc() is arcy +geom_dag_edges_arc() is arcy diff --git a/tests/testthat/_snaps/geom_dag/geom-dag-edges-diagonal-is-arcy.svg b/tests/testthat/_snaps/geom_dag/geom-dag-edges-diagonal-is-arcy.svg index 4f31232..71e5cdb 100644 --- a/tests/testthat/_snaps/geom_dag/geom-dag-edges-diagonal-is-arcy.svg +++ b/tests/testthat/_snaps/geom_dag/geom-dag-edges-diagonal-is-arcy.svg @@ -21,75 +21,71 @@ - - + + - - - - - - - - - - -L -v -w1 -w2 -x -y -z1 -z2 - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + +L +v +w1 +w2 +x +y +z1 +z2 + + + + + + + + + + + + + + + + + + + + + + + + + --1 -0 -1 - - - - - - - - - --1.5 --1.0 --0.5 -0.0 -0.5 -1.0 -x +0 +1 +2 + + + + + + + +-1 +0 +1 +2 +x y -geom_dag_edges_diagonal() is arcy +geom_dag_edges_diagonal() is arcy diff --git a/tests/testthat/_snaps/geom_dag/geom-dag-edges-fan-is-fany.svg b/tests/testthat/_snaps/geom_dag/geom-dag-edges-fan-is-fany.svg index 96813db..35953f3 100644 --- a/tests/testthat/_snaps/geom_dag/geom-dag-edges-fan-is-fany.svg +++ b/tests/testthat/_snaps/geom_dag/geom-dag-edges-fan-is-fany.svg @@ -21,75 +21,71 @@ - - + + - - - - - - - - - - -L -v -w1 -w2 -x -y -z1 -z2 - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + +L +v +w1 +w2 +x +y +z1 +z2 + + + + + + + + + + + + + + + + + + + + + + + + + --1 -0 -1 - - - - - - - - - --1.5 --1.0 --0.5 -0.0 -0.5 -1.0 -x +0 +1 +2 + + + + + + + +-1 +0 +1 +2 +x y -geom_dag_edges_fan() is fany +geom_dag_edges_fan() is fany diff --git a/tests/testthat/_snaps/geom_dag/geom-dag-edges-link-is-straight.svg b/tests/testthat/_snaps/geom_dag/geom-dag-edges-link-is-straight.svg index 395f012..a5a6500 100644 --- a/tests/testthat/_snaps/geom_dag/geom-dag-edges-link-is-straight.svg +++ b/tests/testthat/_snaps/geom_dag/geom-dag-edges-link-is-straight.svg @@ -21,75 +21,71 @@ - - + + - - - - - - - - - - -L -v -w1 -w2 -x -y -z1 -z2 - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + +L +v +w1 +w2 +x +y +z1 +z2 + + + + + + + + + + + + + + + + + + + + + + + + + --1 -0 -1 - - - - - - - - - --1.5 --1.0 --0.5 -0.0 -0.5 -1.0 -x +0 +1 +2 + + + + + + + +-1 +0 +1 +2 +x y -geom_dag_edges_link() is straight +geom_dag_edges_link() is straight diff --git a/tests/testthat/_snaps/geom_dag/geom-dag-label-labels.svg b/tests/testthat/_snaps/geom_dag/geom-dag-label-labels.svg index 284041f..6b016a4 100644 --- a/tests/testthat/_snaps/geom_dag/geom-dag-label-labels.svg +++ b/tests/testthat/_snaps/geom_dag/geom-dag-label-labels.svg @@ -27,44 +27,38 @@ - - - - - - - - - - -Collider - -Exposure - -Outcome + + + + + + + + + + +Collider + +Exposure + +Outcome --0.4 --0.2 -0.0 -0.2 -0.4 -0.6 - - - - - - - - - - --2.00 --1.75 --1.50 --1.25 +-0.9 +-0.6 +-0.3 + + + + + + + +-0.75 +-0.50 +-0.25 +0.00 x y geom_dag_label() labels diff --git a/tests/testthat/_snaps/geom_dag/geom-dag-label-repel-repels-labels.svg b/tests/testthat/_snaps/geom_dag/geom-dag-label-repel-repels-labels.svg index 80ee954..8fb9f0a 100644 --- a/tests/testthat/_snaps/geom_dag/geom-dag-label-repel-repels-labels.svg +++ b/tests/testthat/_snaps/geom_dag/geom-dag-label-repel-repels-labels.svg @@ -39,12 +39,13 @@ m x y - -Here is where they collide - -This is the exposure - -Here's the outcome + + +Here is where they collide + +This is the exposure + +Here's the outcome diff --git a/tests/testthat/_snaps/geom_dag/geom-dag-label-repel2-repels-labels.svg b/tests/testthat/_snaps/geom_dag/geom-dag-label-repel2-repels-labels.svg new file mode 100644 index 0000000..70e90f2 --- /dev/null +++ b/tests/testthat/_snaps/geom_dag/geom-dag-label-repel2-repels-labels.svg @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +m + +x + +y + + + +-1.00 +-0.75 +-0.50 +-0.25 +0.00 + + + + + + + + + +-1.50 +-1.25 +-1.00 +-0.75 +x +y +geom_dag_label_repel2() repels labels + + diff --git a/tests/testthat/_snaps/geom_dag/geom-dag-text-repel-repels-names.svg b/tests/testthat/_snaps/geom_dag/geom-dag-text-repel-repels-names.svg index 0ce024b..3b7c7e5 100644 --- a/tests/testthat/_snaps/geom_dag/geom-dag-text-repel-repels-names.svg +++ b/tests/testthat/_snaps/geom_dag/geom-dag-text-repel-repels-names.svg @@ -36,9 +36,12 @@ -m -x -y + + + +m +x +y diff --git a/tests/testthat/test-geom_dag.R b/tests/testthat/test-geom_dag.R index 63d9217..4e59c54 100644 --- a/tests/testthat/test-geom_dag.R +++ b/tests/testthat/test-geom_dag.R @@ -14,7 +14,7 @@ test_that("repelled labels work", { 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) + geom_dag_text_repel(aes(label = name), show.legend = FALSE, seed = 1234) p2 <- g %>% tidy_dagitty() %>% @@ -27,13 +27,27 @@ test_that("repelled labels work", { geom_dag_edges() + geom_dag_point() + geom_dag_text() + - geom_dag_label_repel(aes(label = label, fill = label), - col = "white", show.legend = FALSE + geom_dag_label_repel( + aes(label = label, fill = label), + col = "white", + show.legend = FALSE, + seed = 1234 ) + p3 <- g %>% + tidy_dagitty() %>% + ggplot(aes(x = x, y = y, xend = xend, yend = yend)) + + geom_dag_edges() + + geom_dag_point() + + geom_dag_label_repel2( + aes(label = name), + show.legend = FALSE, + seed = 1234 + ) expect_doppelganger("geom_dag_text_repel() repels names", p1) expect_doppelganger("geom_dag_label_repel() repels labels", p2) + expect_doppelganger("geom_dag_label_repel2() repels labels", p3) }) test_that("different edge types work", {