Skip to content

Instantly share code, notes, and snippets.

@simecek
Created January 18, 2012 11:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save simecek/1632552 to your computer and use it in GitHub Desktop.
Save simecek/1632552 to your computer and use it in GitHub Desktop.
Facebook Friendship Graph
# the code uses 'facebook' function from the previous gist (https://gist.github.com/1634662) or
# see the original http://romainfrancois.blog.free.fr/index.php?post/2012/01/15/Crawling-facebook-with-R
# scrape the list of friends
friends <- facebook( path="me/friends" , access_token=access_token)
# extract Facebook IDs
friends.id <- sapply(friends$data, function(x) x$id)
# extract names
friends.name <- sapply(friends$data, function(x) iconv(x$name,"UTF-8","ASCII//TRANSLIT"))
# short names to initials
initials <- function(x) paste(substr(x,1,1), collapse="")
friends.initial <- sapply(strsplit(friends.name," "), initials)
# friendship relation matrix
N <- length(friends.id)
friendship.matrix <- matrix(0,N,N)
for (i in 1:N) {
tmp <- facebook( path=paste("me/mutualfriends", friends.id[i], sep="/") , access_token=access_token)
mutualfriends <- sapply(tmp$data, function(x) x$id)
friendship.matrix[i,friends.id %in% mutualfriends] <- 1
}
require(Rgraphviz)
# convert relation matrix to graph
g <- new("graphAM", adjMat=friendship.matrix)
# ellipse graph with initials
pdf(file="facebook1.pdf", width=25, height=25)
attrs <- list(node=list(shape="ellipse", fixedsize=FALSE))
nAttrs <- list(label=friends.initial)
names(nAttrs$label) <- nodes(g)
plot(g, "neato", attrs=attrs, nodeAttrs=nAttrs)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment