-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomNamesD3JS.R
More file actions
35 lines (29 loc) · 1.46 KB
/
RandomNamesD3JS.R
File metadata and controls
35 lines (29 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Libraries
library("networkD3")
library('randomNames')
library("igraph")
# Create data with the randomNames package :
NUMOFLINKS = 100
relations = data.frame(source = randomNames(1000,which.names='both'), target = "")
relations = relations[rep(seq_len(nrow(relations)), sample(1:10,nrow(relations), replace=T)),]
relations = relations[sample(nrow(relations),NUMOFLINKS),]
relations$target = sample(relations$source,nrow(relations), replace = T)
relations = relations[relations[,1]!=relations[,2], ]
vertices<-data.frame("name" = unique(unlist(relations))) # node names
g = graph.data.frame(relations, directed=F, vertices=vertices) # raw graph
vertices$group = edge.betweenness.community(g)$membership # betweeness centrality for each node for grouping
# create indices for each name to fit forceNetwork data format
relations$source.index = match(relations$source, vertices$name)-1
relations$target.index = match(relations$target, vertices$name)-1
# sophisticated network graph
d3 = forceNetwork(Links = relations, Nodes = vertices,
Source = "source.index", Target = "target.index",
NodeID = "name",
Group = "group", # color nodes by betweeness calculated earlier
charge = -70, # node repulsion
linkDistance = 25,
zoom = T)
# If you want to show the graph
show(d3)
#If you want to save the graph as html file
saveNetwork(d3,file = '#87_Interactive_network.html',selfcontained = F)