Created
November 9, 2019 03:04
-
-
Save dmackinnon1/26444ab7ea8f372f0a75f466adcbc5c1 to your computer and use it in GitHub Desktop.
simple rtweet example
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
install.packages("libcurl") | |
install.packages("curl") | |
install.packages("httr") | |
install.packages("rtweet") | |
install.packages("ggplot2") | |
install.packages("maps") | |
install.packages("dyplr") | |
install.packages("igraph") | |
install.packages("network") | |
install.packages("sna") | |
install.packages("ggraph") | |
install.packages("visNetwork") | |
install.packages("threejs") | |
install.packages("networkD3") | |
install.packages("ndtv") | |
# | |
# Fetch and save tweets for a given hashtag | |
# | |
library(rtweet) | |
library(igraph) | |
tag <- "mtbos" | |
file_name <- paste(tag,Sys.time(),".csv", sep="") | |
file_name <- gsub(":","_",file_name) | |
file_name <- gsub(" ","_",file_name) | |
tag <- paste("#",tag) | |
tagTweets <- search_tweets(tag, n=1000) | |
print(paste("saving data to file: ",file_name)) | |
write_as_csv(flatten(tagTweets),file_name) | |
## reload and display | |
twitterData <- unflatten(read_twitter_csv(file_name)) | |
twitterNetworkGraph <- network_graph(twitterData,.e=c("retweet")) | |
for (i in 1:length(V(twitterNetworkGraph))) { | |
V(twitterNetworkGraph)[i]$followers <- twitterData[which(twitterData$screen_name==V(twitterNetworkGraph)[i]$name),]$followers_count[1] | |
if (is.na(V(twitterNetworkGraph)[i]$followers)){V(twitterNetworkGraph)[i]$followers <-0 } | |
if (V(twitterNetworkGraph)[i]$followers < 1000){V(twitterNetworkGraph)[i]$display="white"} | |
if (V(twitterNetworkGraph)[i]$followers > 1000){V(twitterNetworkGraph)[i]$display="grey"} | |
if (V(twitterNetworkGraph)[i]$followers > 5000){V(twitterNetworkGraph)[i]$display="black"} | |
if (V(twitterNetworkGraph)[i]$followers > 20000){V(twitterNetworkGraph)[i]$display="blue"} | |
} | |
l <- layout_with_kk(twitterNetworkGraph) | |
plot(twitterNetworkGraph,edge.arrow.size=.2, vertex.size=3,vertex.color=V(twitterNetworkGraph)$display,vertex.label.cex=0.8, edge.color='grey', vertex.label="") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment