Analysing the Twitter Mentions Network

June 1, 2015

(This article was first published on Mango Solutions, and kindly contributed to R-bloggers)

By Douglas Ashton, Consultant

One of the big successes of data analytics is the cultural change in how business decisions are being made. There is now wide spread acceptance of the role that data science has to play in decision making. With the explosion in the quantity of data available, the task for the modern analyst is to filter through to find the information that is most relevant.

Twitter represents a classic case. The volume and velocity of Twitter data is staggering, and as discussed in the first part of this series it is within reach to obtain large, clean datasets. This puts the pressure on the analyst to ask the right questions of the data. In the remaining parts of this series we’ll be looking through a variety of ways to view the data from Twitter. The view you wish to take will ultimately depend on the question that is being asked. In this post we’re tackling the question: “Who are the big players in the data science Twitter community?”.

The dataset that we will be using is a sample of all tweets tagged with the hashtags #datascience, #rstats and #python (snake related tweets cleaned out) between the 7th and 17th December 2014. Each tweet contains a number of pieces of useful information. The view that we’re going to take in this post is the mentions network.

Mentions Network

There are a number of ways users can interact on Twitter. Users can “follow” other users to receive regular updates of their tweets, and users may “mention” other users in their own tweets.


In the tweet above I mentioned @MangoTheCat in the text of the tweet and so we draw a directed link from me, @dougashton to @MangoTheCat. A retweet is another way for one user to mention another. We then went through each of the 22545 tweets in our data set and formed links for every mention. The resulting network contained 8220 nodes (users) and 20461 edges (mentions). In network language an “edge” is the same as a link.

A useful tool for dealing with networks in R is the feature rich igraph package (also available for Python and C). Once you have created your network as an igraph object many of the standard network analysis tools become easily available.




While igraph has nice built-in plotting tools, for large graphs I also like the cross-platform, open source, Gephi. Gephi is an interactive network visualisation and analysis tool with many available plugins. We can easily export our graph from igraph to an open format, such as graphml, and read it into Gephi. The layout below shows the full mentions network and was created using the Force Atlas 2 layout.



Even with the limited sample that we used, this is a big network. This visualisation is useful as a high level view of the network. For instance, we have a connected core of tweeters and a disconnected periphery, which is to be expected with this type of sampling technique. We can also see a common motif of clusters of nodes around one other node, these look like parachutes in the visualisation. To gain further insight we must dig a little deeper into the data.

Broadcasters and Receivers

As noted above, this network appears to contain some nodes that are surrounded by large cluster of other nodes. We can quantify this by looking at the degree centrality. The in-degree of a node is the total number of tweets that mention that user. Similarly the out-degree is the number of mentions made by that user. The top ten nodes for both in and out degree are listed below:


The two lists are completely different. We have a group of users with a large out-degree who retweet at a high rate. These are our broadcasters. In general they tend to pass on content. The users with a large in-degree have their tweets retweeted many times by different users. These are our receivers.

It might be that you can stop there. The nodes with a high in-degree are likely important nodes in this network. However, how do we really know how far their influence really goes? It is possible to get a high in-degree score by being retweeted many times by a small group of broadcasters. This is not a guarantee of influence. For a broader view we must go beyond this nearest neighbour approach and look to more sophisticated network measures.

Centrality Measures

There are many standard measures of network structure available and the choice of which one to use really comes down to exactly what you are interested in. Here I’ll go through two, Page Rank and Betweenness Centrality. In igraph it’s as easy as running





Page Rank is the basis of Google’s search engine. Roughly speaking it tells you which nodes you are likely to land on if you spent some time surfing twitter feeds randomly following links. If mentions tend to flow towards you, you will get a high score. It also shows that you are connected to other influential nodes.

Betweenness Centrality is a useful measure for networks with a strong community structure. If you work out all of the shortest paths between all of the nodes, betweenness tells you how many of those paths go through each node. If you are the bridge between two communities then you will get a high score.









This time we see many of the same users but now some familiar names begin to appear in these lists. For instance, we know that @hadleywickham is an influential figure in the R community, and while Hadley only tweeted 23 times in this period he features high up the Page Rank centrality list.

@MangoTheCat only tweeted four times in this period yet the cat’s betweenness score is relatively high. This implies that the connections formed in those tweets were bridging connections between different types of node. We can see this a little better if we look at a much smaller version of our network. The strongly connected component is the part of the network where you can travel from any node to any node along the links. We get this by finding the clusters and keeping the largest.



While a little out from the core we see that @MangoTheCat does indeed sit between the core and a group on the edge of the cluster.

Summing up

We’ve seen that the tools available in R have made aquiring and analysing the Twitter network an easily accessible task. In this post we’ve dipped our toe into the vast array of methods available in network analysis and we’ve found that digging a little deeper than simply counting connections can lead to a deeper insight of the network’s true function. With so much data available it is important to ask the right questions. If your goal is to improve your social media presence, or whether you are trying to better search for influential users, a little bit of the right network tools can go a long way.

To leave a comment for the author, please follow the link and comment on their blog: Mango Solutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)