Skip to content

Instantly share code, notes, and snippets.

@aleszu
Created May 7, 2020 21:18
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 aleszu/4c619e5229a6d11420f85141b0041861 to your computer and use it in GitHub Desktop.
Save aleszu/4c619e5229a6d11420f85141b0041861 to your computer and use it in GitHub Desktop.
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()
@JohnCoene
Copy link

It comes from this line

# ....
gt_add_meta(name=maga_label, source=maga_label, target=maga_label)

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 have maga_label as TRUE another FALSE. 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

net <- JimJordan %>% 
  gt_edges(source = name, target = retweet_name, maga_label, tl=F) %>% # get edges
  gt_nodes(meta = TRUE) # add meta data  

Then add maga_label in the resulting data.frame.

net <- net %>% 
  gt_collect()

# make nodes, edges
c(edges, nodes) %<-% net
nodes <- nodes2sg(nodes)
edges <- edges2sg(edges)

nodes <- mutate(nodes, maga_label = grepl("MAGA", description))

Then everything works fine.

# Add colors
nodes$color <- ifelse(nodes$maga_label == TRUE, "#b22222", 
                      ifelse(nodes$maga_label == FALSE, "#000000", 
                             "#000000"))

sigmajs() %>% 
  sg_nodes(nodes, id, label, color, size) %>% 
  sg_edges(edges, id, source, target) %>% 
  sg_layout(layout = igraph::layout_with_kk) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment