Reputation: 431
I have included a data frame which depicts a very small subset of the data I am using. My aim is to construct an interactive network due to the large number of nodes in the data.
library(networkD3)
screenName <- c("ZV8Lxypirmo2T8z", "Zwoodbutcher", "zX3GZYH7Ea5FKhx", "zXZK7fkzrpPpJdb",
"ZyaTheKing", "zzzcccbbbmmm")
mention <- c("GianCavallotto:", "IanPTrait:", "JahovasWitniss:", "Veachtravis:",
"visecs:", "Charles_HRH:")
n <- c(1L, 1L, 1L, 1L, 1L, 1L)
data <- data.frame(screenName,mention,n)
simpleNetwork(data)
The above code allows for the construction of an interactive undirected network graph. I came across the forceNetwork()
function under the networkD3
R library which might help here. But I do not really know how to convert the data.frame for its usage in this function. Thank You in advance!
Upvotes: 1
Views: 1180
Reputation: 8848
The functions simpleNetwork()
and forceNetwork()
are designed to work differently.
simpleNetwork()
takes one data frame as its primary input, and by default assumes that the first column is the 'source' of each link and the that the second column is the 'target' of each link. It does not require a data frame describing nodes because it assumes the only nodes are those that are linked to something in the link data frame and creates the node list internally by determining the unique values in the links data frame.
forceNetwork()
is more powerful and flexible, but it requires you to pass two data frames, one for links and one for nodes. You pass to the parameter Nodes
a data frame that contains a list of unique nodes. The parameters NodeID
and Group
are character values that define the name of the column in the nodes data frame that contains that information, e.g. NodeID = 'name'
and Group = 'type'
. The Group
column in the nodes data frame is used to define the color of the nodes, and is not really necessary, but forceNetwork()
requires it, so you can just make a column in the nodes data frame that has the same value for every row, e.g. 1
.
You can take the code you have above, and build the necessary data frames to use forceNetwork()
like this (for instance)...
library(networkD3)
screenName <- c("ZV8Lxypirmo2T8z", "Zwoodbutcher", "zX3GZYH7Ea5FKhx",
"zXZK7fkzrpPpJdb", "ZyaTheKing", "zzzcccbbbmmm")
mention <- c("GianCavallotto:", "IanPTrait:", "JahovasWitniss:", "Veachtravis:",
"visecs:", "Charles_HRH:")
n <- c(1L, 1L, 1L, 1L, 1L, 1L)
nodeFactors <- factor(sort(unique(c(screenName, mention))))
nodes <- data.frame(name = nodeFactors, group = 1)
screenName <- match(screenName, levels(nodeFactors)) - 1
mention <- match(mention, levels(nodeFactors)) - 1
links <- data.frame(screenName, mention, n)
forceNetwork(Links = links, Nodes = nodes, Source = 'screenName',
Target = 'mention', Value = 'n', NodeID = 'name', Group = 'group')
Upvotes: 1
Reputation: 1360
Let me explain with the example from the documentation.
You'd need two dataframes - links
and nodes
.
data(MisLinks)
glimpse(MisLinks)
#Observations: 254
#Variables: 3
#$ source <int> 1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 11, 11, 11, 11, 12, 13, 14, 15, 17, 18, 18, 19, 19, 1...
#$ target <int> 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 10, 3, 2, 0, 11, 11, 11, 11, 16, 16, 17, 16, 17, 18, ...
#$ value <int> 1, 8, 10, 6, 1, 1, 1, 1, 2, 1, 1, 3, 3, 5, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 3, 3, 3, 4...
length(unique(c(MisLinks$source,MisLinks$target)))
#[1] 77
data(MisNodes)
glimpse(MisNodes)
#Observations: 77
#Variables: 3
#$ name <fctr> Myriel, Napoleon, Mlle.Baptistine, Mme.Magloire, CountessdeLo, Geborand, Champterci...
#$ group <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 5, 4, ...
#$ size <int> 15, 20, 23, 30, 11, 9, 11, 30, 8, 29, 29, 9, 18, 12, 16, 22, 21, 11, 19, 10, 8, 13, ...
Each entry in the source
and target
of the links
dataframe is a node (integers 0,1,...,n). In this case there are 77 unique nodes. So you'd need a nodes
dataframe with 77 nodes describing the node attributes. The first entry in nodes
dataframe is node 0
, the next entry is node 1
and so on. The nodes
dataframe must be sorted according to this sequence. This is the only way to tie the nodes
dataframe to the links
dataframe.
# Create graph
forceNetwork(Links = MisLinks, Target = "target", Value = "value",
Nodes = MisNodes, Source = "source", NodeID = "name", Group = "group",
opacity = 0.4, zoom = TRUE)
Upvotes: 1