visNetwork uses the vis.js library for displaying interactive networks. visNetwork also supports the extremely powerful igraph library for computing graph layouts. visNetwork is well supported and under continuous development.
- [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.
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.
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.
Start by learning to manage packages and structure data for visualizations with the tidyverse and the pipe operator. Then there is an important question: Which library should you choose? The course introduces five popular options: Leaflet, Plotly, Highcharter, visNetwork, and DataTables (DT). Instructor Martin Hadley shows how to use these libraries to create scattergeo, choropleth, and geolines maps; stacked bar charts, scatter charts, bubble charts, and heat maps; treemaps and time series charts; interactive networks and graphs; and responsive, interactive data tables. Plus, learn how to customize your visualizations with legends and tooltips, and extract click information for Shiny apps.
- Installing htmlwidgets
- Selecting the best htmlwidget
- Creating interactive maps with Leaflet
- Creating interactive charts with Plotly and Highcharter
- Creating interactive networks with visNetwork
- Creating interactive tables with DT