FCBarcelona vs Real Madrid Players Twitter Popularity

I created a simple visualization showing the number of followers of each of the players of FCBarcelona and Real Madrid.

In this case the result is a simple bar chart. Barcelona fans, don’t get mad, Messi doesn’t have Twitter account…he is more active on Facebook and I will generate the same kind of report based on Facebook data.

Below you will find the source code to do all this and in the coming days I will illustrate how to get exactly the same results but using only SPSS, some new R nodes and without coding at all. So…stay tuned!

Link to Full Screen Chart

To get started you will need the following CSV file with the players of FCBarcelona and Real Madrid:

http://armandruiz.com/SPSSmaps/SoccerPlayers.csv

I developed thisĀ using R and the following R Packages:

twitteR: good package to connect to your Twitter Application and query data. In this case I was query the main information of each of the players.

-rCharts: Package to create, customize and easily publish interactive JavaScript visualizations.

-Datatables: Displays R matrices or data frames as interactive HTML tables that support filtering, pagination, and sorting.

Here you have the source code, enjoy!

#Authenticate your Twitter Application
library("ROAuth")
library("twitteR")
library("rCharts")
library("DT")
 
 
options(RCurlOptions = list( capath = system.file("CurlSSL", "cacert.pem", package = "RCurl"), ssl.verifypeer = FALSE))
reqURL<-"https://api.twitter.com/oauth/request_token"
accessURL<-"https://api.twitter.com/oauth/access_token"
authURL<-"http://api.twitter.com/oauth/authorize"
 
consumerKey<-"xxxx"
consumerSecret<-"xxx"
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
 
credentials<-OAuthFactory$new(consumerKey=consumerKey,
                              consumerSecret=consumerSecret,
                              requestURL=reqURL,
                              accessURL=accessURL,
                              authURL=authURL)
credentials$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
save(credentials, file="credentials.RData")
 
#Read the CSV file
players<-read.csv('SoccerPlayers.csv',stringsAsFactors=F)
 
#Get the number of followers for each file and create a new column
for (i in 1:nrow(players) ) {
  players$followers[i] = followersCount(getUser(players$TwitterName[i], cainfo="cacert.pem"))
  print(players$followers[i])
}
 
#Sort by number of followers
players<-players[ order(players$followers, decreasing=TRUE), ]
 
#Plot using rCharts
d1 <- dPlot(
  x ="PlayerName" ,
  y = "followers",
  groups='Club',
  data = players,
  type = "bar"
)
d1$legend(
  x = 60,
  y = 10,
  width = 700,
  height = 20,
  horizontalAlign = "left"
)
d1
d1$save('SpanishFootball.html',cdn=TRUE) 
 
#Create a HTML datatable with filtering, pagination and sorting 
datatable(players)
Share this:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">