Those of you following my occasional updates here know that I have previously posted code for graphing Twitter friend/follower networks using R (post #1. post #2). Kai Heinrich was kind enough to send me some updated code for doing so using a newer version of the extremely useful twitteR package. His very crisp, yet thoroughly documented script is pasted below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
# Script for graphing Twitter friends/followers # by Kai Heinrich (email@example.com) # load the required packages library("twitteR") library("igraph") # HINT: In order for the tkplot() function to work on mac you need to install # the TCL/TK build for X11 # (get it here: http://cran.us.r-project.org/bin/macosx/tools/) # # Get User Information with twitteR function getUSer(), # instead of using ur name you can do this with any other username as well start<-getUser("YOUR_USERNAME") # Get Friends and Follower names with first fetching IDs (getFollowerIDs(),getFriendIDs()) and then looking up the names (lookupUsers()) friends.object<-lookupUsers(start$getFriendIDs()) follower.object<-lookupUsers(start$getFollowerIDs()) # Retrieve the names of your friends and followers from the friend # and follower objects. You can limit the number of friends and followers by adjusting the # size of the selected data with [1:n], where n is the number of followers/friends # that you want to visualize. If you do not put in the expression the maximum number of # friends and/or followers will be visualized. n<-20 friends <- sapply(friends.object[1:n],name) followers <- sapply(followers.object[1:n],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 <- graph.data.frame(relations, directed = T) # Assign labels to the graph (=people's names) V(g)$label <- V(g)$name # Plot the graph using plot() or tkplot(). Remember the HINT at the # beginning if you are using MAC OS/X tkplot(g)
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.
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.
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.
# Load twitteR package
# Load igraph package
# 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.
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.