ggdag (version 0.1.0)

ggrepel functions: Repulsive textual annotations

Description

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.

Usage

geom_dag_text_repel(mapping = NULL, data = NULL, parse = FALSE, ...,
  box.padding = 0.35, 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)

geom_dag_label_repel(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)

Arguments

mapping

Set of aesthetic mappings created by aes or aes_. If specified and inherit.aes = TRUE (the default), is combined with the default mapping at the top level of the plot. You only need to supply mapping if there isn't a mapping defined for the plot.

data

A data frame. If specified, overrides the default data frame defined at the top level of the plot.

parse

If TRUE, the labels will be parsed into expressions and displayed as described in ?plotmath

...

other arguments passed on to layer. There are three types of arguments you can use here:

  • Aesthetics: to set an aesthetic to a fixed value, like colour = "red" or size = 3.

  • Other arguments to the layer, for example you override the default stat associated with the layer.

  • Other arguments passed on to the stat.

box.padding

Amount of padding around bounding box, as unit or number. Defaults to 0.25. (Default unit is lines, but other units can be specified by passing unit(x, "units")).

point.padding

Amount of padding around labeled point, as unit or number. Defaults to 0. (Default unit is lines, but other units can be specified by passing unit(x, "units")).

segment.color

Colour of the line segment. Defaults to the same colour as the text. In the unlikely event you specify both US and UK spellings of colour, the US spelling will take precedence.

fontface

A character vector. Default is "bold"

segment.size

Width of line segment connecting the data point to the text label, in mm.

arrow

specification for arrow heads, as created by arrow

force

Force of repulsion between overlapping text labels. Defaults to 1.

max.iter

Maximum number of iterations to try to resolve overlaps. Defaults to 2000.

nudge_x

Horizontal and vertical adjustments to nudge the starting position of each text label.

nudge_y

Horizontal and vertical adjustments to nudge the starting position of each text label.

na.rm

If FALSE (the default), removes missing values with a warning. If TRUE silently removes missing values.

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.

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.

label.padding

Amount of padding around label, as unit or number. Defaults to 0.25. (Default unit is lines, but other units can be specified by passing unit(x, "units")).

label.r

Radius of rounded corners, as unit or number. Defaults to 0.15. (Default unit is lines, but other units can be specified by passing unit(x, "units")).

label.size

Size of label border, in mm.

Examples

Run this code
# NOT RUN {
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_node() +
    geom_dag_text_repel(aes(label = name), show.legend = FALSE) +
    theme_dag() +
    scale_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_node() +
    geom_dag_text() +
    geom_dag_label_repel(aes(label = label, fill = label),
      col = "white", show.legend = FALSE) +
    theme_dag() +
    scale_dag()
# }

Run the code above in your browser using DataCamp Workspace