在 DiagrammeR 中适合节点中的标签

Fit labels in nodes in DiagrammeR

我需要在节点上绘制具有 2 向边和标签的有向网络。节点名称长度有时很短,有时很长。数据是动态生成的,所以每次都是随机的。

这是我的数据示例:

library(DiagrammeR)

# create data:
links=data.frame(
    source=c("AAAAAAAAAAAAAAAAAAAAAAAA","AAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAA","J", "BBBBBBBBB",
             "BBBBBBBBBBBBBBBBBBBBB", "CCCCCCCCCCC", "CCCCCCCCCCC", "D","IIIIIIII"),
    target=c("BBBBBBBBBBBBBBBBBBBBB","BBBBBBBBBBBBBBBBBBBBB", "CCCCCCCCCCC", "D", 
             "J","AAAAAAAAAAAAAAAAAAAAAAAA","E", "F", "G", "H", "IIIIIIII","IIIIIIII")
)


labels <- unique(c(links$source,links$target))

nodes <- data.frame (id = labels, label = labels )

i_graph_1 <-
    DiagrammeR::create_graph() %>% 
    DiagrammeR::add_nodes_from_table(
        table = nodes,
        label_col = label) %>% 
    DiagrammeR::add_edges_from_table(
        table = links,
        from_col = source,
        to_col = target,
        from_to_map = id_external)%>% 
    select_nodes_by_id(nodes = 1:length(labels)) %>% 
    set_node_attrs_ws(
        node_attr = shape,
        value = "rectangle") %>% 
    clear_selection()

gr <- i_graph_1 %>% DiagrammeR::render_graph()

writeLines(
    export_svg(gr), "c:\temp\n1.svg"
    )



我的结果图片:

  1. 是否可以使 A<->B、A<->J 链接双向而不是 2 个单向链接?
  2. 是否可以在节点中放置标签而不重叠节点边界?

更新:

如果我使用 fixedsize = False,结果仍然不好。节点重叠。

i_graph_1 <-
    DiagrammeR::create_graph() %>% 
    DiagrammeR::add_nodes_from_table(
        table = nodes,
        label_col = label) %>% 
    DiagrammeR::add_edges_from_table(
        table = links,
        from_col = source,
        to_col = target,
        from_to_map = id_external)%>% 
    select_nodes_by_id(nodes = 1:length(labels)) %>% 
    set_node_attrs(node_attr = "fixedsize",values = FALSE) %>% 
    set_node_attrs_ws(
        node_attr = shape,
        value = "rectangle") %>% 
    clear_selection()

不确定 1。但是 2 是可能的。你只需要使用 edge_aes(dir="both") 属性。 https://rdrr.io/cran/DiagrammeR/man/edge_aes.html

library(DiagrammeR)
library(dplyr)

graph <-
  create_graph() %>%
  add_path(
    n = 3,
    type = "path",
    edge_aes = edge_aes(
      style = "dot",
      color = c("red", "blue"),
      dir="both"))


graph %>%
  get_edge_df()

my_graph <- graph %>% DiagrammeR::render_graph()
my_graph

关键是使用 graphviz 属性 布局 = dot, 集中注意力 = true.

并且node_attr = "fixedsize",values = FALSE.

library(DiagrammeR)

# create data:
links=data.frame(
    source=c("AAAAAAAAAAAAAAAAAAAAAAAA","AAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAA","J", "BBBBBBBBB",
             "BBBBBBBBBBBBBBBBBBBBB", "CCCCCCCCCCC", "CCCCCCCCCCC", "D","IIIIIIII"),
    target=c("BBBBBBBBBBBBBBBBBBBBB","BBBBBBBBBBBBBBBBBBBBB", "CCCCCCCCCCC", "D", 
             "J","AAAAAAAAAAAAAAAAAAAAAAAA","E", "F", "G", "H", "IIIIIIII","IIIIIIII")
)

# Turn it into igraph object


labels <- unique(c(links$source,links$target))

nodes <- data.frame (id = labels, label = labels )

i_graph_1 <-
    DiagrammeR::create_graph() %>% 
    add_global_graph_attrs("layout", "dot", "graph") %>%
    add_global_graph_attrs("concentrate", "true", "graph") %>%
    DiagrammeR::add_nodes_from_table(
        table = nodes,
        label_col = label) %>% 
    DiagrammeR::add_edges_from_table(
        table = links,
        from_col = source,
        to_col = target,
        from_to_map = id_external)%>% 
    select_nodes_by_id(nodes = 1:length(labels)) %>% 
    set_node_attrs(node_attr = "fixedsize",values = FALSE) %>% 
    set_node_attrs_ws(
        node_attr = shape,
        value = "rectangle") %>% 
    clear_selection()


gr <- i_graph_1 %>% DiagrammeR::render_graph( )
gr

结果如下: