Created
May 30, 2013 07:45
-
-
Save tts/5676314 to your computer and use it in GitHub Desktop.
Tweets and RT's of #elag2013 plotted on a timeline by Twitter screen name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################### | |
# | |
# Tweets and RT's of #elag2013 plotted on a timeline by Twitter screen name | |
# | |
# Tuija Sonkkila 30.5.2013 | |
# | |
# The R code and most of the comments are from | |
# http://blog.ouseful.info/2012/02/06/visualising-activity-round-a-twitter-hashtag-or-search-term-using-r/ | |
# except when defining my own values etc of course | |
# | |
# OAuth help from | |
# http://danielgillis.wordpress.com/2012/07/20/twitter-r-twitter/ | |
# and http://dev.twitter.com | |
# | |
############################################################ | |
library(twitteR) | |
library(ROAuth) | |
library(RCurl) | |
library(plyr) | |
library(stringr) | |
library(ggplot2) | |
# Set SSL certs globally | |
# http://stackoverflow.com/a/16437729 | |
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))) | |
reqURL <- "https://api.twitter.com/oauth/request_token" | |
accessURL <- "https://api.twitter.com/oauth/access_token" | |
authURL <- "https://api.twitter.com/oauth/authorize" | |
cKey <- "your consumer key here" | |
cSecret <- "your consumer secret here" | |
credentials <- OAuthFactory$new(consumerKey=cKey, | |
consumerSecret=cSecret, | |
requestURL=reqURL, | |
accessURL=accessURL, | |
authURL=authURL) | |
credentials$handshake() | |
registerTwitterOAuth(credentials) | |
#Pull in a search around a hashtag. | |
searchTerm='#elag2013' | |
rdmTweets <- searchTwitter(searchTerm, n=1000) | |
# Tuija: got 892 | |
#Plot of tweet behaviour by user over time | |
#Based on @mediaczar's http://blog.magicbeanlab.com/networkanalysis/how-should-page-admins-deal-with-flame-wars/ | |
#Make use of a handy dataframe creating twitteR helper function | |
tw.df=twListToDF(rdmTweets) | |
#@mediaczar's plot uses a list of users ordered by accession to user list | |
## 1) find earliest tweet in searchlist for each user [ http://stackoverflow.com/a/4189904/454773 ] | |
tw.dfx=ddply(tw.df, .var = "screenName", .fun = function(x) {return(subset(x, created %in% min(created),select=c(screenName,created)))}) | |
## 2) arrange the users in accession order | |
tw.dfxa=arrange(tw.dfx,-desc(created)) | |
## 3) Use the username accession order to order the screenName factors in the searchlist | |
tw.df$screenName=factor(tw.df$screenName, levels = tw.dfxa$screenName) | |
#ggplot seems to be able to cope with time typed values... | |
ggplot(tw.df)+geom_point(aes(x=created,y=screenName)) | |
#Identify and colour the RTs... | |
#A helper function to remove @ symbols from user names... | |
trim <- function (x) sub('@','',x) | |
#Identify classic style RTs | |
tw.df$rt=sapply(tw.df$text,function(tweet) trim(str_match(tweet,"^RT (@[[:alnum:]_]*)")[2])) | |
tw.df$rtt=sapply(tw.df$rt,function(rt) if (is.na(rt)) 'T' else 'RT') | |
png("elag2013tweets.png", width=1280,height=800) | |
ggplot(tw.df)+geom_point(aes(x=created,y=screenName,col=rtt)) | |
dev.off() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment