voson.tcn - Twitter Conversation Networks

CRAN status Downloads Total Dev Last Commit R build status

Twitter Conversation Networks and Analysis. This package uses the Twitter API v2 Early Access endpoints to collect tweets and generate networks for threaded conversations identified using the new tweet conversation identifier.

An introduction to the Twitter API v2 can be found here, and the Twitter Developer Application that includes early access here.

OAuth Authentication

This package currently uses app based authentication approach with an OAuth2 bearer token rather than a user based one that uses an OAuth1a token. Bearer tokens have read-only API access and higher rate-limits, whereas user tokens have lower rate-limits and broader permissions that are not required for searching and collecting tweets. To retrieve a bearer token, both the consumer key and consumer secret for a Developer Standard Project or Academic Research Project app (that has been approved to use the Twitter API v2 endpoints) are required. These can be found or created on the Twitter Developer Portals Projects & Apps page. If you already have your bearer token you can also assign it directly to a voson.tcn token object using the bearer string parameter.

Search Endpoint

By default the recent search endpoint is used that makes available for collection only tweets that were made within the last 7 days. If the user has an Academic Research Project they can also use the tcn_threads parameter endpoint = "all" to collect on full-archive conversation tweets.

If collecting on historical tweets a start_time = "2021-03-18T00:00:00Z" datetime parameter will need to be specified if the conversation is older than 30 days old (the default API search start time). The datetime is in UTC and ISO 8601 format passed as a string.


The API recent search endpoint where the conversation tweets are retrieved from has a rate-limit of 450 requests per 15 min (per app). A single request can retrieve 100 tweets, translating to an upper limit of 45,000 tweets per 15 mins. The full-archive search allows 300 requests of 500 tweets, translating to 150,000 tweets per 15 mins. There is also a limit of only 1 request per second for the full-archive search endpoint.

The tweet lookup endpoint used by tcn_tweets has a rate-limit of 300 requests of 100 tweets (30,000) per 15 minutes.

Tweet Caps

There is currently a cap of 500 thousand tweets that be collected per month per project under the Twitter API v2 Standard product track, and 10 million for the Academic Research track. These caps only apply to certain API endpoints, such as recent and full-archive search. The voson.tcn tcn_threads function uses the search endpoints and therefore contributes towards this cap, however the tcn_tweets function does not.

Project caps are only able to be checked from the Twitter Developer Console Dashboard.



Install the latest release via CRAN:


Install the latest development version:




Get Access Token

Retrieve and save an app bearer token using its consumer keys.


token <- tcn_token(consumer_key = "xxxxxxxx",
                   consumer_secret = "xxxxxxxx")

# alternatively a bearer token string can be assigned directly
token <- tcn_token(bearer = "xxxxxxxx")

# if you save the token to file this step only needs to be done once
saveRDS(token, "~/.tcn_token")

Collect Conversation Tweets

Using tweet urls collect conversation tweets and metadata to generate networks.

# read token from file
token <- readRDS("~/.tcn_token")

# choose a twitter conversation thread or multiple threads to collect
# e.g https://twitter.com/Warcraft/status/1372615159311699970, and
#     https://twitter.com/Warcraft/status/1372487989385965569

