public
Created — forked from sckott/iworkfortheinternet.R

Code for searching Twitter using the twitteR #rstats package.

  • Download Gist
iworkfortheinternet.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
require(plyr); require(stringr); require(ggplot2); require(lubridate); require(twitteR)
 
##need to figure out why there are two datout objects and not just 1...
twitterPlot<-function(myString, n=1500, since=NULL, until=NULL){
 
since2<-as.character(strptime(since, '%Y-%m-%d')+2*24*60*60)
until2<-as.character(strptime(until, '%Y-%m-%d')+2*24*60*60)
 
datout_1 <- searchTwitter(myString, n = n, since=since, until=until)
datout_2 <- searchTwitter(myString, n = n, since=since2, until=until2)
datoutdf <- ldply(c(datout_1, datout_2), function(x) x$toDataFrame(), .progress="text")
 
 
#what is input string 135 is invalid in this locale about
#and why is it causing scrip to fail? happens with this grep...
actual <- grep(myString, datoutdf[,1], ignore.case=T)
 
datoutdf2 <- datoutdf[actual,]
 
datoutdf2$newtime <- round_date(datoutdf2[,4], "hour")
datoutdf2_ <- ddply(datoutdf2, .(newtime), summarise,
numtweets = length(newtime))
 
ret<-ggplot(droplevels(datoutdf2_), aes(newtime, numtweets)) +
theme_bw(base_size=18) +
geom_line(color="red") +
labs(x='\nTime', y='Number of tweets\n') +
opts(title=paste("Tweets containing", myString, sep=" "))
 
return(ret)
}
 
#make the ggplot2 object
working<-twitterPlot("I work for the internet", n = 1500, since='2011-11-11', until='2011-12-12')
 
#plot and save
working
ggsave("iworkfortheinternet.png")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.