Code and brief instruction for graphing Twitter with R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Edit: 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 compromise between readability and efficiency.
Edit #2 And yet another update, this one contributed by Kai Heinrich.
I hacked together some code for R last night to visualize a Twitter graph (=who you are following and who is following you) that I briefly showed at the session on visualizing text today at THATCamp and that I wanted to share. My comments in the code are very basic and there is much to improve, but in the spirit of “release early, release often”, I think it’s better to get it out there right away.
Ingredients:
Note that packages are most easily installed with the install.packages()
function inside of R, so R is really the only thing you need to download initially.
Code:
# Load twitteR package
library(twitteR)
# Load igraph package
library(igraph)
# Set up friends and followers as vectors. This, along with some stuff below, is not really necessary, but the result of my relative inability to deal with the twitter user object in an elegant way. I'm hopeful that I will figure out a way of shortening this in the future
friends <- as.character()
followers <- as.character()
# Start an Twitter session. Note that the user through whom the session is started doesn't have to be the one that your search for in the next step. I'm using myself (coffee001) in the code below, but you could authenticate with your username and then search for somebody else.
sess <- initSession('coffee001', 'mypassword')
# Retrieve a maximum of 500 friends for user 'coffee001'.
friends.object <- userFriends('coffee001', n=500, sess)
# Retrieve a maximum of 500 followers for 'coffee001'. Note that retrieving many/all of your followers will create a very busy graph, so if you are experimenting it's better to start with a small number of people (I used 25 for the graph below).
followers.object <- userFollowers('coffee001', n=500, sess)
# This code is necessary at the moment, but only because I don't know how to slice just the "name" field for friends and followers from the list of user objects that twitteR retrieves. I am 100% sure there is an alternative to looping over the objects, I just haven't found it yet. Let me know if you do...
for (i in 1:length(friends.object))
{
friends <- c(friends, friends.object[[i]]@name);
}
for (i in 1:length(followers.object))
{
followers <- c(followers, followers.object[[i]]@name);
}
# Create data frames that relate friends and followers to the user you search for and merge them.
relations.1 <- data.frame(User='Cornelius', Follower=friends)
relations.2 <- data.frame(User=followers, Follower='Cornelius')
relations <- merge(relations.1, relations.2, all=T)
# Create graph from relations.
g <- graph.data.frame(relations, directed = T)
# Assign labels to the graph (=people's names)
V(g)$label <- V(g)$name
# Plot the graph.
plot(g)
For the screenshot below I've used the tkplot()
method instead of plot()
, which allows you to move around and highlight elements interactively with the mouse after plotting them. The graph only shows 20 people in order to keep the complexity manageable.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.