首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R diagrammeR水平图形

R diagrammeR水平图形
EN

Stack Overflow用户
提问于 2019-01-09 19:58:00
回答 2查看 917关注 0票数 2

我想用R中的DiagrammeR包绘制一个水平图形,但是我发现只绘制了一个垂直图形。你知道怎么把它翻90度吗?

代码语言:javascript
复制
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")

结果:

dput:

代码语言:javascript
复制
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")
EN

回答 2

Stack Overflow用户

发布于 2019-01-09 20:16:50

我只使用了我之前的一个模板来说明另一种选择:

代码语言:javascript
复制
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']
      }")

输出:

票数 1
EN

Stack Overflow用户

发布于 2019-06-14 10:45:47

我发现的唯一方法是在将dot格式传递给grViz之前对其进行操作。在这里,我将默认布局选项替换为dot布局,并通过添加rankdir = LR来反转它。

代码语言:javascript
复制
DiagrammeR::generate_dot(graph)  %>% 
    gsub(pattern = 'neato',replacement = 'dot',x= .) %>%
    gsub(pattern = "graph \\[",'graph \\[rankdir = LR,\n',x = .)%>%
    grViz

所以在你的情况下

代码语言:javascript
复制
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来显式地将字体颜色设置为黑色。否则,默认字体颜色为浅灰色。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54109621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档