使用visNetwork动态更新R中的节点 [英] Using visNetwork to dynamically update nodes in R

查看:779
本文介绍了使用visNetwork动态更新R中的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下快照视觉是使用"visNetwork"包创建的.我在这里的要求是,我必须对边缘进行硬编码,并且在使用visHierarchicalLayout()之后,我无法按顺序看到它们,请以动态方式帮助我,无论有多少个数字,我都会得到连续的数字没有硬代码的订单.谢谢,请帮忙.

the below snapshot visual is created using the "visNetwork" package. My requirement here is that I have to hard code the edges and also after using visHierarchicalLayout(), I am not able to see them in order, Please help me with a dynamic approach such that no matter how many numbers, I get consecutive numbers in order without hard code. Thanks and please help.

library(visNetwork)
nodes <- data.frame(id = 1:7, label = 1:7)
edges <- data.frame(from = c(1,2,3,4,5,6),
                  to = c(2,3,4,5,6,7))
visNetwork(nodes, edges, width = "100%") %>% 
visEdges(arrows = "to") %>% 
visHierarchicalLayout()

推荐答案

使用级别属性可以完成此任务,它会根据给定的顺序对齐网络.

Using level attribute does the job, it aligns the network based on the order given.

library(visNetwork)
nodes <- data.frame(id = 1:7, label = 1:7, level = 1:7)
# Extract the id
num <- nodes$id
# Repeat the numbers
num2 <- rep(num, each = 2)
# Remove the first and last numbers
num3 <- num2[c(-1, -length(num2))]
#Create a data frame
edges <- as.data.frame(matrix(num3, ncol = 2, byrow = TRUE))
names(edges) <- c("from", "to")
visNetwork(nodes, edges, width = "100%") %>% 
visEdges(arrows = "to") %>% 
visHierarchicalLayout()

这篇关于使用visNetwork动态更新R中的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