【问题标题】:Place nodes explicitly with visNetwork (or an Alternative)使用 visNetwork(或替代方法)显式放置节点
【发布时间】:2018-10-08 22:36:26
【问题描述】:

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

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

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

【问题讨论】:

    标签: r igraph graph-theory visnetwork


    【解决方案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

    【讨论】:

    • 感谢您将我指向layoutMatrix 选项。这有助于明确放置节点。但是,当我运行您的代码时,所有箭头都绘制为直线,因此反之亦然箭头会遮盖住自己(与我的目标相反,请参见图片..)。你知道如何将箭头绘制为弯曲线吗?
    • @Christoph,我们可以生成类似的东西吗link here
    【解决方案2】:

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

    这个情节

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多