Skip to content

Instantly share code, notes, and snippets.

@Sandy4321
Forked from abresler/twitter_node_graph_rcharts
Created December 17, 2015 16:11
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 Sandy4321/783432ac37153a038039 to your computer and use it in GitHub Desktop.
Save Sandy4321/783432ac37153a038039 to your computer and use it in GitHub Desktop.
Interactive twitter node graph
require(twitteR)
require(devtools)
install_github('rCharts', 'ramnathv') ## install rCharts
require(data.table)
#Register Twitter
## A real example, but using a fictitious consumerkey and consumer
## secret - you'll need to supply your own
#register access to twitter API
library(RCurl)
# Set SSL certs globally
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
require(twitteR)
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "https://api.twitter.com/oauth/authorize"
consumerKey <- "YOURKEY"
consumerSecret <- "YOURSECRE"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,consumerSecret=consumerSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)
twitCred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
registerTwitterOAuth(twitCred)
user <- getUser('abresler') ## Enter whoever you want
userFriends <- user$getFriends()
userFollowers <- user$getFollowers()
userNeighbors <- union(userFollowers, userFriends)
userNeighbors.df = twListToDF(userNeighbors)
userNeighbors.df[userNeighbors.df=="0"]<-1
userNeighbors.df$logFollowersCount <-log(userNeighbors.df$followersCount)
userNeighbors.df$logFriendsCount <-log(userNeighbors.df$friendsCount)
kObject.log <- data.frame(userNeighbors.df$logFriendsCount,userNeighbors.df$logFollowersCount)
##Run the K Means algorithm, specifying 2 centers
user2Means.log <- kmeans(kObject.log, centers=6, iter.max=10, nstart=100)
##Add the vector of specified clusters back to the original vector as a factor
userNeighbors.df$cluster <- user2Means.log$cluster
dt = data.table(userNeighbors.df) #create data.table
dt[location == '',location := 'Unknown'] #change blank location to unknown
dt$detail =
sprintf("<table border='2' cellpadding='4' style='line-height:1.55'><tbody><tr><th colspan='5.5'><span style='font-size:12px;'><span style='font-family:lucida sans unicode,lucida grande,sans-serif;'>%1$s</span></span></th></tr><tr><td><span style='font-size:8px;'><span style='font-family:lucida sans unicode,lucida grande,sans-serif;'><img src='%2$s' height='60' width='60'></td><td style='text-align: justify;'><span style='font-size:8px;'><span style='font-family:lucida sans unicode,lucida grande,sans-serif;'><strong>Screen Name:</strong> %3$s<br><strong>Location:</strong> %4$s<br><strong>Follower(s):</strong> %5$s<br><strong>Friend(s):</strong> %6$s<br><strong>Joined Twitter:</strong> $%8$s<br><strong>Description:</strong> $%7$s</span></td></tr></tbody></table>",
dt$name,
dt$profileImageUrl,
dt$screenName,
dt$location,
dt$followersCount,
dt$friendsCount,
dt$description,
dt$created
)
dt =
dt[,c(2:5,9:10,13:15,17:20), with = F]
setkey(dt,'cluster')
colors = c('#000000','#ff0000','#b8860b','#0000ff','#008000','#800080') #add custom colors
p2 <- nPlot(logFollowersCount ~ logFriendsCount, group = 'cluster', data = dt, type = 'scatterChart')
p2$yAxis(axisLabel = '')
p2$xAxis(axisLabel = '')
p2$chart(color = colors)
p2$chart(showControls = F) #Turns off the controller that shows grouped/stack
p2$chart(showDistX = F) #Freezes x-axis & shows tick
p2$chart(showDistY = F)
p2$yAxis( tickFormat="#!function(d) {return d3.format(',f')(d)}!#" )
p2$xAxis( tickFormat="#!function(d) {return d3.format(',f')(d)}!#" )
p2$chart(tooltipContent = "#! function(key, x, y, e){
return '<p>' + e.point.detail + '</p>'
} !#")
p2$addControls("x", value = "Log Friends",
values = names(dt)[c(11,4,12,7,10,2,3,8,1,9)])
p2$addControls("y", value = "Log Friends",
values = names(dt)[c(10,2,3,1,8,7,12,11,4)])
p2$addControls("group", value = "cluster",
values = names(dt)[c(12,6,5)])
p2$addFilters('cluster')
p2$templates$script = "http://timelyportfolio.github.io/rCharts_nvd3_templates/script_multiselect.html"
p2$publish(host = 'gist', 'twitter_node_graph')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment