Диаграмма R горизонтальный график - PullRequest
0 голосов
/ 09 января 2019

Я хотел бы построить горизонтальный график, используя пакет DiagrammeR в R. Однако я нашел только построить вертикальный график. Есть идеи, как перевернуть его на 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")

Результат:

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")

Ответы [ 2 ]

0 голосов
/ 14 июня 2019

Единственный способ, который я нашел, заключается в манипулировании форматом dot перед передачей его в grViz. Здесь я заменяю параметры макета по умолчанию на макет dot и переворачиваю его, добавляя rankdir = LR.

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

Так в вашем случае

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

Обратите внимание, что я добавил еще один set_node_attrs, чтобы явно установить черный цвет шрифта. В противном случае цвет шрифта по умолчанию светло-серый.

0 голосов
/ 09 января 2019

Я использовал только мой предыдущий шаблон для иллюстрации альтернативы:

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']
      }")

Выход: enter image description here

...