0

How do I add names (or labels) to a DiagrammeR GraphViz layout? I'd like to add names to each dot below, and I'd like them to be in an organized fashion, not overlapping anything else, a la ggrepel style. You can use any names you wish for the following example. This is going to basically be an org chart. Thank you.

library(DiagrammeR)
grViz("
digraph neato {

graph [layout = neato]

node [shape = circle,
      style = filled,
      color = grey,
      label = '']

node [fillcolor = red]
a

node [fillcolor = green]
b c d

node [fillcolor = orange]

edge [color = grey]
a -> {b c d}
b -> {e f g h i j}
c -> {k l m n o p}
d -> {q r s t u v}
}")

grViz neato

Display name
  • 4,153
  • 5
  • 27
  • 75

1 Answers1

1

after the node, add [label = 'your label']. You can declare a node's label separately or inline.

grViz("
digraph neato {

graph [layout = neato]

node [shape = circle,
      style = filled,
      color = grey,
      label = '']

node [fillcolor = red]
a [label='a']

node [fillcolor = green]
b c d

node [fillcolor = orange]

l [label = 'llllllllllllll', fixedsize = true, width = 0.5]

edge [color = grey]
a -> {b c d}
b -> {e f g h i j}
c -> {k l m n o [label = 'o'] p [label = 'p']}
d -> {q r s t u v}
}")

enter image description here

yusuzech
  • 5,896
  • 1
  • 18
  • 33
  • Can I stop the circle sizes from increasing? When I apply single character names, as demonstrated in your example, everything looks fine. But if I put in longer names the circle sizes grow with the length of the name and everything starts looking odd. – Display name Sep 26 '19 at 20:31
  • 1
    I already edited my answer. And yes, use `fixedsize = true` and then specify width manually. You can read more details in `diagrammeR`'s documentation: http://rich-iannone.github.io/DiagrammeR/graphviz_and_mermaid.html – yusuzech Sep 26 '19 at 20:49