Gephi, R, SNA, Twitter

GraphTweets – R to Gephi

Let’s get some tweets, build the graph, write as graphml and analyse in with Gephi.

I love R, we all do. However regarding  SNA I find Gephi much more more user-friendly and easy-to-use. I only do SNA on R for the larger graphs (20K nodes +) as Gephi is quite resource intensive. If you want to do all this within R you can check my other example.

Features: Gephi, igraph package and GraphTweets.

EDIT: I have now released a package on cran and elsewhere to do exactly what is shown in this blog post, please look at graphTweets (install.packages(“graphTweets”)). Vignette can be found here.

First download and install Gephi if you haven’t fone so yet.

Get some tweets and use the GraphTweets package to build the fraph and write the graphml file that we’ll be opening in Gephi.

#load or download if not already installed

if("twitteR" %in% rownames(installed.packages()) == FALSE) {
} else if ("igraph" %in% rownames(install.packages()) == FALSE) {
} else if ("GraphTweets" %in% rownames(install.packages()) == FALSE) {
} else {
libs <- c("igraph", "twitteR", "GraphTweets")
lapply(libs, library, character.only=TRUE)

##get some tweets

setup_twitter_oauth("API key", "API secret", "access token", "access secret")

#pull and parse tweets
tweets <- searchTwitter("rstats", n=1500, lang="en")
tw_df <- twListToDF(tweets)

#build edge and node table
edges <- edge_table(tw_df, "text", "screenName", "favorited", "retweetCount", "longitude", "latitude")
nodes <- node_table(edges,"favorited", "retweetCount", "longitude", "latitude")

#build graph and write graphml - select only the two first column of the edge table since the meta-data is with our node table
graph <- graph.data.frame(edges[,1:2], directed=TRUE, vertices=nodes)
write.graph(graph, "D:/graph.graphml", format="graphml")

Now that we have the graph as graphml file and Gephi installed we can just go and open that file with Gephi.

In Gephilayout, start by applying a layout (i.e.:layout=layout.fruchterman.reingold in R). I suggest starting with the force directed algorithms – think of it as a physics engine. I generally go for the Force Atlas 2 layout algorithm, tick Prevent Overlap and play with the Scale untill you get something clear enough. There is no real science to it – fiddle with the parameters – don’t spread your network all over the shop.modularity

Once you have a decent layout you can run the community finder algorithm. In the right taskbar, under Statistics, next to Modularity, click Run. This will bring a new window open where you can change the Resolution. This is bascially the granularity of the community, the lower the more numerous and the smaller the communities and vice versa. The following pop-up gives the output of the algorithm. Do not be alarmed by the large number of communities. Twitter interactions form unconnected graphs – each isolate is technically a commpartitionunity.

Now we need to apply that to the graph, very simple again in Gephi. In the top left under Partition, Nodes hit the green “refresh” arrows then select Modularity in the dropdown menu. This will colour the nodes by community. Tip: right-click in the box and randomize colours until you get something decent.

In the top left corner again, under the Ranking tab and hit the Diamond to change the size of the nodes according tolabels your liking i.e.:degree. You can then, at the bottom of the canvas, show labels according to node size.

Finally save a nice png or pdf of your graph in the preview window.


Obviously, we find @hadleywickham and @Rbloggers right at the center of our graph. If you haven’t done so yet, go and check R-bloggers; it aggregates over 500 R-related blogs.




  1. Pingback: Tinkering with R and Social Network Analysis – Geocaching Librarian - May 21, 2016


Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: