Created
January 18, 2012 11:28
-
-
Save simecek/1632552 to your computer and use it in GitHub Desktop.
Facebook Friendship Graph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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