Using Digital Trace Data in the Social Sciences, University of Konstanz (Summer 2018)

Instructor: Andreas Jungherr

Week 10: Sample Analyses: Networks

In this session, we will focus on the use of Twitter data in network analysis. Network analysis offers a powerful perspective on social processes with a focus on the influence of structures on outcomes and processes. Social media data offers a powerful base for research following a network analytical framework.

As a conceptual background to network analyis, I would recommend:

For the mathematics underlying network analysis:

For running network analysis in R:

And as a primer for network analysis:

Code Examples:

Before we begin, let us prepare our workspace:

cd "/Users/(...)/twitterresearch"


import examples
import database
import json

Prepare a network file

Now let’s start on working with network data. As a first step, let’s create .graphml files of retweet and @mention networks in messages you collected.

import network

Now, let’s create a retweet network:


And now, let’s do the same for @replys


@ReTweet Networks

Now, we are ready to work with the network data in R. For this, start R Studio and call:


library(igraph) <- read.graph("retweets.graphml", format="graphml")

Edges and nodes (here called vertices) can be accessed with the following commands:

This one shows a list of vertex IDs:


This one shows a list of edges with source ID-> target ID notation:


Let us now look for specific users. So, where is Donald Trump in our network:

trump <- which(V($label == "realDonaldTrump")

And what are his network metrics, like degre…

degree(, v=trump)

… and indegree.

degree(, v=trump, mode="in")

Now, let’s write the degree as a node attribute:

V($degree <- degree(

The same can be done for in-degree:

V($indegree <- degree(, mode="in")

To visuall inspect the degree distribution we can plot it in a log-log plot

plot(degree.distribution(, mode="in"), log="xy")

Let’s continue by sorting users by indegree


Now, who are the top 10 users by indegree? Let’s identify them and print their names.


By creating a small subset of network graphs, we can gain quick and intuitive insights withoht being flooded by information of the whole network. For convenience let’s focus on the top 25 nodes by indegree. You could use any othe characteristic of nodes to define a subset of interest to you.

Let’s get the top 25 nodes:

top25.nodes <- V([order(-indegree)][1:25]

Now, we create a small graph by deleting all nodes except the top 25. <- delete.vertices(, which(!V( %in% top25.nodes))

To plot the network graph we choose a layout determined by the Fruchterman Rheingold algorithm. This one works nicely for small graphs.

l <- layout_with_fr(

And plot it with corresponding labels:

plot(, l)

We can do the same by focusing on a different metric identifying prominence in the network by another criteria instead of raw indegree counts, such as betweeness centrality…

V($betweenness <- betweenness(
top25.nodes <- V([order(-betweenness)][1:25] <- delete.vertices(, which(!V( %in% top25.nodes))
l <- layout_with_fr(
plot(, l)

… or closeness centrality.

V($closeness <- closeness(, mode="in")
top25.nodes <- V([order(-closeness)][1:25] <- delete.vertices(, which(!V( %in% top25.nodes))
l <- layout_with_fr(
plot(, l)

@Reply Networks

In analyzing @Reply networks, we follow the same steps as before: <- read.graph("replies.graphml", format="graphml")

V($indegree <- degree(, mode="in")
V($betweenness <- betweenness(
V($closeness <- closeness(, mode="in")
V($closeness.out <- closeness(, mode="out")

top25.nodes <- V([order(-closeness)][1:25] <- delete.vertices(, which(!V( %in% top25.nodes))
l <- layout_with_fr(
plot(, l)

As an alternative, you could simply load the .graphml file into Gephi or your network analysis program of choice.

As before, this is only dipping our toes into the potentials of network analyis. Make sure to dive deeper according to your interests.

Required Readings:

Background Readings:

Week 9Week 14