Recipe 6 Creating a Graph of Retweet Relationships

6.1 Problem

You want to construct and analyze a graph data structure of retweet relationships for a set of query results.

6.2 Solution

Query for the topic, extract the retweet origins, and then use igraph to construct a graph to analyze.

6.3 Discussion

Recipes 4 and 5 introduced and expanded on searching Twitter plus looking for retweets. The igraph package can be used to capture and analyze details of relationships across retweets. We’ll focus on just examining the Twitter user pair relationships.

Let’s get a larger sample this time — 1,500 tweets in #rstats. We can use the technique from the previous recipe and:

  • find the retweets (using the API-provided data)
  • expand out all the mentioned screen names
  • create an igraph graph object
  • look at some summary statistics for the graph

You can reference the igraph print() and summary() functions for more information on the output of summary() but output from the following line shows that the graph is Directed with Named vertices and it has 1,106 vertices and 1,945 edges.

## IGRAPH b4b8447 DN-- 1106 1945 -- 
## + attr: name (v/c)

We’ll produce more visualizations in the next recipe, but the degree of graph vertices is one of the most fundamental properties of a graph and it’s much nicer to see the degree distribution than stare at a wall of numbers:

6.4 See Also