Chapter 10 Advanced

At this point, if you have read the book up to here, you have been explored all the difference types of graphs that we can build with the twinetverse. Now we can introduce two more functions to bind edges together and form more complex graphs.

Let’s take two of our previous graphs and merge them into one. Let’s build a graph where we not only connect users to the hashtags they use but also connect the hashtags used in the same tweets together.

# TK <- readRDS(file = "token.rds")
tweets <- search_tweets("rstats", n = 1000, token = TK, include_rts = FALSE, lang = "en")
## Searching for tweets...
## Finished collecting tweets!

Technically, the two graphs we want to merge are below.

# Users to hashtags
users <- tweets %>% 
  gt_edges(screen_name, hashtags) %>% 
  gt_nodes() %>% 

# hashtags co-mentions
hashtags <- tweets %>% 
  gt_co_edges(hashtags) %>% 
  gt_nodes() %>% 

Building two graphs, as above, to then merge them into one would not be convienient. It works differently in graphTweets, we do not need to build two full graphs, we only need to bind edges together and thus can do it in one single pipe (%>%).

net <- tweets %>% 
  gt_edges(screen_name, hashtags) %>% 
  gt_co_edges_bind(hashtags) %>% 
  gt_nodes() %>% 

We just call gt_co_edges_bind (in essence the same function as gt_co_edges) to bind those edges to previously piped edges. This would totally work the other way around too.

net <- tweets %>% 
  gt_co_edges(hashtags) %>%
  gt_edges_bind(screen_name, hashtags) %>% 
  gt_nodes() %>% 

You can unpack the network just as before, though we bound edges, everything remains the same from here onward.

c(edges, nodes) %<-% net

edges <- edges2sg(edges)
nodes <- nodes2sg(nodes)

sigmajs() %>% 
  sg_nodes(nodes, id, label, size) %>% 
  sg_edges(edges, id, source, target) %>% 
  sg_neighbours() %>% 
  sg_layout(layout = igraph::layout_components) %>% 
    colors = c(
  ) %>% 
    minNodeSize = 1,
    maxNodeSize = 2.5,
    edgeColor = "default",
    defaultEdgeColor = "rgba(211, 211, 211, 0.4)"
## Found # 75 clusters

You can technically bind any edges together. However, be careful that the resulting graph still makes sense. For instance, binding two networks of co-mentions, one of hashtags and another of users, would result in two entirely separate graph as none of the users are connected to hashtags or vice verse.