Neat R Package for Graphing Facebook Friend Networks


SNaPP Lab alumnus Drew Engelhardt showed me a cool new package in R yesterday that lets users connect to Facebook’s Graph API and access their facebook info and friend networks from R. I had fun with it, so I thought I’d write up a walkthrough for anyone who might be interested in poking around with R over break. (This doesn’t really assume any prior R knowledge and is based off of this code)

First step – install and open R (

Second step – copy and paste the following R code into a text editor

Third step – follow the instructions in the R code. The only thing you need to add to the code is where it says “token” you need to go to Facebook and generate your own access code. Once you’ve pasted the token into the correct place in the code, select all of the code, paste it into the R syntax window, and hit enter.

# Playing around with Rfacebook - 12.20.2013
# Created by : Meg Schwenzfeier
# Adapted from: Pablo Barbera - 

# Set the "Working Directory" - this is just the folder that R will work
# out of and save things into. Just put the file extension of whichever
# folder you want in quotes.


# Installing a few packages. Packages are add-ons to R that help it do
# functions that aren't built-in. ONLY RUN THESE ONCE

# Load libraries - now the packages are installed, you just have to 
# load them

# Now go login to facebook and go here to generate a Graph API Explorer 
# access token. It's a long string that will let you access facebook 
# info from R.

# Paste the token in between the quotation marks below. Now R has your 
# access token stored as 'token'

    token = "###############"

# Now you can get your friend network using the getFriends() and 
# getNetwork() functions

    # This is just a list of your friends
    friends <- getFriends(token, simplify = TRUE)

    # This is your friend network - this might take a while
    friend_network <- getNetwork(token, format = "adj.matrix")

# Turn the facebook data into an adjacency matrix using the igraph package
    friend_network <- graph.adjacency(friend_network, mode = "undirected")

# Now you can create the layout for the network by passing the network 
# through the fruchterman reingold function, which sets up where the 
# nodes are laid out in the eventual network graph

    l <- layout.fruchterman.reingold(friend_network)

# Want to color your matrix by groups? Run this function to detect 
# clusters in your network

    cluster <-

# Create the dataframe with all of the information created so far. Data 
# frames in R are basically like excel spreadsheets

    network_data <- data.frame(l); names (network_data) <- c("x", "y")
    network_data$cluster <- factor(cluster$membership)
    network_data$name <- friends$name

# Creating a list of connecting lines (edges) for the network
    connectors <- get.edgelist(friend_network, names = FALSE)
    connections <- data.frame(network_data[connectors[,1], c("x", "y")], network_data[connectors[,2], c("x", "y")])
    names(connections) <- c("x", "y", "x2", "y2")

# Now you can plot it!

    # plotting the basic network
    network_plot <- ggplot(network_data, aes(x = x, y = y, color = 

    # adding the connectors
    network_plot <- network_plot + geom_segment(aes(x = x, y = y, 
            xend = x2, yend = y2), data = connections, size = 0.25, 
            color = "grey", alpha = 1/3) + geom_point()

    # Cleaning it up a little bit and adding a title
    network_plot <- network_plot + ggtitle("Facebook Friend Network") + 
           xlab("") + ylab("") + theme(axis.text.y = element_blank(), 
           axis.text.x = element_blank(), axis.ticks = element_blank(),
           panel.background = element_rect(fill="white"), 
           legend.position = "none")

    # look at the plot