2

I am trying to draw a standard triangular mediation diagram using DiagrammeR in R (it can also interpret graphviz code). On the whole, it's working fine but the edge label text gets placed oddly. The bottom edge label is not centered and the two angled edge labels are positioned at different heights (see the red lines in the diagram below). Is there a way to manually assign positions to edge text or get something more consistent?`

library(DiagrammeR)

# Create a node data frame (ndf)
ndf <- create_node_df(
  n         = 3,
  label     = c("Experimental\nTreatment", "Some\nMediator", "Outcome\nof Interest"),
  shape     = rep("rectangle", 3),
  style     = "empty",
  fontsize  = 6,
  fixedsize = TRUE,
  height    = .5,
  width     = .75,
  color     = "gray40",
  x         = c(1, 2, 3),
  y         = c(1, 2, 1)
)

# Create an edge data frame (edf)
edf <- create_edge_df(
  from     = c(1, 1, 2),
  to       = c(2, 3, 3),
  label    = c("1.1*", "2.0*", "-0.33***"),
  fontsize = 6,
  minlen   = 1,
  color    = "gray40",
  )

# Create a graph with the ndf and edf
graph <- create_graph(
  nodes_df = ndf,
  edges_df = edf
  )

graph %>%
  render_graph()

Example mediation diagram showing edge label text not lining up properly

Omar Wasow
  • 1,870
  • 24
  • 24

2 Answers2

1

I would also love to know the answer to this.

I have tried to delete the default "layout" graph attribute and set direction of graph layout via:

graphAttr <- DiagrammeR::get_global_graph_attrs(graph)

But, I get the following error: Error: 'get_global_graph_attrs' is not an exported object from 'namespace:DiagrammeR'

....which it is, consequently, I am stuck.

K.McM
  • 11
  • 2
1

After fiddling with DiagrammeR for a while, I ended up switching to the LaTeX diagramming package TikZ. It allows for enormous control of every aspect of the diagram but can be overwhelming. TikZ would require a bit of tweaking, also, to get diagrams into HTML output if that's important. For a presentation using LaTeX's Beamer class for slides, generating TikZ code with a function in R worked well. That TikZ code was then written to a text file that could be imported automatically via the LaTeX command \input{path/to/file.txt} or just copied and pasted into my slides.

See the code for two DiagrammeR solutions and the TikZ solution, here: https://stackoverflow.com/a/64886536/893399

Omar Wasow
  • 1,870
  • 24
  • 24