From the course: R: Interactive Visualizations with htmlwidgets

What is the visNetwork library? - R Tutorial

From the course: R: Interactive Visualizations with htmlwidgets

Start my 1-month free trial

What is the visNetwork library?

- [Instructor] The visNetwork Library allows us to create interactive network diagrams or graph. These are incredibly useful for visualizing the connections and relationships between individuals, locations, and other data sets as well. Networks are used less often than other types of visualization, so it's useful to come up with and example of where networks are a useful representation of your data. In the exercise files, there is a file called map-of-journies.R; let's open that. And if I select all of the code in the script bar with command A and hit command enter, what this generates for me is a geo lines map. It shows number locations on the globe with lines between them and there are both start and end points and points which are both send and receive locations. So what this represents is a number of journeys between Europe and the United States of America. Now displaying the data in this way doesn't really provide us with a tangible understanding of which locations are linked with which. Is it possible to move from any location in the United States of America to any other going via Europe? To answer this kind of data, we should re-represent the data as a graph and then a network visualization is a very useful way to understand the connectiveness of the data. In our script files, I've written code that converts that data into a format that visNetwork will accept. So if we open make-network-data.R and we select all of the code within the script file with command A and hit command Enter, this outputs two files: a nodes.csv file and an edges.csv file. Let's import these and visualize it with visNetwork. So I'll go to example-network.R. I'll load at the top of the script file the tidyverse and the visNetwork Library will import our nodes and our edges and then on line nine, we use visNetwork to visualize our network. And we can see that we do not have a fully connected network; it is not possible to move from any one location to another other location on the map. Now we can beautify this network quite a bit. So, let me just quickly do that on line 13 and 14. Line 16, I'll add a new column called width. I'll add a new column called color, title and group on lines 19 through 22 and then I'll visualize this new network on line 24 and 25. Here we can see the same network as before, but now we've colored the nodes by the country that they belong to and if I zoom in on this connected component here, we can see that this location in Germany is connected to all of these other locations and we can see a slightly thicker line between this city and Rochester in New York, which indicates to me there are more journeys between these two locations than any other in this small connected component. Now igraph is a very useful library for analyzing graph and we can use igraph objects directly with visNetwork. So this is the exact same network, but generated from an igraph object and here we can see arrows on our edges. Now we can decompose our network so it extract just this connected component here through the decompose function. Igraph also provides a number of different layouts to us. One of these is the hierarchical layout. If we apply this to our data, we can see the location from before is set at the top of this visualization because we have many children nodes that fit into it. It's also possible to cluster nodes. Here I load data from the Game of Thrones network, which I visualized on line 51 through 52. If I zoom in a little bit, I can see character names and their connected to characters that they have interactions with inside of the Game of Thrones universe. On line 54 through 58, I cluster the data by a column called super culture. Let's see how this looks. So I now only have a few different types of node. We can see if I zoom in, that these nodes are clustered on the unknown culture group. And this node here is simply Renly Baratheon. Now if I go back to the unknown culture node and double click on it, it explodes out and shows all of the members of that group. I can re-cluster the network as well simply by clicking re-initialize clustering. VisNetwork also supports to some extent legends. Line 71 through 73 generates me the same visualization as before, but with a legend showing me each of the super cultures in my data set. Finally, we can ask visNetwork to highlight nearest neighbors to us. So when visNetwork is generated for me, when I hover over a node, it highlights for me just the nearest neighbors of this individual. So visNetwork is a really powerful library that allows us to create a wide range of different types of interactive networks but how is it able to do that? Well, visNetworks is an example of a htmlwidget library. It binds to the wonderful vis.js JavaScript library. What can we do with these interactive network visualizations? Well, like all htmlwidgets we can easily include these outputs in RMarkdown documents or even in Shiny apps. How does visNetwork compare to other options for visualizing graph. Well in my opinion visNetworks is hands down the most fully featured and flexible htmlwidget library for visualizing networks. At the time of recording, I never even consider a different option if I want to visualize a network inside of R. One of the best things about the visNetwork library is what we just saw; it's tight integration with igraph. Igraph is an extremely powerful tool for analyzing networks and the objects created by igraph can be given directly to visNetwork. As well as being good at analysis, igraph has a wide range of very fast, efficient, algorithms for computing graph layouts and visNetwork can easily use these algorithms to speed up it's own graph layout process. When talking about the limitations of htmlwidgets it's important to first ask, about what license the underlying JavaScript library is subject to. Well, vis.js is jewel licensed under Apache 2.0, and MIT, meaning it can be used freely without restriction. Similarly the visNetwork library may be used freely as well. One of the main limitations I come up against when I use visNetwork is the fairly inflexible legends that it supports. I tend to find that if my legend has more than five elements in it, it becomes difficult to read. Another limitation is that the default visNetwork layout algorithm is extremely slow. Because visNetwork supports the igraph layouts, including the awesome layout nicely layout from igraph, I recommend that you just default to always using visIgraphLayout on all your visNetworks. It will radically decrease the amount of time you spend waiting for visualizations to appear on screen. Shiny is a framework for building interactive web applications using the R language. VisNetwork provides good support for Shiny apps. You can easily extract the following information from the visNetwork graph visualization. The select a node ie. which node was just clicked or double clicked. It's also possible to modify an existing visNetwork visualization by proxy. This is extremely useful if you visualized a large network and you want to recolor or remove some nodes, but don't want to have to wait for the graph layout to be recomputed. Let's see a quick example of that in the exercise files. So if I move to the file explorer, and go to server.R and run our Shiny app, we can see it currently says no node has been clicked yet, but if I zoom into the network we can see it's the same network as before from the Game of Thrones universe. If I select Joanna Lannister, then it tells me to selected node is Joanna Lannister. What I can do is I can remove certain subcultures. If I wanted to see what does this network look like. If I remove all of the individuals from an unknown culture and now zoom out, we can see how my network looks as a result of removing those nodes via visNetwork proxy. Okay so how about when you want to go and do advance things with visNetwork? Well the developers of the html widget library have tried to be as faithful as possible to the underlying JavaScript API for making network diagrams meaning it's typically possible to implement advance functionality you see a native vis.js diagrams using visNetwork; however, you may need to write short snippets of JavaScript sometimes when doing advanced things. For instance spawning models from interacting with the network or explicit modifications to the layout algorithms. Where should you go to find out more? Well this really good online documentation for the underlying JavaScript library and a great cite dedicated to the visNetwork library too.

Contents