0

我使用 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)
4

1 回答 1

0

这是我找到的最好的解决方案。简单地忽略一般属性并依赖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() 代码。它基本上基于原始的edges_df、nodes_df、directed 和graph_attrs 重新构建图形,但它不包括node_attrs 或edge_attrs,因此它们恢复为默认值。简单的解决方案是构建一个考虑到这一点的新函数,但我忽略了是否会与 create_graph 发生冲突。

于 2015-10-31T13:30:55.623 回答