使用 visNetwork(或替代方案)明确放置节点

Place nodes explicitly with visNetwork (or an Alternative)

如何在 visNetwork 图表上明确放置 nodes

或者:如何使用 visNetwork 或替代方法在 R 中重新创建该图形?

背景:最终目标是表示Causal Loop Diagrams coming from Vensim个文件。明确放置节点只是第一步(关键),因为在因果循环图中,节点的视觉映射是信息的一部分(与一般图论不同)。因此,如果有人对大局有建议。 'Bringing Causal Loop Diagram Modeling to R',我会很高兴的

我尝试了什么:

library("visNetwork")

nodes <- data.frame(id = 1:3, label = c("one", "two", "three"))
edges <- data.frame(from = c(1,1,2), to = c(2,3,1))

visNetwork(nodes, edges, width = "100%", title = nodes$labels, stringsAsFactors = FALSE) %>% visEdges(arrows = "to")

它绘制了类似的东西(确切的布局会改变,因为随机种子):

通过 here 的问答,我尝试通过设置 xy 值手动放置节点。

library("visNetwork")

nodes <- data.frame(id = 1:3, label = c("one", "two", "three"), x = c(0,1,2), y = c(0,1,2))
edges <- data.frame(from = c(1,1,2), to = c(2,3,1))

visNetwork(nodes, edges, width = "100%", title = nodes$labels, stringsAsFactors = FALSE) %>% visEdges(arrows = "to")

哪个地块:

..我真的不明白xy和屏幕上的位置有什么对应..

此外,我在 docs 中找不到 visLayout 的任何内容。

不知何故,xy 参数不起作用。这里有一个解决方案:

library("visNetwork")

nodes <- data.frame(id = 1:3, label = c("one", "two", "three"))
edges <- data.frame(from = c(1,1,2), to = c(2,3,1))

coords <- as.matrix(data.frame(x = c(0,1,2),
                               y = c(0,1,2),
                               stringsAsFactors = FALSE))

visNetwork(nodes, edges, width = "100%", title = nodes$labels) %>%
    visNodes() %>%
    visOptions(highlightNearest = TRUE) %>%
    visInteraction(navigationButtons = TRUE,
                   dragNodes = TRUE, dragView = TRUE,
                   zoomView = FALSE) %>%
    visEdges(arrows = 'to') %>%
    visIgraphLayout(layout = "layout.norm", layoutMatrix = coords)

有关历史,另请参阅 。 也许这些链接可能对您想要实现的目标有所帮助:causaleffect and plot.CLD

使用 ggraph 而不是 visNetwork 可以简化事情。

library(ggraph)
library(igraph)

g <- make_graph(edges = c(1,2,2,1,1,3))
V(g)$name <- c('one', 'two', 'three')

ggraph(g, layout = 'manual', node.positions = data.frame(x = c(1,1,2), y = c(2,1,2.1))) + 
  geom_edge_arc(aes(start_cap = label_rect(node1.name),
                    end_cap = label_rect(node2.name)),
                 angle_calc = 'along',
                 label_dodge = unit(2.5, 'mm'),
                 arrow = arrow(length = unit(4, 'mm'))) + 
  geom_node_text(aes(label = name, x = x, y = y))

此地块

这是(除了网格线和颜色)我正在寻找的东西。