2

我想使用 R 中的 DiagrammeR 包绘制水平图。但是我只发现绘制垂直图。知道如何将其翻转 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")

结果 :

在此处输入图像描述

输入:

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

2 回答 2

1

我只使用了我以前的模板来说明替代方案:

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

输出: 在此处输入图像描述

于 2019-01-09T12:16:49.577 回答
0

我发现的唯一方法是在将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()

在此处输入图像描述

请注意,我添加了另一个set_node_attrs以明确将字体颜色设置为黑色。否则默认字体颜色为浅灰色。

于 2019-06-14T02:45:46.760 回答