1

如何明确放置nodesvisNetwork图表上?

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

在此处输入图像描述

背景:最终目标是表示来自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")

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

在此处输入图像描述

通过此处的问答,我尝试通过设置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")

其中情节:

在此处输入图像描述

..我真的不明白x,y和屏幕上的放置之间的对应关系是什么..

此外,我在文档中找不到任何内容visLayout

4

2 回答 2

2

不知何故,xand yargs 不起作用。这里有一个解决方案:

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)

有关历史,请参见此处。也许这些链接可能对您想要实现的目标有所帮助:causaleffectplot.CLD

于 2018-10-03T05:11:52.963 回答
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))

这情节在此处输入图像描述

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

于 2018-10-08T14:36:26.570 回答