Interactive and Dynamic Social Network Visualization in R

Prof James P. Curley
6th April 2016

Section II - Interactive Graphs

visNetwork

visNetwork


Disclaimer: some things are very new in visNetwork - e.g. arrows don't appear in the legend, or if dropdown menus fails

Try reloading the browser if this happens ! (sorry)

visNetwork


library(visNetwork)
# minimal example
nodes <- data.frame(id = 1:5)
edges <- data.frame(from = c(2,2,4,1,1), to = c(1,3,2,3,5))
visNetwork(nodes, edges, width = "100%")

visNetwork


visNetwork


Classic rhesus monkey grooming data from Donald Sade (1960s)

head(nodes)
#     id group label font.size
#   oldF     F  oldF        24
#  1956F     F 1956F        24
#  1957M     M 1957M        24
#  1959M     M 1959M        24
#  1960M     M 1960M        24
#  1960F     F 1960F        24

head(edges)
#  from    to width
#  oldF 1960F     2
#  oldF 1957M     1
#  oldF 1961F     2
#  oldF 1956F     1
#  oldF 1959M     1
# 1960F   old     2

ledges
#     color     label  arrows
# darkblue     grooms      to
#      red    groomed    from  

visNetwork


<br>
visNetwork(nodes, edges,width="100%") %>% visEdges(arrows = 'from')  %>%

  visGroups(groupname = "M", color = "darkblue", shape = "square", shadow = list(enabled = T)) %>% 

  visGroups(groupname = "F", color = "red", shape = "triangle") %>%

  visPhysics(solver = "forceAtlas2Based", forceAtlas2Based = list(gravitationalConstant = -60)) %>%

  visLegend(width=0.2,addEdges = ledges)


visNetwork highlight nearest


#create a random graph to work from
library(igraph)
set.seed(576)

g <- sample_forestfire(125, fw.prob=0.05, bw.factor = 0.2, ambs = 2,directed = F)

nodes <- data.frame(id = as.character(V(g)))
nodes$font.size<-20

edges <- data.frame(get.edgelist(g))
colnames(edges)<-c("from","to")

# Plot with defaut layout
visNetwork(nodes, edges, height = "600px") %>%
  visIgraphLayout() %>%
  visNodes(size = 25) %>%
  visOptions(highlightNearest = T) %>%  
  visInteraction(keyboard = TRUE)


Highlights nearest
nodes and edges

visNetwork select groups


# Add some community membership (a quick and dirty way)
nodes$group <- cluster_fast_greedy(g)$membership
nodes$value = betweenness(g,directed=F, normalized = T)
nodes$font.size <-28

visNetwork(nodes, edges, height = "600px") %>%
  visIgraphLayout() %>%
  visNodes(size = 40) %>%
  visOptions(selectedBy = "group", 
             highlightNearest = TRUE, 
             nodesIdSelection = TRUE) %>%
  visInteraction(keyboard = TRUE,
                 dragNodes = T, 
                 dragView = T, 
                 zoomView = T)


visNetwork tooltips


#add a variable with some HTML as a character string
nodes$title <- paste0("<p>Group: ", cluster_fast_greedy(g)$membership, "<br>",
                      "Degree: ", degree(g),"</p>")

visNetwork(nodes, edges, height = "600px") %>%
  visIgraphLayout() %>%
  visNodes(size = 40) %>%
  visOptions(selectedBy = "group", 
             highlightNearest = TRUE, 
             nodesIdSelection = TRUE) %>%
  visInteraction(keyboard = TRUE,
                 dragNodes = T, 
                 dragView = T, 
                 zoomView = T)



10,000 nodes

Next Section