Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Network Diagrams

DS 2023 | Communicating with Data

Overview

Networks diagrams are visual representations of mathematical graph.

A graph consists of a set of vertices, or nodes, and edges, or links.

Typically, nodes are represented as shapes, like circles, and edges are represented as lines.

Nodes may represent individuals, groups, or categories, and edges represent any kind of relationship between those nodes.

For example, nodes can stand for individual people and edges can stand for relationships between people.

Nodes can also stand for abstract entities like ideas and edges can stand for the logical relationships between them.

There are numerous methods for arranging nodes and edges in a diagram, from force directed to circular to hierarchical layouts.

Use cases

Network diagrams can be used to detect centrality, betweenness, and other properties of graphs.

They are particularly useful for social network analysis (SNA).

Networks diagrams can be used to detect outliers and clusters.

They are also great at showing structural patterns in populations.

A simple example

We will begin with the Penguins dataset.

We are going to create a “bipartite” graph.

This means we are going to see how two categorical variables are related to each other.

For this example, we will look at species and island, two variables we have looked at through other lenses.

Import the libraries.

Read in the data.

Loading...

Create a table of nodes.

We first need to create a table of nodes.

Nodes are the unique values of the variables we have chosen, along with some properties.

An easy way to generate this table is with value_counts.

Loading...

Create node types table.

We also want to have data associated with the types of node.

This will make it easy to define display attributes, such as shape and color, as visualization time.

Loading...

Add node type attributes to nodes.

We join the type data with the node instances for convenient access.

Loading...

Create a table of edges, aka links.

Next, we create the links.

These are all the relationships between our two variables.

As with alluvials, we want all the combinations of the domains in the two variables.

An easy way to generate these is, again, with value_counts.

Loading...

We also want to provide the IDs of the nodes.

Note that we remove combinations without counts.

Loading...

Now let’s create a crude visualization of the graph.

<Figure size 600x600 with 1 Axes>

Pause to inspect the properties of the graph.

In particular, check out the degrees:

  • For Adelie, d=3d = 3, meaning it lives on three islands.

  • For Torgersen and Gentoo, d=1d = 1, meaning they each have only one relationship.

Using iGraph

Create the graph object.

IGraph will take our node and edge tables and create a graph object from them.

A graph object will contain all the data, plus a bunch of graph theoretical computions.

It is also capable of being plotted.

To create the graph, we just move our data into the object.

Plot the graph.

Plotting is a matter of calling the plot function and passing it the graph object along with parameters.

Note that some parameters — e.g. shape and color — don’t need to be given in the plotting function since they are already defined in the object.

<Figure size 800x600 with 1 Axes>

Size nodes and edges with weights.

We can also resize the nodes and edges based on their weights.

<Figure size 800x600 with 1 Axes>

Finally, we can specify a different layout algorithm.

Here are all the options:

See the documentation for layout options.

Here we specify tree.

<Figure size 800x600 with 1 Axes>

A bigger example

Let’s look at a larger dataset.

We will explore a dataset of wine reviews.

We will create a network out of wine reviewers (tasters) and the countries whose wine they tend to review.

Loading...

Get shape of data set.

(71605, 19)

Create the nodes and edges.

Loading...

Add node styles.

Loading...
Loading...
Loading...

Convert to graph

Explore graph properties.

<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>

Plot.

<Figure size 2000x2000 with 1 Axes>
<Figure size 2000x2000 with 1 Axes>

Generalize

We create three functions to generalize the pattern demonstrated above.

Note that we can easily convert these functions into a single object that would have a method to directly plot a network from a dataframe and node columns.

Test out the functions.

<Figure size 2000x2000 with 1 Axes>
<Figure size 2000x2000 with 1 Axes>