Graph Visualization With Neo4j Using Neovis.js: Leveraging Graph Algorithms For Data Visual...

Leveraging Graph Algorithms For Data Visualization

Graph Visualization With Neo4j Using Neovis.js: Leveraging Graph Algorithms For Data Visual...
Graph Visualization With Neo4j Using Neovis.js: Leveraging Graph Algorithms For Data Visual...
In this post we’ll use a Neo4j Sandbox instance to start with a Twitter dataset, run PageRank and community detection on the data. Then show how to embed a graph visualization in a web app using Neovis.js .

In this post we explore how to create graph data visualizations that use the results of graph algorithms like PageRank and community detection . After running some graph algorithms using the neo4j-graph-algorithms library we’ll use the JavaScript graph visualization library Neovis.js to create visualizations that can be embedded in a web app, fetching data directly from Neo4j.

Goals Of Graph Visualization

There are different motivations and tools for creating graph visualizations. This includes tools for exploring the graph — the type of interactive visualizations you might see in Neo4j Browser . Or visualizations for showing the results of some analysis . These can be interactive (something to be embedded in a web app or even a standalone application), or static, meant to convey specific meaning that might be used in print or a blog post.

This post will focus on one tool that addresses some specific goals of graph visualization. This tool is Neovis.js and is used for creating JavaScript based graph visualizations that are embedded in a web app. It uses the JavaScript Neo4j driver to connect to and fetch data from Neo4j and a JavaScript library for visualization called vis.js for rendering graph visualizations. Neovis.js can also leverage the results of graph algorithms like PageRank and community detection for styling the visualization by binding property values to visual components.

This screencast shows how to use the Neovis.js library to create graph data visualizations styled to the results of graph algorithms with data from Neo4j.

Graph Visualization + Graph Algorithms

There are three common ways that graph visualizations can be enhanced with graph algorithms. Specifically this involves styling visual components proportionally to the results of these algorithms:

  1. Binding node size to a centrality algorithm , such as degree, PageRank, or betweenness centrality. This allows us to see at a glance the most important nodes in the network.
  2. Visually grouping communities or clusters in the graph is done through the use of color, so that we can quickly identify these distinct groupings.
  3. Styling relationship thickness proportionally to an edge weight , in social network data this might be the number of interactions between two characters, in logistics and routing data it might be the distance between two distribution centers and is useful for pathfinding algorithms (such as A* or Dijkstra’s).

Getting Started With The Dataset

We’re going to use the Russian Twitter Trolls sandbox as our dataset. This dataset contains tweets from known Russian Troll accounts, as released publicly by NBC News. You can create your own Neo4j Sandbox instance here.


   The data model for the Twitter Trolls dataset includes Tweets, Users, Hashtags, URLs shared in Tweets and mentions and retweets.
The data model for the Twitter Trolls dataset includes Tweets, Users, Hashtags, URLs shared in Tweets and mentions and retweets.

In graph data, often some of the most interesting relationships are inferred , and not directly modeled in the data. The User-User retweet graph is an example of this. Which users are retweeting which other users? Who is the most important User in this retweet graph? Are there groups of users that frequently retweet each other?


   Here we have a tweet that was posted by a user, that another user retweeted. This implies a user-user retweets relationship. Our first step will be to find those inferred retweets relationships and run our graph algorithms.
Here we have a tweet that was posted by a user, that another user retweeted. This implies a user-user retweets relationship. Our first step will be to find those inferred retweets relationships and run our graph algorithms.

To find the most important users and communities using this retweet network we will first find all Troll users and create a RETWEETS relationship connecting directly the users in the graph. We store a count property on the relationship representing the number of times that the user has retweeted the other:

Once we’ve created these RETWEETS relationships we can run PageRank over this part of the graph (we could also use a Cypher query to run PageRank over the projected graph without explicitly created the relationships):

Since we specify write: true above this will not only run PageRank but add a pagerank property to the nodes contains their PageRank score. We can then query using that property value to find the top ten Troll accounts by PageRank score:

And finally we can run a community detection algorithm on the retweet network, in this case label propagation:

This will add a community property to the nodes, indicating which community the algorithm has determined the node belongs to.

So we’ve now run two graph algorithms (PageRank and label propagation), but how do we make sense of the results? A visualization can help us find insights in the data.

Creating A Graph Visualization With Neovis.js

In order to create a visualization with Neovis.js we first need to connect to Neo4j. In the details tab of our Sandbox instance we can find the connection details for our Neo4j instance:

Graph Visualization With Neo4j Using Neovis.js: Leveraging Graph Algorithms For Data Visual...
Graph Visualization With Neo4j Using Neovis.js: Leveraging Graph Algorithms For Data Visual...

The server connection string, username, and password will be included in a config object that we’ll pass to the constructor for Neovis. We’ll also need to specify what node labels we want to visualize and how they should be styled (which properties determine node size and color).

Neovis.js works by populating a <div> element with the visualization, so we’ll need to specify the id of that element in the config object, as well as how to connect to our Neo4j instance, and which properties to use for determining node size, color, and relationship thickness. Here’s the code to generate a graph visualization of our retweet network from our Neo4j Sandbox instance, using the pagerank property to determine node size, community for color, and the count relationship property for relationship thickness:

And here’s how our visualization looks:

Graph Visualization With Neo4j Using Neovis.js: Leveraging Graph Algorithms For Data Visual...
Graph Visualization With Neo4j Using Neovis.js: Leveraging Graph Algorithms For Data Visual...

There are a few more configuration options which you can read about in the project documentation.

Resources