Andrew's Data Blog

creatively titled, I know

Exploratory Network Analysis using networkD3 in R

Posted at — Jun 8, 2019

I haven’t had to do much network analysis, so generally it’s been simple when I have. I need to see the general structure, and I want to poke around in it. Drag things around.

I started with other packages that I’m sure are great for network analysis, but I wanted a straightforward exploratory version. The quickest I’ve found is simpleNetwork using the networkD3 package.

If you set up a dataframe correctly, it just works. You can explore and get excited about the data before commiting to a full blown analysis without knowing if it even “might” be useful. I just want to drag things around like those cool d3.js graphs I see.

library(networkD3)

# data we'll use as an example
head(networkD3::SchoolsJournals)
##                               school_1
## 1 Royal Holloway, University of London
## 2 Royal Holloway, University of London
## 3                University of Warwick
## 4                University of Warwick
## 5             University of Nottingham
## 6             University of Nottingham
##                               school_2       journal
## 1                University of Warwick Acta Politica
## 2             University of Nottingham Acta Politica
## 3 Royal Holloway, University of London Acta Politica
## 4             University of Nottingham Acta Politica
## 5 Royal Holloway, University of London Acta Politica
## 6                University of Warwick Acta Politica
# a smaller sample for illustrative purposes. gotta get that zoom though
simpleNetwork(SchoolsJournals[1:100, ],zoom = T)

So mission accomplished. But I found some other freebies on top of that. You can get a standalone html that is the exact same graph as found in the RStudio pane. Share it with friends and family since it opens up in your favorite web browser (probably, I don’t know. Chrome works.)

# save your network from earlier.
mynetwork <- simpleNetwork(SchoolsJournals[1:100, ],zoom = T)

# and make an html as easily as you write.csv (actually easier, no row.names)
saveNetwork(mynetwork,file = 'myd3networkgraph.html')

Once you get that under your belt, you can work on a full fledged version. I haven’t done it, but use forceNetwork. It’s the not so simpleNetwork.

comments powered by Disqus