使用visNetwork(或替代方法)显式放置节点

时间:2018-10-02 14:51:02

标签: r igraph graph-theory visnetwork

如何在nodes图上显式放置visNetwork

或者:如何使用visNetwork或其他方式在R中重新创建该图形?

enter image description here

背景:最终目标是代表Causal Loop Diagrams个来自Vensim文件的文件。明确地放置节点只是第一步(关键),因为在因果循环图中,节点的可视化映射是信息的一部分(与一般图论不同)。因此,如果有人对大局有所建议。 “将因果回路图建模引入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")

它绘制出类似的内容(由于随机种子,确切的布局会改变):

enter image description here

根据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")

绘制:

enter image description here

..我真的不明白xy和屏幕放置位置之间的对应关系。

我也无法在visLayout的{​​{3}}中找到任何内容。

2 个答案:

答案 0 :(得分:1)

事实证明,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)

有关历史记录,另请参见here。 也许这些链接可能对您想要达到的目标有用:causaleffectplot.CLD

答案 1 :(得分:1)

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

此图enter image description here

(我正在搜索的是(除网格线和颜色之外)。