DSTO
DSTO

Reputation: 285

Text on top of the arrows in GGDAG

I am trying to plot a ggdag plot (Directed Acyclic Graphs in R) using the wonderful ggdag package in R (https://github.com/malcolmbarrett/ggdag). Here is an example script.

library(ggdag)
library(ggplot2)
example <- ggdag::dagify(d ~ p,m ~ d,y ~ d,y ~ m)
ggdag::ggdag(example)+theme_dag()

I was wondering if there is a way to add some text on top of the arrows so that the plot looks like

enter image description here

Thanks!

Upvotes: 1

Views: 515

Answers (1)

Quinten
Quinten

Reputation: 41235

An option could be using annotate. Keep in mind that you need to use set.seed, because the dag will keep chaning from position randomly. You can use the following code:

library(ggdag)
library(ggplot2)
example <- ggdag::dagify(d ~ p,m ~ d,y ~ d,y ~ m)
set.seed(1)
ggdag::ggdag(example)+theme_dag() +
  annotate("text", x = c(0.4, 0), , y = c(2.6, 2.3), , label = "beta==x p =xx")

Created on 2022-07-28 by the reprex package (v2.0.1)

Upvotes: 3

Related Questions