D3 is a (JavaScript) library for manipulating data. It can be useful to create dynamic networks, for example, or reactive non-linear structures (which may be an interesting way to present feature geometry or prosodic structures in phonology if you like to come up with fancy visuals). In R, you can use the networkD3 package to generate this type of network. In the example below, I show a force-directed network that represents the relationship between Romance Languages.


An example


Data

To generate the network above, I’m using two data frames: nodes and links. In the nodes data frame below, name is the label of each node; group will be used to define the different colours in the network; and size will be used to adjust the size of each node. The links data frame simply establishes the sources and targets of each link in the network. The variable value will be used later to define the width of each link.

head(nodes); nrow(nodes)
##                  name group  size
## 1               LATIN     1 250.0
## 2     Classical Latin     1 100.0
## 3        Vulgar Latin     1  80.0
## 4 Continental Romance     2  25.0
## 5  Sardinian Language     2   0.1
## 6     Eastern Romance     2  15.0
## [1] 24
head(links); nrow(links)
##   source target value
## 1      0      1     5
## 2      1      2     4
## 3      2      3     3
## 4      2      4     1
## 5      3      5     2
## 6      3      5     1
## [1] 24

Code

forceNetwork(Links = links, 
             Nodes = nodes,
             Source = "source", 
             Value = "value",
             Target = "target",
             NodeID = "name",
             Nodesize = "size",
             Group = "group", 
             opacity = 1,
             linkColour = "black",
             linkDistance = JS("function(d) { return d.value * 20; }"),
             colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20);"),
             charge = -150,
             fontSize = 15,
             legend = F,
             linkWidth = JS("function(d) { return d.value*0.7; }"),
             radiusCalculation = JS("Math.sqrt(d.nodesize)+10"),
             opacityNoHover = 0.5,
             zoom = FALSE)


Copyright © 2022 Guilherme Duarte Garcia