Neat R Package for Graphing Facebook Friend Networks

fbnetwork3

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 (http://cran.cnr.berkeley.edu/)

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 - 
#https://github.com/pablobarbera/Rdataviz/blob/master/code/05_networks.R

# 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.

    setwd("/Users/meg/Desktop")

# Installing a few packages. Packages are add-ons to R that help it do
# functions that aren't built-in. ONLY RUN THESE ONCE
    install.packages("igraph")
    install.packages("ggplot2")
    install.packages("Rfacebook")

# Load libraries - now the packages are installed, you just have to 
# load them
    library(igraph)
    library(ggplot2)
    library(Rfacebook)

# 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 <- fastgreedy.community(friend_network)

# 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 = 
    cluster))

    # 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
    network_plot

Did you pledge to vote this year? How political science research can be applied to political campaigns

If you were on the William & Mary campus at all this semester, you might have been asked to sign a “Pledge to Vote” card by an organizer working for the McAuliffe campaign, or even the Sierra Club. In just a few years, voter pledge cards have become important parts of campus political organizing, particularly for Democratic campaigns.

While they might seem like a waste of time, campaigns have started taking these cards seriously thanks to a 2004 working paper by Donald Green titled “The Effects of an Election Day Voter Mobilization Campaign Targeting Young Voters.” In the paper, Green used a field experiment to demonstrate that young voters in New Jersey voted at higher rates when called on Election Day and reminded, and that this effect was strongest for individuals who had been previously contacted and had told the canvasser that they would vote. Translating this to use for a campaign, Rock the Vote and several other groups interested in youth turnout created “Pledge to Vote” postcards to get young voters to commit to vote and then mailed them back right before Election Day as a reminder. In my experience, pledge cards are sometimes misapplied (pledges are often biased toward individuals who are already likely to vote, rather than including infrequent young voters who might benefit most from a reminder), but they are an interesting example of how political campaigns are paying attention to political science research and working it into their strategies.

Here at the SNaPP Lab, we’re also working on several projects that could have really cool campaign applications:

  • Professor Settle and the Social Anxiety group in the Lab are working on interesting research on how personality differences, particularly in response to anxiety, may influence who participates and who withdraws from contentious political situations. This research could be useful for campaign targeting—voters likely to respond favorably could be targeted with traditional messaging about the importance of an election, while those more likely to withdraw could be targeted with less anxiety-inducing messages in order to increase turnout for both groups.
  • The Lab’s Online Political Discussion group has been looking at ways in which online political discussion is both similar to and different from traditional face-to-face political discussion. A better understanding of how people discuss politics online could be useful for campaigns that are increasingly trying to get their message out on the Internet.
  • My senior honors thesis looks at how political direct mail can turn out infrequent voters at higher rates by combining social pressure mobilization tactics with loss aversion related to issues in the election. If it works (fingers crossed), political campaigns could use this technique as a new tool to increase turnout at low cost.