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() %>% gt_collect() # hashtags co-mentions hashtags <- tweets %>% gt_co_edges(hashtags) %>% gt_nodes() %>% gt_collect()
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() %>% gt_collect()
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() %>% gt_collect()
You can unpack the network just as before. We’ll clean the graph a bit, remove the rstats hashtag and color the nodes by type (whether it is a user or hashtag).
c(edges, nodes) %<-% net edges <- edges2sg(edges) nodes <- nodes2sg(nodes) edges <- edges %>% edges2sg() %>% dplyr::filter( source != "#rstats", target != "#rstats" ) nodes <- nodes %>% nodes2sg() %>% dplyr::filter( id != "#rstats" ) nodes$color <- ifelse(nodes$type == "user", "#0084b4", "#1dcaff") sigmajs() %>% sg_nodes(nodes, id, label, size, color) %>% sg_edges(edges, id, source, target) %>% sg_neighbours() %>% sg_layout(layout = igraph::layout_components) %>% sg_settings( minNodeSize = 1, maxNodeSize = 2.5, edgeColor = "default", defaultEdgeColor = "rgba(211, 211, 211, 0.4)" )
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.