1

I want to draw dot graphs in R using diagrammeR, in which some nodes have arrows pointing to edges.

For example, this diagram

library(DiagrammeR)
grViz("
digraph PrimC{
  graph [layout = dot]
  node [shape = circle]
  A B
  A -> B [label = 'Rate']
  }")

I would like to look something like this one (edited by hand). NB these types of diagram are commonly used to show when a rate is affected by the quantity of something else.

This answer suggests that using invisible nodes can achieve this, but does not actually show how. It links to this answer, which shows how to use invisible nodes for a somewhat different type of graph in which several edges meet at a single point, but does not include what I'm looking for with an edge pointing to the midpoint of another edge.

I've tried a number of different combinations of invisible nodes and edges, but can't get any of them close to what I want.

Here's one messy attempt as an example

grViz("
digraph PrimC{
  graph [layout = dot]

  node [shape = circle]
  A B
  node[shape=none, width=0, height=0, label=''];
  p1
  node [shape = circle]
  B

  A -> p1 [label = 'Rate']
  p1 -> B
  B -> p1;
  {rank=same; A -> p1; B -> p1;}
  }")

Is there any way to get this to work. Open to suggestions using other approaches than diagrammeR and graphviz, if there's a better solution out there.

Community
  • 1
  • 1
dww
  • 30,425
  • 5
  • 68
  • 111

1 Answers1

3

I'm not sure, if this will work in general, but for this case, I would do it like:

digraph PrimC{

  graph [layout = dot]
  rankdir = LR
  node[shape = circle]
  {rank=same 
    A 
    B
    p1[shape=none, width=0, height=0, label='']
  }

  A -> p1 [label = 'Rate', arrowhead=none]
  p1 -> B
  B:ne -> p1[constraint=no, arrowType=normal]

}

enter image description here

bergant
  • 7,122
  • 1
  • 20
  • 24
  • Thanks, it works great on my example. But I think my minimal example was perhaps a little too trivial. With more complex graphs the routing of the edges becomes very convoluted, and the edges take sharp discontinuous turns at the invisible nodes (particularly if you try to point to an edge that is curved rather than straight). I'll wait to see if any other answers turn up, but maybe this is as good as it gets. I'll accept this if nothing better after a few days. – dww Feb 15 '17 at 16:50
  • Maybe try with intermediate nodes like in https://caretdashcaret.com/tag/graphviz/ (not directly pointing from connected node to edge) – bergant Feb 16 '17 at 09:25