Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Recently the R package visNetwork was brought to my attention. It is an R interface to the ‘vis.js’ JavaScript charting library where you can create interactive network graphs. To try out the package I need some data, luckily I still have some restaurant reviews data from an earlier exercise. See my previous post. You need two data frames to plot a network in visNetwork, One is a data set with nodes, in my case a bunch of restaurants that have been reviewed. The second is a data set with edges, it has two columns (from and to), each row in this set shows the link between two restaurant for a network graph. How are two restaurants linked in my case? When a reviewer visited restaurant A and also visited restaurant B then the two restaurants are linked. In the edges data set I would have a row:  A   B.

Both the nodes and edges data sets can contain some extra columns which can be used in the network graph. For example, in the nodes data set I have used restaurant type (French, Chinese, Indian,…) to color my nodes. And in the edges data set I have specified the strength of the edge/link. In my case I have defined the strength as the number of reviewers. So, If reviewer x, y and z have reviewed restaurants A and B then the strength of edge A-B is 3.

Before I went to the university I have lived in the lovely city of of Hoorn in the Netherlands. Lets look at the restaurants in Hoorn and how they are connected. The restaurant nodes data set looks like:

List of restaurants in Hoorn

The restaurant edges data set looks like:

Given the two data sets, the R code to create the network is given by

visNetwork( RestaurantNodes, RestaurantEdges, legend = TRUE) %>%
visOptions( highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
visPhysics( maxVelocity = 35)


There are many options you can set, see the help for more information. I have just used a couple. A handy option is highlightNearest, when there are many nodes you can select a node and only the nodes nearest to the selected are highlighted and the rest is grayed out. Here are some screen shots of my network graph, click to enlarge.

network graph of restaurants

Zooming in on the core

Don’t they look lovely? In the interactive graph the nodes can ‘vibrate’, just like atomic nuclei.  I have published the network graph on Rpubs so that you can interact with the graph yourself. Apart from the nostalgic reason to choose restaurants in Hoorn, there is another reason, when there are too many nodes and edges the graph does not perform well. Trying to create this graph for all restaurants in the Netherlands is not possible.