Created
May 7, 2020 21:18
-
-
Save aleszu/4c619e5229a6d11420f85141b0041861 to your computer and use it in GitHub Desktop.
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
library(tidyverse) | |
library(rtweet) | |
library(twinetverse) | |
JimJordan <- search_tweets2("https://twitter.com/Jim_Jordan/status/1257813096707641346", n = 15000, include_rts = T) | |
glimpse(JimJordan) | |
JimJordan <- JimJordan %>% | |
mutate(maga_label = str_detect(description, "MAGA")) %>% | |
glimpse() | |
net <- JimJordan %>% | |
gt_edges(source = name, target = retweet_name, tl=F) %>% # get edges | |
gt_nodes() # get nodes | |
# Convert to list | |
net <- net %>% | |
gt_collect() | |
# make nodes, edges | |
c(edges, nodes) %<-% net | |
nodes <- nodes2sg(nodes) | |
edges <- edges2sg(edges) | |
sigmajs() %>% | |
sg_nodes(nodes, id, label, size) %>% | |
sg_edges(edges, id, source, target) %>% | |
sg_layout(layout = igraph::layout_with_kk) %>% | |
sg_cluster( | |
colors = c( | |
"#000000" | |
) | |
) %>% | |
sg_settings( | |
minNodeSize = 1, | |
maxNodeSize = 20, | |
edgeColor = "default", | |
defaultEdgeColor = "#E8E8E8" | |
) %>% | |
sg_drag_nodes() | |
# Build netviz **** WITH COLOR ***** | |
net <- JimJordan %>% | |
gt_edges(source = name, target = retweet_name, maga_label, tl=F) %>% # get edges | |
gt_nodes() %>% # get nodes | |
gt_add_meta(name=maga_label, source=maga_label, target=maga_label) | |
# Convert to list | |
net <- net %>% | |
gt_collect() | |
# make nodes, edges | |
c(edges, nodes) %<-% net | |
nodes <- nodes2sg(nodes) | |
edges <- edges2sg(edges) | |
# Add colors | |
nodes$color <- ifelse(nodes$maga_label == "TRUE", "#b22222", | |
ifelse(nodes$maga_label == "FALSE", "#000000", | |
"#000000")) | |
glimpse(nodes) | |
table(nodes$color) | |
sigmajs() %>% | |
sg_nodes(nodes, id, label, color, size) %>% | |
sg_edges(edges, id, source, target) %>% | |
sg_layout(layout = igraph::layout_with_kk) %>% | |
sg_cluster( | |
colors = c( | |
"#000000" | |
) | |
) %>% | |
sg_settings( | |
minNodeSize = 1, | |
maxNodeSize = 20, | |
edgeColor = "default", | |
defaultEdgeColor = "#E8E8E8" | |
) %>% | |
sg_drag_nodes() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It comes from this line
This attempts to add meta data to the nodes based on variables present in the edges. The issue is that one node can have two different values of
maga_label
: one node can in one instance havemaga_label
asTRUE
anotherFALSE
. Because of that it creates duplicate nodes, I'll add a warning for that.In this instance I suggest you use the built-in method to add meta data
Then add
maga_label
in the resulting data.frame.Then everything works fine.