How To Get Started with Social Network Analysis
By: Mitchell Telatnik
Tags: Guides And Tutorials, Data Science, Data Visualization, Marvel, Social Network Analysis

Implementation

Now that you have an understanding of social network analysis terms and concepts, this guide will walk you through applying these techniques to a dataset using the Gephi software.

Download and Install Gephi

First, download and install the Gephi software for the operating system your machine is running. Gephi is available for Mac, PC, and Linux and can be downloaded here.

Dataset

For this guide, we will be using the Marvel Universe Social Network dataset from Kaggle. While this dataset is already laid out with a node and edge list, when working with datasets not structured as a network this will require some data transformation skills. I recommend using Python and Pandas in these situations.

The dataset can be downloaded here.

After downloading the dataset, there will be three csv files: nodes, edges, and network. Open the file nodes.csv in Excel.

nodes.csv in Excel

The nodes file contains a list of all the nodes in the network. This file has two columns: node and type. This network contains two different types of nodes that represent different actor types. If you are familiar with object-oriented programming, you can think of the node type as a class and nodes as objects. The two types of nodes in this network are heroes and comics.

There is no data preparation needed to import this node list into Gephi, so we will close the file.

Open the file edges.csv in Excel.

edges.csv in Excel

The edges file also contains two columns: hero and comic. Each row in this table represents a single edge. The hero node and comic node are the two nodes connected by the edge.

In Gephi, an edges table requires the column headers of ‘source’ and ‘target’. In an undirected network it does not matter which node is in which column. However, in a directed network the source column contains the starting node and the target column contains the ending node. Rename column A to ‘source’ and column B to ‘target’. Then save the file.

edges.csv with renamed column headers

Loading Network Data into Gephi

Now that the node and edge lists are properly formatted for Gephi, it is time to load the data!

Open the Gephi software. You should see the below screen.

Click on ‘New Project’. If you do not see the welcome screen, go to file>new project.

Then, click the Data Laboratory tab.

The data laboratory tab is where we will load in our edge and node list files. To import a list click the import spreadsheet button.

Then navigate to the folder containing the datasets and open the nodes file.

An import wizard will then step you through correctly importing the node list. Set Separator to Comma, Import as to Nodes table, and Charset as UTF-8. Then click next.

After clicking next, the wizard will provide additional setting configurations. Set Time representation to Intervals. For Imported columns, check the node and type boxes and set their data types to string. Then, click finish.

There is one more step in importing the nodes list. Set Graph Type to Undirected and Edges merge strategy to Sum. Ensure that it is set up to append to the existing workspace. Then, click OK.

You should now see some data in the data laboratory window! Next we need to import the edges list.

To import the edges list, click on Import Spreadsheet and open the edges.csv file. In the import wizard set Separator to Comma, Import as to Edges table, and Charset to UTF-8. Then click next.

Set Time representation to Intervals then click Finish.

Then, set Graph Type to Undirected and Edges merge strategy to Sum. Choose append to existing workspace. Click OK.

Congrats! You have just imported the node and edge lists! In the data library, you can switch your view between these two lists by clicking on Nodes or Edges in the top left-hand corner.

Now that the data has been imported it is time to view the graph. Click on the overview tab.

Using Layout Functions

You might be disappointed in the graph that was visualized. It will likely look like the black mess below.

In order to make the graph more readable, we will need to use a layout function to change the position of nodes in the graph.

There are a variety of layout functions in Gephi, however, in this guide, we will be using the ForceAtlas 2 function. Select this function and then click run. You will see the nodes move in real-time, and you can stop the function when you like the position of the nodes.

After running the layout function your graph should look something like the one shown below. You can continue to play with other layout functions if you wish to get a better node position. This is by no means the best node layout for this graph. In addition, you can change the parameters of layout functions. While this guide uses the stock ForceAtlas 2 parameters, changing them can give you better control over the node positions.

Calculating Network-Level Measures

This guide previously covered the network-level measures of Size and Density. Let's calculate what the network size and density of this Marvel network are.

The network size is easy to find. In the upper right-hand corner is a pane called Context. This window provides the number of nodes and edges in the graph. Because a network’s size is the number of nodes in it, the network size of our Marvel network is 19,090.

To find the network density, we will take our first dive into the statistics window. Click on the statistics tab shown below.

You should then see the below window.

The statistics window contains many measures that can be calculated on the network. To find the network density, click run for Graph Density.

Select undirected, and then click OK.

A new window will then pop up showing the results. This Marvel network was a density of 0.001

You can save this report by clicking the save button in the bottom left-hand corner, or close it by clicking the close button in the bottom right-hand corner.

Calculating Centrality Measures

Recall that centrality measures are on a node-level, and not a network-level. However, centrality measures can also be averaged to get a network-level metric. In Gephi, you calculate centrality measures as a network-level average, which then also inputs the centrality measure on a node-level into the data laboratory tab.

Node Degree

To calculate node degree, click run on the average degree algorithm in the statistics window.

The report will provide you with the average degree for the network, as well as a distribution graph. While these can be useful in some applications, we are more interested in the degree on a node-level. Click close on the report.

To see the degree for each node in the network, go back to the data laboratory window and click on the node table. You will see a new column in the data titled degree.

Node Closeness and Betweenness

Calculating node closeness and betweenness is a similar process as calculating node degree. In the statistics window, click run on the network diameter algorithm.

Select undirected and click OK. Depending on the specs of your machine this may take a little bit to calculate.

Like with the node measure, Gephi will provide a network-level report. Click close on this report and go to the data laboratory.

In the data laboratory, you will find additional columns in the node table including the node betweenness and closeness.

Calculating Edge Weights

Edge weights are auto-calculated in Gephi and can be found in the edge list in the data laboratory.

Using Color in Network Graphs

Currently, our graph nodes and edges are black, providing no additional information. Both nodes and edges can be color-coded in Gephi to provide additional information. Coloring options can be found in the appearance window.

To color-code the nodes of the graph based on the node degree, click on the nodes button and the color palette button in the appearance window.

There are three options to encode information in the color of nodes: Unique, Partition, and Ranking. If you want to change the color of all nodes of the graph to the same color, use the Unique window. Partition will break the nodes into color-coded groups. Ranking will color-code the nodes on a scale.

Lets color the nodes by their degree. To do this, click on the ranking section and select degree.

A color scale will be used to color the nodes. To select a new scale, click on the color selector button to the right of the color scale.

You can select any color scale to use. Then click apply.

As you can see in the above image, coloring our nodes also colored our edges. You can change the color of edges to a specific color using the Unique color tab for edges, or apply a ranking or partitioning color scale to them.

Using Size in Network Graphs

You may also notice that the majority of the graph is colored red. This is because most nodes in the graph have a low degree. Zooming in will show that some nodes are yellow or blue.

To make these nodes easier to see in the graph let’s scale the size of the nodes to the node degree as well. To do this click on the nodes and size buttons in the appearance window.

Then, click on ranking and select degree. Change the minimum size to 1 and the maximum size to 100.

Then click apply.

As can be seen in the above graph, we can now better see what nodes have a high degree.

Changing Background Color

Let's also change the background from white to black. Depending on the colors used in a graph, either color may look better and is often up to personal preference. To change the color to black, press the lightbulb button.

Recap

That is the end of this guide. While this should you get started making your first network graph using the Marvel dataset, I encourage you to continue playing around with this graph in Gephi. There are many more measures you can calculate and other appearances you could use.

Your next step should be to take another dataset and try to reproduce these steps on that data. Finally, you can try to collect your own data and transform it into network data.

I hope this guide was useful to you! Feel free to reach out if you have any questions!