簡體   English   中英

邊的權重屬性不適用於`DiagrammeR` R package

[英]weight attribute of edges not working on `DiagrammeR` R package

我正在嘗試使用 R 中的DiagrammeR package 向 GraphViz 圖中添加一些邊緣屬性。 特別是,我想添加屬性arrowheadweight ,但只有前者被添加到圖表中,而不是后者。 下面的例子不是真實的,而是一個超級簡化的版本,為了重現性和清晰性。

library(tidyverse)
library(DiagrammeR)
# create nodes
nd <- tibble(
  label =  c("A", "B", "C", "D"), 
  rank  =  c("1", "0", "2", "2")
)

node <- create_node_df(
  n     = nrow(nd),
  label = nd$label,
  id    =  c(1:nrow(nd)),
  # rank  = nd$rank
)

# create edges
ed <- tribble(
  ~from, ~to, ~arrowhead, ~weight,
  1,     2,   'normal',       "5",
  1,     3,   'normal',       "1",
  3,     4,   'normal',       "1",
  3,     2,   'normal',       "1"
)
edge <- create_edge_df(
  from      = ed$from,
  to        = ed$to,
  arrowhead = ed$arrowhead,
  weight    = ed$weight
)


# create graph
graph <-
  create_graph(
    nodes_df   = node,
    edges_df   = edge) %>%
  add_global_graph_attrs(
    attr       = "overlap",
    value      = "false",
    attr_type  = "graph") %>%
  add_global_graph_attrs(
    attr       = "layout",
    value      = "dot",
    attr_type  = "graph")

然而,當我生成dot代碼時, weight屬性不是邊緣的一部分,但arrowhead是。

graph %>%
  generate_dot() %>%
  cat()

#> digraph {
#> 
#> graph [layout = 'dot',
#>        outputorder = 'edgesfirst',
#>        bgcolor = 'white',
#>        overlap = 'false']
#> 
#> node [fontname = 'Helvetica',
#>       fontsize = '10',
#>       shape = 'circle',
#>       fixedsize = 'true',
#>       width = '0.5',
#>       style = 'filled',
#>       fillcolor = 'aliceblue',
#>       color = 'gray70',
#>       fontcolor = 'gray50']
#> 
#> edge [fontname = 'Helvetica',
#>      fontsize = '8',
#>      len = '1.5',
#>      color = 'gray80',
#>      arrowsize = '0.5']
#> 
#>   '1' [label = 'A'] 
#>   '2' [label = 'B'] 
#>   '3' [label = 'C'] 
#>   '4' [label = 'D'] 
#> '1'->'2' [arrowhead = 'normal'] 
#> '1'->'3' [arrowhead = 'normal'] 
#> '3'->'4' [arrowhead = 'normal'] 
#> '3'->'2' [arrowhead = 'normal'] 
#> }

我還嘗試使用set_edge_attrs()添加屬性,但它仍然無法正常工作。

graph <- graph %>% 
  set_edge_attrs(edge_attr = "weight", 
                 values = "5", 
                 from = 1, 
                 to = 2
                 )
# digraph {
#   
#   graph ...
#  {trimmed output}
#
#   '1'->'2' [arrowhead = 'normal'] 
#   '1'->'3' [arrowhead = 'normal'] 
#   '3'->'4' [arrowhead = 'normal'] 
#   '3'->'2' [arrowhead = 'normal'] 
# }

最后,我不得不手動修改dot代碼(見下文),但考慮到我必須從數據幀中的信息生成幾個圖表,這並不理想。

graph2 <- "digraph {

graph [layout = 'dot',
       outputorder = 'edgesfirst',
       bgcolor = 'white',
       overlap = 'false']

node [fontname = 'Helvetica',
      fontsize = '10',
      shape = 'circle',
      fixedsize = 'true',
      width = '0.5',
      style = 'filled',
      fillcolor = 'aliceblue',
      color = 'gray70',
      fontcolor = 'gray50']

edge [fontname = 'Helvetica',
     fontsize = '8',
     len = '1.5',
     color = 'gray80',
     arrowsize = '0.5']

  '1' [label = 'A'] 
  '2' [label = 'B'] 
  '3' [label = 'C'] 
  '4' [label = 'D'] 
'1'->'2' [arrowhead = 'normal', weight = 5] 
'1'->'3' [arrowhead = 'normal', weight = 0] 
'3'->'4' [arrowhead = 'normal', weight = 0] 
'3'->'2' [arrowhead = 'normal', weight = 0]  
}
"
grViz(graph2)

我非常感謝任何幫助。

謝謝。 最好的,

我不確定這是一個錯誤還是故意忽略了“重量”,但無論如何罪魁禍首是DiagrammeR中的一個助手 function ,它生成一個允許屬性的向量,而generate_dot()只允許來自該向量的屬性。

# DiagrammeR/R/utils.R
gv_edge_attributes <- function() {

  c("style", "penwidth", "color", "arrowsize",
    "arrowhead", "arrowtail",
    "fontname", "fontsize", "fontcolor",
    "len", "tooltip", "URL",
    "label", "labelfontname", "labelfontsize",
    "labelfontcolor", "labeltooltip", "labelURL",
    "edgetooltip", "edgeURL",
    "headtooltip", "headURL",
    "headclip", "headlabel", "headport",
    "tailtooltip", "tailURL",
    "tailclip",  "taillabel", "tailport",
    "dir", "decorate")
}

如果您將“權重”添加到該列表 - 或修改generate_dot以允許所有屬性無需檢查 - 您的示例就可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM