使用visNetwork(或替代方法)显式放置节点 [英] Place nodes explicitly with visNetwork (or an Alternative)
问题描述
如何在visNetwork
图上显式放置nodes
?
How can I explicitly place nodes
on a visNetwork
graph?
或者:如何使用visNetwork
或其他方式在R中重新创建该图形?
Or: How can I recreate that graphic in R using visNetwork
or an alternative?
背景:最终目标是代表原因回路图来自 Vensim 文件.明确地放置节点只是第一步(关键),因为在因果循环图中,节点的视觉映射是信息的一部分(与一般图论不同).因此,如果有人对大局有所建议. 将因果回路图建模带到R",我会很高兴.
Background: The ultimate goal is to represent Causal Loop Diagrams coming from Vensim files. Placing the nodes explicitly is just the first (crucial) step, because in Causal Loop Diagrams the visual mapping of nodes is part of the information (unlike in general graph theory). So if anybody has advice on the bigger picture aka. 'Bringing Causal Loop Diagram Modeling to R', I'll be more than happy.
我尝试过的事情:
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")
它绘制出类似的内容(由于随机种子,确切的布局会发生变化):
which plots something like (exact layout will change, because of random seed):
使用这里我试图通过设置x
和y
值来手动放置节点.
With the Q&A from here I tried to place nodes manually by setting x
and y
values.
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
和屏幕上放置位置之间的对应关系.
..and I really don't understand what's the correspondance between x
, y
and the placing on the screen..
我也没有在文档visLayout
的a>.
Also I could not find anything in the docs for visLayout
.
推荐答案
使用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))
此图
(我正在搜索的是(除网格线和颜色之外).
which is (apart from gridlines and colours) what I was searching for.
这篇关于使用visNetwork(或替代方法)显式放置节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!