0

I am trying to create a DAG with several nodes similar to the one provided by Dr. Andrew Heiss. I like this graph in particular because the labels are distanced from the node so that the DAG is de-cluttered. However, despite providing the same syntax, I am unable to replicate the labels attached to the nodes with a line like the reference one does.

Here is the syntax provided by the reference DAG:

library(tidyverse)  
library(ggdag)  
library(dagitty)  
library(broom) 

node_details <- tribble(
  ~name, ~label, ~x, ~y,
  "math_camp", "Math camp", 2, 1,
  "final_grade", "Final grade", 4, 1,
  "needs_camp", "Needs camp", 1, 2,
  "gre_quant", "GRE quantitative", 2.5, 2,
  "gre_verbal", "GRE verbal", 5, 2,
  "background", "Background", 2, 3,
  "undergraduate_gpa", "Undergraduate GPA", 4, 3
)

node_labels <- node_details$label
names(node_labels) <- node_details$name

math_camp_dag <- dagify(final_grade ~ math_camp + gre_quant + gre_verbal + 
                          undergraduate_gpa + background,
                        math_camp ~ needs_camp, 
                        needs_camp ~ background + undergraduate_gpa + gre_quant,
                        gre_quant ~ background + undergraduate_gpa,
                        gre_verbal ~ background + undergraduate_gpa,
                        undergraduate_gpa ~ background,
                        exposure = "math_camp",
                        outcome = "final_grade",
                        latent = "background",
                        coords = node_details,
                        labels = node_labels)

# Turn DAG into a tidy data frame for plotting
math_camp_dag_tidy <- math_camp_dag %>% 
  tidy_dagitty() %>%
  node_status()   # Add column for exposure/outcome/latent

status_colors <- c(exposure = "#0074D9", outcome = "#FF4136", latent = "grey50")

# Fancier graph
ggplot(math_camp_dag_tidy, aes(x = x, y = y, xend = xend, yend = yend)) +
  geom_dag_edges() +
  geom_dag_point(aes(color = status)) +
  geom_dag_label_repel(aes(label = label, fill = status), seed = 1234,
                       color = "white", fontface = "bold") +
  scale_color_manual(values = status_colors, na.value = "grey20") +
  scale_fill_manual(values = status_colors, na.value = "grey20") +
  guides(color = FALSE, fill = FALSE) + 
  theme_dag()

And the subsequent DAG this code produces:

enter image description here

When I run the same code, my DAG looks like the following:

enter image description here

Note that the labels are no longer distanced from the node, despite using the exact same syntax. I am wondering how I can replicate the labels being distanced from the node as demonstrated in the first DAG.

0 Answers0