Skip to content

Instantly share code, notes, and snippets.

@SwampThingPaul
Created November 3, 2019 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SwampThingPaul/930389147021ca6840726f46f23bb958 to your computer and use it in GitHub Desktop.
Save SwampThingPaul/930389147021ca6840726f46f23bb958 to your computer and use it in GitHub Desktop.
#CERF2019 data retrieval
##
## Code was compiled by Paul Julian
## contact info: pauljulianPhD@gmail.com
# Some helpful sites
#https://rtweet.info
#https://www.johnlittle.info/project/custom/rtweet/network_graph_more_examples.nb.html
#https://www.earthdatascience.org/courses/earth-analytics/get-data-using-apis/use-twitter-api-r/
#http://www.rdatamining.com/docs/twitter-analysis-with-r
library(rtweet)
library(plyr)
library(httpuv)
library(AnalystHelper);#devtools::install_github("SwampThingPaul/AnalystHelper")
library(wesanderson)
cols=wes_palette("Zissou1")
# data --------------------------------------------------------------------
rt.cerf=search_tweets2("#CERF2019",include_rts = TRUE,lang = "en",type = "mixed",n=20000)
tot.dat=ts_plot(rt.cerf,"1 days")
tot.dat$data
cum.dat=data.frame(tot.dat$data)
cum.dat$cum_count=cumsum(cum.dat$n)
cum.dat$time=date.fun(cum.dat$time,form="%F",tz="UTC")
# Plot --------------------------------------------------------------------
xlim.val=date.fun(c("2019-10-25",as.character(Sys.time()+ddays(1))),form="%F",tz="UTC")
xmaj=seq(xlim.val[1],xlim.val[2],"5 days");xmin=seq(xlim.val[1],xlim.val[2],"1 days")
par(family="serif",oma=c(1.5,2,1,0.25),mar=c(1.5,2,0.5,1))
ylim.val=c(0,300);by.y=100;ymaj=seq(ylim.val[1],ylim.val[2],by.y);ymin=seq(ylim.val[1],ylim.val[2],by.y/2)
plot(cum_count~time,cum.dat,xlim=xlim.val,ylim=ylim.val,yaxt="n",xaxt="n",ylab=NA,xlab=NA,type="n",yaxs="i")
abline(h=ymaj,v=xmaj,lty=3,col="grey")
with(subset(cum.dat,time!=date.fun(Sys.Date(),form="%F",tz="UTC")),shaded.range(time,rep(0,length(time)),cum_count,cols[1],lty=1))
with(subset(cum.dat,time!=date.fun(Sys.Date(),form="%F",tz="UTC")),lines(time,n,lwd=2,col=cols[4]))
axis_fun(1,line=-0.5,xmaj,xmin,format(xmaj,"%b-%d"))
axis_fun(2,ymaj,ymin,ymaj);box(lwd=1)
mtext(side=2,line=2.75,"Count")
mtext(side=1,line=1.75,"Date (Month-Day)")
mtext(side=3,expression(italic("#CERF2019")))
legend("topleft",
legend=c("Source: Data collected from Twitter's REST API via rtweet","Cumulative Total","Daily Total"),
pch=c(NA,22,NA),pt.bg=c(NA,adjustcolor(cols[1],0.5),NA),pt.cex=2,
lty=c(NA,NA,1),lwd=c(NA,NA,2),col=c(NA,NA,cols[3]),
cex=c(0.8,1,1),bty="n",y.intersp=1,x.intersp=0.75,xpd=NA,xjust=0.5)
## End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment