简体   繁体   中英

How to keep edge colours after delete_edge() in DiagrammeR

I've created a graph with DiagrammeR by first setting up a basic graph and then adding nodes and edges. I've set up the edge color attribute in the edge_attrs() inside create_graph().

If I add a new edge without the color attribute it uses, as expected, the pre-defined colour. However, if I delete one of the edges with delete_edge() the general edge attribute color disappears for all edges. Since the graph$edges_df doesn't include any color information the graph defaults to black.

Is there a way to add the colour of the edge to graph$edges_df when using add_node()?

The only way I've thought would work is to add the node without the edge and then add the edge individually with add_edge().

Here a reproducible example:

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)

This is the best solution I've found. Simply ignoring the general attributes and relying on 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)

Edit: I've now looked at the code from rich-iannone for delete_edge(). It basically re-builds the graph based on the original edges_df, nodes_df, directed and graph_attrs but it does not include the node_attrs or the edge_attrs, therefore they go back to the default. The easy solution is to build a new function that takes that into consideration, but I ignore if there would be conflicts with create_graph.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM