Created
December 9, 2018 17:23
-
-
Save knbknb/9de0db6aae91b60346d174a840145ec8 to your computer and use it in GitHub Desktop.
Create pretty network-diagram of tweets with visjs, after custom filtering. (WORK IN PROGRESS)
This file contains hidden or 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
# create pretty network-diagram of tweets, | |
# captured with Gephi's Twitter plugin, | |
# exported as JSON | |
# with more sophisticated filtering | |
# knbknb 20181209 | |
library(visNetwork) | |
library(tidyverse) | |
workdir <- "/home/knut/gephi/" | |
# infile created with "Export as ..." Feature of Gephi Desktop App | |
infile <- "twitter-neurips2018--tweetnetwork.gephi.json" | |
outfile <- paste0(workdir, infile, ".html") | |
gephi_json_str <- readr::read_file(file.path(workdir, infile)) | |
# convert stringified JSON to R data structure | |
# (list of (sublists, data frames, empty data frames)) | |
gephi_json <- jsonlite::fromJSON(txt = gephi_json_str) | |
# gephi_json is a JSON object with 2 keys: | |
# {"nodes": [], "edges": []} | |
# both are data frames with sublists | |
# str(gephi_json, max.level = 2) # long output | |
map_dbl(gephi_json, nrow) # number of elements | |
map_dbl(gephi_json, length) # number of attributes | |
nodes <- gephi_json$nodes %>% | |
nest(attributes) %>% | |
filter(str_detect(label, "@")) %>% | |
select(id, label, size) | |
dim(nodes) | |
edges <- gephi_json$edges %>% | |
rename("from" = source, "to"=target) %>% | |
select(-attributes, -color) | |
dim(edges) | |
# edges2 <- edges %>% semi_join(nodes, by=c("to"="id") ) | |
#g <- igraph::graph_from_data_frame(d= edges2, directed = TRUE, | |
# vertices = nodes) | |
#summary(g) | |
#plot(g) | |
# enable_phys = FALSE: | |
# - much shorter rendering times even for larger graphs | |
# - layout is ugly | |
# enable_phys = TRUE: | |
# - startup + rendering time can be several minutes | |
# - layout is better (repulsion between nodes) | |
enable_phys <- TRUE | |
nw <- visNetwork::visNetwork(nodes = nodes, edges = edges, | |
width = 2000, height = 2000 | |
) %>% | |
#visEdges(smooth = list(type="discrete", forceDirection="vertical")) %>% | |
#visOptions(manipulation = FALSE) %>% | |
visPhysics(enabled = enable_phys) | |
visNetwork::visSave(nw, outfile) | |
# open a web browser | |
browseURL(url = outfile, browser = "google-chrome") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment