下面的快照可视化是使用"visNetwork“包创建的。这里我的要求是,我必须硬编码边,而且在使用visHierarchicalLayout()之后,我无法按顺序看到它们,请用动态方法帮助我,这样无论有多少个数字,我都可以在没有硬代码的情况下得到连续的数字。谢谢,请帮忙。
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()

发布于 2018-01-05 08:27:21
使用level属性完成任务,它根据给定的顺序对网络进行对齐。
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()发布于 2018-01-05 06:29:55
如果我正确理解了您的问题,您希望基于edges数据框架中的id创建nodes数据框架。这里有一个选择。
# 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")
edges
# from to
# 1 1 2
# 2 2 3
# 3 3 4
# 4 4 5
# 5 5 6
# 6 6 7 https://stackoverflow.com/questions/48108231
复制相似问题