graphTweets 4.0 has been redisigned to work hand-in-hand with rtweet. Let’s start by getting some tweets. If you’re unsure how to get started, head over to the rtweet website, everything is very well explained. We’ll get 1,000 tweets on #rstats, exluding re-tweets.

Now we can start using graphTweets.

  1. Get the edges using gt_edges.
  2. Build an igraph object using gt_graph or collect results with gt_collect.


tweets %>% 
  gt_edges(screen_name, mentions_screen_name) %>% 
  gt_graph() -> graph

#> [1] "igraph"


If you do not want to return an igraph object, use gt_collect, it will return a list of two data.frames; edges and nodes.

tweets %>% 
  gt_edges(screen_name, mentions_screen_name) %>% 
  gt_collect() -> edges

#> [1] "edges" "nodes"

(It also returns nodes but it’s empty since we only ran gt_edges).

So far we only used gt_edges to extract the edges, we can also extract the nodes.

On graphTweets version 0.4.1 gt_nodes returns the number of edges the node is present in: n_edges. Here I used gt_collect, you can, again, use gt_graph if you want to return an igraph object.

Adding nodes has not bring much to table however, gt_nodes takes another argument, meta, which if set to TRUE will return meta data on each node, where availbale*.

tweets %>% 
  gt_edges(screen_name, mentions_screen_name) %>% 
  gt_nodes(meta = TRUE) %>% 
  gt_collect() -> graph

# lapply(graph, names) # names of data.frames returned

Note that you can also pass meta-data to edges if needed.

tweets %>% 
  gt_edges(screen_name, mentions_screen_name, created_at) %>% 
  gt_nodes(meta = TRUE) %>% 
  gt_collect() -> graph

Before we plot out graph, we’re going to modify some of the meta-data, a lot of NA are returned (where the meta-data was not available *).

Here I use sigmajs to plot the graph.

Let’s look at communities, we’ll return an igraph object with gt_graph so we can easily run a community finding algorithm from the igraph package.

tweets %>% 
  gt_edges(screen_name, mentions_screen_name) %>% 
  gt_graph() -> g

#> [1] "igraph"

Users to Hashtags

The same principles follow, we simply use gt_edges_hash and pass the hashtags column as returned by rtweet. This creates a tibble of edges from screen_name to hashtags used in each tweet.

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

We’ll visualise the graph with sigmajs. Let’s prepare the data to meet the library’s requirements.

  • We add id to both nodes and edges
  • We add rename a few columns to meet sigmajs’ convention
  • We color the nodes by type (hashtag or user)

Apologies for not getting into details here but sigmajs is very well documented, check the website if you want to understand it all.

Let’s visualise it.

We use sg_layout to layout the graph and sg_neightbours to highlight nodes on click.


You can also build networks of retweets.

* Some nodes are mentioned in tweets only and therefore have no meta-data associated.