Creating flowchart with DiagrammeR nodes and edges instead of graphviz

I would like to create a flowchart using the DiagrammeR nodes and edges functionality with R instead of using the graphviz wrapper function.

However, I can't figure out how to make the edges straight to make it nice.

This is the graphviz solution that looks like what I want:

# Packages needed for the test
library(DiagrammeR)

# grViz solution
grViz("digraph flowchart {
# node definitions with substituted label text
node [fontname = Helvetica, shape = rectangle]        
tab1 [label = '@@1', group=gr1]
tab2 [label = '@@2', group=gr2]
tab3 [label = '@@3', group=gr3]
invis1 [style = invis, shape=point, width = 0, group=gr1]
invis1a [style = invis, shape=point, width = 0, group=gr2]
invis1b [style = invis, shape=point, width = 0, group=gr3]

# edge definitions with the node IDs
edge [arrowhead='none']
tab1 -> invis1;
invis1a -> invis1 -> invis1b; {rank=same invis1a invis1 invis1b}
edge [arrowhead='normal']
invis1a -> tab2;
invis1b -> tab3; {rank=same tab2 tab3}}

[1]: 'A'
[2]: 'B'
[3]: 'C'
")

This is my attempt to recreate the same graph with the nodes and edges solution:

# Packages needed for the test
library(DiagrammeR)
library(magrittr)

# Node and edge df solution
create_graph() %>% 
  add_node( # id 1
    label = "A",
    type = "group_1",
    node_aes = node_aes(
      style = "filled",
      shape = "rectangle",
      fixedsize = FALSE)
    ) %>% 
  add_node( # id 2
    type = "group_1",
    node_aes = node_aes(
      style = "invisible",
      height = 0,
      width = 0)
    ) %>% 
  add_edge(
    from = 1,
    to = 2,
    edge_aes = edge_aes(
      arrowhead = "none")
    ) %>% 
  add_node( # id 3
    type = "group_2",
    node_aes = node_aes(
      style = "invisible",
      height = 0,
      width = 0)
    ) %>%
  add_edge(
    from = 2,
    to = 3,
    edge_aes = edge_aes(
      arrowhead = "none")
    ) %>% 
  add_node( # id 4
    type = "group_3",
    node_aes = node_aes(
      style = "invisible",
      height = 0,
      width = 0)
    ) %>%
  add_edge(
    from = 2,
    to = 4,
    edge_aes = edge_aes(
      arrowhead = "none")
    ) %>% 
  add_node( # id 5
    label = "B",
    type = "group_2",
    node_aes = node_aes(
      style = "filled",
      shape = "rectangle",
      fixedsize = FALSE)
    ) %>% 
  add_edge(
    from = 3,
    to = 5,
    edge_aes = edge_aes(
      arrowhead = "normal")
    ) %>% 
  add_node( # id 6
    label = "C",
    type = "group_3",
    node_aes = node_aes(
      style = "filled",
      shape = "rectangle",
      fixedsize = FALSE)
    ) %>%
  add_edge(
    from = 4,
    to = 6,
    edge_aes = edge_aes(
      arrowhead = "normal")
    ) %>% 
  render_graph()
%>% render_graph(layout = "tree")

will get you closer

1 Like

Thanks! It definitely looks better.

I used the "group" argument to make the edges vertical and the "rank" argument to make them horizontal in the graphviz code. But seemingly group does not do that here and I do not find a substitute code for the rank argument.

Do you have any other tips?

Take a look at the documentation in some detail: for example recode_edge_attrs has a penwidth option

I have seen that attribute in the documentation but it is described as "the thickness of the stroke for the shape".

I've tried to create a more sophisticated flowchart (it has more nodes) with the tree layout method and without the invisible nodes. The only problem is that the nodes that I want to be on the same level ("rank" in graphviz) are not on the same level here.

Take a look at the ggnetwork package. Great flexibility by converting graph objects into data frames, allowing finer grained control

Thanks I will! My main goal is to include this flowchart in a Shiny app and make the nodes behave as an actionbutton. I know that it is solvable with the DiagrammeR package functional approach that is why I am trying to replicate my DOT graph with this method.

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.