admin管理员组文章数量:1386824
I'm trying to generate a network using the randomNames package, then create a network graph using visNetwork. Unfortunately, none of the connections show up on the network graph when I do what seems fairly straightforward.
Here is what I have tried.
library(randomNames)
library(visNetwork)
#Generate network using randomNames package
Amount = sample(50:600, 15, replace = T)
From = randomNames(15, name.order = "first.last",name.sep = " ")
To = randomNames(15, name.order = "first.last",name.sep = " ")
From = c(From, From[10:15], From[1:5], From[10:15], From[5:10])
To = c(To, To[1:5], To[10:15], To[1:5], To[5:10], To[1:5])
edges <- data.frame(cbind(From, To, Amount))
nodes <- as.data.frame(unique(as.vector(rbind(edges1$From, edges1$To))))
#Graph network using visNetwork
visNetwork(nodes, edges, width = "100%")
Unfortunately, the resulting graph doesn't have any links or connections. The nodes and edges are characters.
I'm trying to generate a network using the randomNames package, then create a network graph using visNetwork. Unfortunately, none of the connections show up on the network graph when I do what seems fairly straightforward.
Here is what I have tried.
library(randomNames)
library(visNetwork)
#Generate network using randomNames package
Amount = sample(50:600, 15, replace = T)
From = randomNames(15, name.order = "first.last",name.sep = " ")
To = randomNames(15, name.order = "first.last",name.sep = " ")
From = c(From, From[10:15], From[1:5], From[10:15], From[5:10])
To = c(To, To[1:5], To[10:15], To[1:5], To[5:10], To[1:5])
edges <- data.frame(cbind(From, To, Amount))
nodes <- as.data.frame(unique(as.vector(rbind(edges1$From, edges1$To))))
#Graph network using visNetwork
visNetwork(nodes, edges, width = "100%")
Unfortunately, the resulting graph doesn't have any links or connections. The nodes and edges are characters.
Share Improve this question edited Mar 17 at 21:02 MrFlick 207k19 gold badges295 silver badges318 bronze badges Recognized by R Language Collective asked Mar 17 at 19:15 Mary WadeMary Wade 211 bronze badge2 Answers
Reset to default 1Values in R are case sensitive. So in the docs for ?visNetwork
when they give you the expected column names "to" and "from" for the egdes, you want to make sure those match and are not "To" and "From". Here's an example that should work.
library(randomNames)
library(visNetwork)
Amount = sample(50:600, 35, replace = T)
From = randomNames(15, name.order = "first.last",name.sep = " ")
To = randomNames(15, name.order = "first.last",name.sep = " ")
From = c(From, From[11:15], From[1:5], From[11:15], From[6:10])
To = c(To, To[1:5], To[11:15], To[1:5], To[1:5])
edges <- data.frame(from=From, to=To, amount=Amount)
nodes <- data.frame(id=unique(c(edges$from, edges$to)))
visNetwork(nodes, edges)
(I also fixed up the data so each vector has the same number of values)
The output for me looked like this:
Your main issue is that visNetwork::visNetwork()
needs an id
column in order to generate the edges. Also, your From, To, and Amount vectors are all of different lengths, and R is case-sensitive. Given this is not 'real data', I have made some modifications to your data for simplicity, and to illustrate a potential workflow:
library(randomNames)
library(visNetwork)
# Generate randomNames vectors
set.seed(42)
From <- randomNames(15, name.order = "first.last", name.sep = " ")
To <- randomNames(15, name.order = "first.last", name.sep = " ")
# Expand to/from, ensure both vectors the same length (padded here with NAs)
From <- c(From, NA, From[10:15], NA, From[1:5], NA, From[10:15], NA, From[5:10])
To <- c(To, To[1:5], To[10:15], To[1:5], To[5:10], To[1:5])
# Ensure Amount matches the length of From and To
Amount <- sample(50:600, length(From), replace = TRUE)
# Create edges df
edges <- data.frame(from = From, to = To, value = Amount)
# Create nodes df with a id column (and label, for illustrative purposes)
nodes <- data.frame(id = unique(c(edges$from, edges$to)),
label = unique(c(edges$from, edges$to)))
# Graph network
visNetwork(nodes, edges, width = "100%") %>%
visEdges(arrows = "to")
The result. Note that the layout of your nodes may differ from this example. Also, the node labels may not appear in the RStudio viewer pane, but they will if you click the "Show in new window" button.
本文标签: rVisNetworkNo EdgesStack Overflow
版权声明:本文标题:r - VisNetwork - No Edges - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744539268a2611508.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论