Graphing Twitter friends/followers with R (updated)

On June 25, 2010, in Code, by cornelius

Edit: And here is an update of the update, this one contributed by Kai Heinrich.

Here’s an updated version of my script from last month, something I’ve been meaning to do for a while. I thank Anatol Stefanowitsch and Gábor Csárdi for improving my quite sloppy code.

# Load twitteR and igraph packages.

# Start a Twitter session.
sess <- initSession('USERNAME', 'PASSWORD')

# Retrieve a maximum of 20 friends/followers for yourself or someone else Note that
# at the moment, the limit parameter does not [yet] seem to be working.
friends.object <- userFriends('USERNAME', n=20, sess)
followers.object <- userFollowers('USERNAME', n=20, sess)

# Retrieve the names of your friends and followers from the friend
# and follower objects.
friends <- sapply(friends.object,name)
followers <- sapply(followers.object,name)

# Create a data frame that relates friends and followers to you for expression in the graph
relations <- merge(data.frame(User='YOUR_NAME', Follower=friends), data.frame(User=followers, Follower='YOUR_NAME'), all=T)

# Create graph from relations.
g <-, directed = T)

# Assign labels to the graph (=people's names)
V(g)$label <- V(g)$name

# Plot the graph using plot() or tkplot().

Tagged with:  

4 Responses to Graphing Twitter friends/followers with R (updated)

  1. [...] I’ve posted an updated version of the script here. It is not quite as compressed as Anatol’s version, but I think it’s a decent [...]

  2. Boris says:

    initSession function isn’t recognized even after loading the twitteR library.

  3. cornelius says:

    @Boris: the problem is that twitteR has to date not been updated to support OAuth, which has become the only way of authenticating with Twitter after Basic Auth was shut down last year. Here’s hoping that someone implements OAuth for TwitteR at some point — in the mean time, the above code is basically useless. :-(

  4. [...] I have previously posted code for graphing your Twitter friend/follower network using R (post #1. post #2). Last week Kai Heinrich was kind enough to send me some updated code for doing so using a newer [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>