Skip to content

Instantly share code, notes, and snippets.

@reuning
Last active January 22, 2020 16:59
Show Gist options
  • Save reuning/a0cdc00505f7e18af6363c7ca60be658 to your computer and use it in GitHub Desktop.
Save reuning/a0cdc00505f7e18af6363c7ca60be658 to your computer and use it in GitHub Desktop.
Simple script to grab tweets with polling info and process them to see what leads to retweets.
library(rtweet)
library(data.table)
library(ggplot2)
library(stringr)
tweets <- as.data.table(get_timeline("PpollingNumbers", n=3200))
tweets <- tweets[is_retweet!=TRUE,]
tweets[,nat:=grepl("National", text, ignore.case = T) &
grepl("Biden", text, ignore.case = T) &
!grepl("students", text, ignore.case=T) &
!grepl("favor", text, ignore.case=T) &
!grepl("matchup", text, ignore.case=T)&
!grepl("non-white", text, ignore.case=T)&
!grepl("african", text, ignore.case=T)&
!grepl("general|genral", text, ignore.case=T)&
!grepl(" GE", text, ignore.case=F)&
!grepl("hispanic", text, ignore.case=T)&
!grepl("head-2-|head-to", text, ignore.case=T)&
!grepl("consider", text, ignore.case=T)&
!grepl("trust", text, ignore.case=T)&
!grepl("job", text, ignore.case=T)&
!grepl("won", text, ignore.case=T)]
tweets[,biden:=as.numeric(str_match(str_match(text, "Biden[: ]*\\d*"), "\\d+"))]
tweets[,sanders:=as.numeric(str_match(str_match(text, "Sanders[: ]*\\d*"), "\\d+"))]
tweets[,comp:=cut(sanders-biden, breaks=c(20,-1,-5,-10,-15,-100),
labels=rev(c("Sanders Tied or\nBeating Biden",
"Biden +1 to +5",
"Biden +6 to +10",
"Biden +11 to +15",
"Biden +16 or more")), ordered_result = T)]
tweets[,raw_dif:=sanders-biden]
# View(tweets[nat==TRUE,.(date,retweet_count, sanders, biden, comp, text)])
tweets[,date:=as.Date(created_at)]
library(ggthemes)
# tweets$comp
ggplot(tweets[nat==TRUE & !is.na(comp),], aes(x=date, y=retweet_count, fill=comp)) +
geom_point(pch=21, color='gray') +
theme_minimal() + scale_y_log10() + geom_smooth(aes(col=comp),
se = F,
formula=y~s(x),
method='gam') +
scale_color_brewer("Results in Tweet", type='seq', palette = 'YlGnBu',
guide = guide_legend(reverse = TRUE)) +
scale_fill_brewer("Results in Tweet", type='seq', palette = 'YlGnBu',
guide = guide_legend(reverse = TRUE)) +
labs(y='Number of Retweets', caption='Data collected from @PpollingNumbers',
x='')
ggsave('polling_rts.png', height=4, width=7, units='in')
ggplot(tweets[nat==TRUE & !is.na(comp),], aes(x=raw_dif, y=retweet_count)) +
geom_point() +
theme_minimal() + scale_y_log10() +
geom_smooth(se = F,
formula=y~s(x),
method='gam') +
labs(y='Number of Retweets', x='Sanders-Biden',
caption='Data collected from @PpollingNumbers')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment