Skip to main content

R, D3js and SNA Course

Exploring centrality with R, D3 and four toy networks.
R
networks
D3
Author

Joshua Kunst

Published

April 5, 2013

Update 2015-11-09: This is a migration from an old post.

I took the SNA course by Lada Adamic on Coursera. It’s a super interesting course. In fact, I was using networks only as a visualization tool, and that makes me a little bit embarrassed because there is more, a lot more. You can detect communities, find more central nodes and get a lot of other information. So there are a lot of reasons to look at the course.

On the other hand, I like the D3 JavaScript library. Recently I was learning JavaScript, so I decided to make a very little app to keep learning this library and show different measures of centrality for each node in a set of four toy networks, using size, color or a label.

Now, the R code to make the data:

degree_sna <- function(net, norm = TRUE, ...){
  degree(net, ...)/2/(if (norm) ncol(net) - 1 else 1)
}

betweenness_sna <- function(net, norm = FALSE, ...){
  n <- ncol(net)
  betweenness(net, ...)/2/(if (norm) (n - 1)*(n - 2)/2 else 1)
}

# Mean absolute pairwise differences, normalized by twice the mean degree.
# This reproduces the Gini values previously calculated with reldist.
gini <- function(x) {
  sum(abs(outer(x, x, "-"))) / (2 * length(x) * sum(x))
}
net.butterfly <- matrix(c(0,1,1,0,0,0,0,
                          1,0,1,0,0,0,0,
                          1,1,0,1,0,0,0,
                          0,0,1,0,1,0,0,
                          0,0,0,1,0,1,1,
                          0,0,0,0,1,0,1,
                          0,0,0,0,1,1,0),
                        byrow = TRUE, nrow = 7)

net.star <- matrix(c(0,1,1,1,1,1,
                     1,0,0,0,0,0,
                     1,0,0,0,0,0,
                     1,0,0,0,0,0,
                     1,0,0,0,0,0,
                     1,0,0,0,0,0),
                   byrow = TRUE, nrow = 6)

net.line <- matrix(c(0,1,0,0,0,
                     1,0,1,0,0,
                     0,1,0,1,0,
                     0,0,1,0,1,
                     0,0,0,1,0),
                   byrow = TRUE, nrow = 5)

net.circular <- matrix(c(0,1,0,0,1,
                         1,0,1,0,0,
                         0,1,0,1,0,
                         0,0,1,0,1,
                         1,0,0,1,0),
                       byrow = TRUE, nrow = 5)

nets <- list(net.butterfly, net.star, net.line, net.circular)
net.all <- as.matrix(bdiag(net.butterfly, net.star, net.line, net.circular))
gplot(net.butterfly, displaylabels = TRUE, usearrows = FALSE)

gplot(net.star, displaylabels = TRUE, usearrows = FALSE)

gplot(net.line, displaylabels = TRUE, usearrows = FALSE)

gplot(net.circular, displaylabels = TRUE, usearrows = FALSE)

gplot(net.all, usearrows = FALSE,
      label = unlist(llply(nets, degree_sna, norm = FALSE)))

# Normalized and raw degree for each network.
llply(nets, degree_sna)
[[1]]
[1] 0.333 0.333 0.500 0.333 0.500 0.333 0.333

[[2]]
[1] 1.0 0.2 0.2 0.2 0.2 0.2

[[3]]
[1] 0.25 0.50 0.50 0.50 0.25

[[4]]
[1] 0.5 0.5 0.5 0.5 0.5
llply(nets, degree_sna, norm = FALSE)
[[1]]
[1] 2 2 3 2 3 2 2

[[2]]
[1] 5 1 1 1 1 1

[[3]]
[1] 1 2 2 2 1

[[4]]
[1] 2 2 2 2 2
# Differences in degree between nodes within a network.
laply(nets, function(net) gini(degree_sna(net)))
[1] 0.0893 0.3333 0.1500 0.0000
laply(nets, function(net) sd(degree_sna(net)))
[1] 0.0813 0.3266 0.1369 0.0000
# Network-level degree centralization.
laply(nets, centralization, degree)
[1] 0.167 1.000 0.167 0.000
llply(nets, betweenness_sna)
[[1]]
[1] 0 0 8 9 8 0 0

[[2]]
[1] 10  0  0  0  0  0

[[3]]
[1] 0 3 4 3 0

[[4]]
[1] 1 1 1 1 1
llply(nets, betweenness_sna, norm = TRUE)
[[1]]
[1] 0.000 0.000 0.533 0.600 0.533 0.000 0.000

[[2]]
[1] 1 0 0 0 0 0

[[3]]
[1] 0.000 0.500 0.667 0.500 0.000

[[4]]
[1] 0.167 0.167 0.167 0.167 0.167
llply(nets, closeness)
[[1]]
[1] 0.400 0.400 0.545 0.600 0.545 0.400 0.400

[[2]]
[1] 1.000 0.556 0.556 0.556 0.556 0.556

[[3]]
[1] 0.400 0.571 0.667 0.571 0.400

[[4]]
[1] 0.667 0.667 0.667 0.667 0.667
llply(nets, evcent)
[[1]]
[1] 0.335 0.335 0.450 0.384 0.450 0.335 0.335

[[2]]
[1] 0.408 0.408 0.408 0.408 0.408 0.408

[[3]]
[1] 0.309 0.463 0.617 0.463 0.309

[[4]]
[1] 0.447 0.447 0.447 0.447 0.447
names <- paste(rep(1:length(nets), laply(nets, ncol)),
               unlist(llply(nets, function(x) 1:ncol(x))), sep = "_")

colnames(net.all) <- names
rownames(net.all) <- names

links <- ldply(names, function(name){
  # name <- sample(names, size = 1)
  # name <- names[1]
  data.frame(source = which(names == name) - 1, 
             target = which(net.all[name,] == 1) - 1)
})

nodes <- data.frame(name = names)
nodes$degree_norm <- unlist(llply(nets, degree_sna))
nodes$degree <- unlist(llply(nets, degree_sna, norm = FALSE))
nodes$betweenness <- unlist(llply(nets, betweenness_sna))
nodes$betweenness_norm <- unlist(llply(nets, betweenness_sna, norm = TRUE))
nodes$closeness <- unlist(llply(nets, closeness))
nodes$eigen_vector_centrality <- unlist(llply(nets, evcent))

# D3 uses zero-based node indices, as in the source and target columns above.
write_json(list(nodes = nodes, links = links),
           "blog/posts/2013-04-05-r-d3js-and-sna-course/data/networks.json",
           dataframe = "rows", auto_unbox = TRUE, digits = 10)

You can fork the original app from here.

References

  • Adamic, L. Social Network Analysis — the original course link that motivated the example.
  • Bostock, M. D3 3.5.5 — the D3 release used by the preserved app, including the original force-layout API.
  • Kunst, J. R-D3-SNA-Course-Example — the original app and its code.