I've been exploring Jeff Gentry's twitteR package, which has a ton of great functionality for intereacting with twitter data in R. Today, I thought a bit about a problem I've noticed several times on twitter: users profiles are often only noisy signals of the content they tweet about!
I decided that a table of a user's commonly-used tweets might give a better sense of the content a user tweets about. My code to extract the hashtags is below (note: you'll need to load the twitteR package, and complete the OAuth Authentication first.. if you're having trouble with this, try visiting this page)
Here's the code I used:
tw = userTimeline("BarackObama", cainfo = x1, n = 3200) tw = twListToDF(tw) vec1 = tw$text extract.hashes = function(vec){ hash.pattern = "#[[:alpha:]]+" have.hash = grep(x = vec, pattern = hash.pattern) hash.matches = gregexpr(pattern = hash.pattern, text = vec[have.hash]) extracted.hash = regmatches(x = vec[have.hash], m = hash.matches) df = data.frame(table(tolower(unlist(extracted.hash)))) colnames(df) = c("tag","freq") df = df[order(df$freq,decreasing = TRUE),] return(df) } dat = head(extract.hashes(vec1),50) dat2 = transform(dat,tag = reorder(tag,freq)) library(ggplot2) p = ggplot(dat2, aes(x = tag, y = freq)) + geom_bar(fill = "blue") p + coord_flip() + labs(title = "Hashtag frequencies in the tweets of the Obama team (@BarackObama)")
I like it, but I'd move alpha to alnum for the switch, in case, for example, the hashtag was something like #vote2014
ReplyDeleteThanks for the code ! I had an error at first, but adding "stat = "identity" " to the geom_bar() corrected the issue.
ReplyDeleteThanks for the code!! i am also getting an error like "Error: stat_count() must not be used with a y aesthetic."
ReplyDelete