2

I would like to plot a horizontal graph using DiagrammeR package in R. However I only found to plot a vertical one. Any idea how to flip it 90° ?

library(DiagrammeR)
library(dplyr)

create_graph() %>% 
  add_nodes_from_table(table=n,label_col = task) %>% 
  add_edges_from_table(table=e,from_col = from,to_col = to,from_to_map = label) %>% 
  set_node_attrs(
    node_attr = "shape",
    values = "square"
  ) %>%
  render_graph(layout = "tree")

Result :

enter image description here

dput :

n <- structure(list(task = c("1", "2", "3", "4", "5", "6", "7", "8", 
"A", "B", "C")), .Names = "task", row.names = c(NA, -11L), class = "data.frame")
e <- structure(list(from = c("A", "1", "2", "4", "B", "3", "C", "5"
), to = c("1", "2", "4", "8", "3", "6", "5", "7")), .Names = c("from", 
"to"), row.names = c(NA, -8L), class = "data.frame")
s__
  • 9,270
  • 3
  • 27
  • 45
Nicolas Rosewick
  • 1,938
  • 4
  • 24
  • 42
  • Check for `circo` and `twoppi` if memory serves me right. – NelsonGon Jan 09 '19 at 12:09
  • If you use the "[canonical sinthax](http://rich-iannone.github.io/DiagrammeR/graphviz_and_mermaid.html)", you have the option `rankdir = LR`. With this `dplyr` form, I've found [this](https://gist.github.com/rich-iannone/b1c66c0734355bceea95) but it seems the functions don't exist. – s__ Jan 09 '19 at 12:45

2 Answers2

1

I've only used a previous template of mine for illustration of an alternative:

grViz("
      digraph Random{
      graph [layout = circo,
      overlap =T,
      outputorder = edgesfirst,
      bgcolor='white',
      splines=line]#controls l type setup
      edge[labelfontname='Arial',fontSize=13,color='red',fontcolor='navy']
      node [shape = box,style='filled',
      fillcolor='indianred4',width=2.5,
      fontSize=20,fontcolor='snow',
      fontname='Arial']#node shape
      a [label = 'A']
      b [label = 'B']
      c [label='D']
      a->b[color='red'] 
      b->c[color='dodgerblue']
      }")

The output: enter image description here

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
1

The only way I found relies on manipulating the dot format before passing it to grViz. Here I replace the default layout options with the dot layout and flip it around by adding rankdir = LR.

DiagrammeR::generate_dot(graph)  %>% 
    gsub(pattern = 'neato',replacement = 'dot',x= .) %>%
    gsub(pattern = "graph \\[",'graph \\[rankdir = LR,\n',x = .)%>%
    grViz

So in your case

n <- structure(list(task = c("1", "2", "3", "4", "5", "6", "7", "8", 
                             "A", "B", "C")), .Names = "task", row.names = c(NA, -11L), class = "data.frame")
e <- structure(list(from = c("A", "1", "2", "4", "B", "3", "C", "5"
), to = c("1", "2", "4", "8", "3", "6", "5", "7")), .Names = c("from", 
                                                               "to"), row.names = c(NA, -8L), class = "data.frame")

create_graph() %>% 
    add_nodes_from_table(table=n,label_col = task) %>% 
    add_edges_from_table(table=e,from_col = from,to_col = to,from_to_map = label) %>% 
    set_node_attrs(
        node_attr = "shape",
        values = "square",
    ) %>%
    set_node_attrs(
        node_attr = 'fontcolor',
        values = 'black'
    ) %>% 
    generate_dot() %>% 
    gsub(pattern = 'neato',replacement = 'dot',x= .) %>%
    gsub(pattern = "graph \\[",'graph \\[rankdir = LR,\n',x = .,perl = TRUE) %>% 
    grViz()

enter image description here

Note that I have added another set_node_attrs to set the font color to black explicitly. Otherwise the default font color is light gray.

OganM
  • 2,543
  • 16
  • 33