# can use any tweet or tweet id that is part of the conversation thread
# input is a list of tweet ids, tweet urls or combination of both
tweet_ids <- c("https://twitter.com/Warcraft/status/1372615159311699970",

# collect the conversation thread tweets for supplied ids           
tweets <- tcn_threads(tweet_ids, token)

# academic track historical endpoint - specify start_time and optionally end_time
tweets <- tcn_threads(tweet_ids, token = token,
                      endpoint = "all",
                      start_time = "2021-03-17T00:00:00Z")

The tcn_threads function produces a named list comprising a dataframe with tweets and metadata and a dataframe of users metadata.

Note: If using the standard product track only recent search API requests can be performed. No tweets older than 7 days will be collected in the conversation search. The tweets and any directly referenced tweets for the tweet id’s provided will still be collected however.

# [1] "tweets" "users" "errors" "meta"
# [1] 147
# [1] 118
# [1] 0
# [1] 2

Collect Specific Tweets

Using tweet urls or id’s it’s also possible collect specific tweets and their metadata.

# read token from file
token <- readRDS("~/.tcn_token")

# choose tweets to collect
# e.g https://twitter.com/Warcraft/status/1372615159311699970, and
#     https://twitter.com/Warcraft/status/1372487989385965569

tweet_ids <- c("https://twitter.com/Warcraft/status/1372615159311699970",

# collect the tweets for supplied ids           
tweets <- tcn_tweets(tweet_ids, token)

# [1] "tweets" "users" "errors"
# [1] 2
# [1] 1
# [1] 0

Generate Networks

Two types of networks can be generated from the tweets collected. An activity network in which tweets are the nodes and an actor network where Twitter users are the nodes. Edges are the relationships between nodes, in both networks these are either a reply or a quote, signifying for example that a tweet is a reply-to another tweet or that a user has replied to another user.

Create an activity network

The activity network has tweet metadata such as tweet metrics and author usernames as node attributes.

activity_net <- tcn_network(tweets, "activity")

# activity nodes dataframe structure
print(activity_net$nodes, n = 3)

# # A tibble: 148 x 11
#   tweet_id  user_id  source created_at text    public_metrics.~ public_metrics.~
#   <chr>     <chr>    <chr>  <chr>      <chr>              <int>            <int>
# 1 13726476~ 9427940~ Twitt~ 2021-03-1~ @Warcr~                0                0
# 2 13726461~ 1609030~ Twitt~ 2021-03-1~ @Patri~                0                0
# 3 13726452~ 1190870~ Twitt~ 2021-03-1~ @Warcr~                0                0
# # ... with 145 more rows, and 4 more variables:
# #   public_metrics.like_count <int>, public_metrics.quote_count <int>,
# #   profile.name <chr>, profile.username <chr>

# activity edges dataframe structure
print(activity_net$edges, n = 3)

# # A tibble: 122 x 3
#   from                to                  type      
#   <chr>               <chr>               <chr>     
# 1 1372636834971455494 1372630068162297860 replied_to
# 2 1372635200748937223 1372615159311699970 replied_to
# 3 1372634777275265029 1372615159311699970 replied_to
# # ... with 119 more rows

Create an actor network

The actor network has additional user profile metadata as node attributes.

actor_net <- tcn_network(tweets, "actor")

# actor nodes dataframe structure
print(actor_net$nodes, n = 3)

# # A tibble: 105 x 13
#   user_id source profile.name profile.profile~ profile.location profile.username
#   <chr>   <chr>  <chr>        <chr>            <chr>            <chr>           
# 1 275993~ Twitt~ "\U0001d43f~ https://pbs.twi~ England          Stab~       
# 2 133101~ Twitt~ "Andr ~      https://pbs.twi~ NA               virg~ 
# 3 240160~ Twitt~ "Sebast ~    https://pbs.twi~ NA               Nord~     
# # ... with 102 more rows, and 7 more variables: profile.created_at <chr>,
# #   profile.description <chr>, profile.verified <lgl>,
# #   profile.public_metrics.followers_count <int>,
# #   profile.public_metrics.following_count <int>,
# #   profile.public_metrics.tweet_count <int>,
# #   profile.public_metrics.listed_count <int>

# actor edges dataframe structure
print(actor_net$edges, n = 3)

# # A tibble: 124 x 6
#   from       to     type  tweet_id    created_at     text                       
#   <chr>      <chr>  <chr> <chr>       <chr>          <chr>                      
# 1 2759935913 24599~ reply 1372636834~ 2021-03-18T19~ "@Limp ~   @Warcraft @MSF_~
# 2 133101119~ 61033~ reply 1372635200~ 2021-03-18T19~ "@Warcraft @MSF_USA Coming~
# 3 2401609580 61033~ reply 1372634777~ 2021-03-18T19~ "@Warcraft @MSF_USA When d~
# # ... with 121 more rows

Network Graphs

Convert network to an igraph object and perform a simple plot of an actor network with node and edge labels.


g <- graph_from_data_frame(actor_net$edges, vertices = actor_net$nodes)

# plot graph
plot(g, layout = layout_with_fr(g),
     vertex.label = V(g)$profile.username,
     edge.label = E(g)$type,
     vertex.size = 3, edge.arrow.size = 0.2)