forked from stathack/Presidential-Semantic-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresidentialsemantics.R
27 lines (24 loc) · 1.01 KB
/
presidentialsemantics.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
install.packages('twitteR', dependencies=T)
library("twitteR")
library("reshape2")
library("ggplot2")
library("plyr")
president<-function(x){
Romney=searchTwitter('@MittRomney', n=1500)
Obama=searchTwitter('@BarackObama', n=1500)
textRomney=laply(Romney, function(t) t$getText())
textObama=laply(Obama, function(t) t$getText())
resultRomney=score.sentiment(textRomney, positive.words, negative.words)
resultRomney$candidate='Romney'
resultObama=score.sentiment(textObama, positive.words, negative.words)
resultObama$candidate='Obama'
result<-merge(resultObama,resultRomney, all=TRUE)
result$candidate<-as.factor(result$candidate)
result$time<-date()
return(result)
}
cresult<-ddply(result, .(candidate),summarise, score.mean=mean(score))
p<-ggplot(result, aes(x=score, fill=candidate)) +
geom_histogram(binwidth=1, alpha=.5, position="identity") +
geom_vline(data=cresult, aes(xintercept=score.mean, colour=candidate),
linetype="dashed", size=1)