Fun with twitteR: Osama bin Laden tweets
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I thought it would be fun to play around with the R package twitteR , an R API into Twitter. I decided to take the most prominent news story of the past few days, Osama bin Laden’s death, to see the progression of tweets as news spread. I quickly found that the max results for the searchTwitter() function were 1500 per day since this is such a big story right now. I decided to narrow the results by location using the geocode parameter to look at how people in Denver were tweeting. I used the Colorado State Capitol Building (39.739567,-104.984794) with a radius of 6 miles. I am still maxing out the results per day, but it looks like a better representation than at a 5 mile radius. I’m still new to this API so maybe with a little tweaking, I can improve the results.
library(twitteR) #Colorado State Capitol Building geoLocation = '39.739567,-104.984794,6mi' tweets <- searchTwitter("Osama Bin Laden", n=1500, geocode=geoLocation, since='2011-04-30', until='2011-05-01') tweets <- rbind(tweets, searchTwitter("Osama Bin Laden", n=1500, geocode=geoLocation, since='2011-05-01', until='2011-05-02')) tweets <- rbind(tweets, searchTwitter("Osama Bin Laden", n=1500, geocode=geoLocation, since='2011-05-02', until='2011-05-03')) tweets <- rbind(tweets, searchTwitter("Osama Bin Laden", n=1500, geocode=geoLocation, since='2011-05-03')) times <- sapply(tweets, function(x) format(x$created, "%m-%d %H:00",tz = "America/Denver")) users <- sapply(tweets, function(x) x$screenName) times <- times[!duplicated(users)] #removing duplicate counts <- table(times) bp <- barplot(counts, main="Counts of 'Osama Bin Laden' Tweets by Hour\nwithin 6 miles of Colorado State Capitol Building", col="lightblue", border=NA, ylim=c(0,300),las=3 ) lines(spline(counts ~ bp), lwd=3, lty="dashed", col="darkblue")