簡體   English   中英

如何在DiagrammeR中的delete_edge()之后保留邊緣顏色

[英]How to keep edge colours after delete_edge() in DiagrammeR

我通過首先設置基本圖,然后添加節點和邊,使用DiagrammeR創建了一個圖。 我已經在create_graph()內部的edge_attrs()中設置了邊緣顏色屬性。

如果我添加沒有顏色屬性的新邊緣,則它按預期使用了預定義的顏色。 但是,如果我使用delete_edge()刪除邊緣之一,則所有邊緣的常規邊緣屬性顏色都會消失。 由於graph $ edges_df不包含任何顏色信息,因此該圖默認為黑色。

使用add_node()時,是否可以將邊緣的顏色添加到graph $ edges_df?

我認為可行的唯一方法是添加沒有邊緣的節點,然后使用add_edge()分別添加邊緣。

這是一個可重現的示例:

library(DiagrammeR)
library(magrittr)

nodes <-
  create_nodes(
    nodes = "a",
    label = "A",
    type = "lower",
    style = "filled",
    shape = "circle",
    color = "orange",
    x = 5,
    y = 4
  )

graph_1 <-
  create_graph(
    nodes_df = nodes,
    graph_attrs = c(
      "layout = neato"
      ),
   edge_attrs = c(
      "relationship = requires",
      "arrowhead = normal",
      "color = 'lightgrey'"
      )
  )

render_graph(graph_1)

graph_1 %>%
  add_node(
    node = "b",
    from = "a",
    label = "B",
    style = "filled",
    shape = "circle",
    color = "orange",
    x = 5,
    y = 3
  ) ->
  graph_2

render_graph(graph_2)

new_edges <- create_edges(
  from = "a",
  to = "a"
)

graph_2 %>%
  add_edges(
    edges_df = new_edges
  ) ->
  graph_3

render_graph(graph_3)

graph_3 %>%
  delete_edge(
    to = "a",
    from = "a"
  ) ->
  graph_4

render_graph(graph_4)

這是我找到的最佳解決方案。 只需忽略常規屬性並依靠edges_df。

library(DiagrammeR)
library(magrittr)

nodes <-
  create_nodes(
    nodes = "a",
    label = "A",
    type = "lower",
    style = "filled",
    shape = "circle",
    color = "orange",
    x = 5,
    y = 4
  )

graph_1 <-
  create_graph(
    nodes_df = nodes,
    graph_attrs = c(
      "layout = neato"
    )
  )

render_graph(graph_1)

graph_1 %>%
  add_node(
    node = "b",
    label = "B",
    style = "filled",
    shape = "circle",
    color = "orange",
    x = 5,
    y = 3
  ) %>%
  add_edges(
    edges_df = create_edges(
      from = "a",
      to = "b",
      color = "lightgrey"
    )
  ) ->
  graph_2

render_graph(graph_2)

graph_2 %>%
  add_edges(
    edges_df = create_edges(
      from = "a",
      to = "a",
      color = "lightgrey"
    )
  ) ->
  graph_3

render_graph(graph_3)

graph_3 %>%
  delete_edge(
    to = "b",
    from = "a"
  ) ->
  graph_4

render_graph(graph_4)

編輯:我現在看了來自rich-iannone的delete_edge()代碼。 它基本上基於原始的edge_df,nodes_df,directed和graph_attrs重新構建圖形,但不包括node_attrs或edge_attrs,因此它們返回默認值。 一種簡單的解決方案是構建一個考慮到這一點的新函數,但我忽略是否與create_graph發生沖突。

暫無
暫無

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

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