Who is in the UN's Twitter network?

January 25, 2021
rtweet rstats

For a while now I have been interested in analyzing behavior on Twitter. As a platform Twitter provides unparalleled insight to elite discourse between politicians, experts, activists, and other actors. For international organizations (IOs) it offers a venue to try and reach this audience with thousands of communication experts working full time to try and craft messages that rise to the top of the ocean of tweets. But what do these networks look like? To try and tackle this question, I looked at the UN’s Twitter network using the rtweet package for R.

Analysis

To build the dataset for the UN Twitter network I took the UN’s official list of Twitter accounts, and gathered the friends (followed accounts) for each account. This gave me a dataset of all the base UN accounts and their friends on Twitter. I then looked at the frequency for the friends across the UN Twitter accounts and if the friend account was followed by 50% of the base UN accounts, I included it in the dataset.

The result is a dataset of 432 accounts with the vast majority being the official Twitter accounts of various IOs. As this dataset is based on the who these accounts are following and because IO’s social media accounts are very careful with who they follow, the resulting network is very close knit. Despite this, the top five users with the most followers in this network are not IOs. Bill Gates tops the list with 53 million followers, followed by five major news outlets, and then finally the @UN Twitter account. This contrast with the bottom of the list which is dominated by various UN permanent mission Twitter accounts, with the Permanent Mission of Monaco having the lowest number of followers.

You can explore the dataset below:

 

Using the data that we have gathered, it is easy to plot the expansion of the growth of UN network Twitter accounts. The rapid growth of Twitter accounts within this network (and probably generally) took off in 2009, with 117 new accounts before slowing down to the single digits after 2016.

New UN network accounts per year

 

This quick analysis doesn’t really answer my main question, which is who are these accounts tweeting to and how do they engage with each other and the public. Because of limitations of computing power and time, those are questions that I will probably have to explore another time.

 

R walkthrough

The rtweet package makes Twitter analysis a breeze. For my analysis I first created a list of the UN accounts I was interested in and then used the lookup_user function to get data for all of the UN accounts. In order to use rtweet you need to have a Twitter account as Twitter requires authorization to use their API. Luckily, the rtweet package makes this pretty painless, when you run a function that requires authorization, a browser window will pop up where you sign into Twitter and confirm authorization.

My first step was to create a list of UN accounts and then look up information about each account and store it in a data frame.

### list of UN accounts

un_accounts <- c("un","antonioguterres", "unpublications", "un_news_centre", "UNMediaLiaison","UNlibrary", "UNWebTV", "UN_Photo", "AfricaRenewal", "_unchronicle", "UNYearbook", "globalgoalsun", "unpeacekeeping", "UNDPPA", "un_spokesperson", "unocha","un_disarmament", "undesa", "UNDGACM_EN", "UNHumanRights", "UN_PGA", "unpeacebuilding", "UN_Careers")

### Pull UN data

un_accounts_data <- lookup_users(un_accounts)

I think because of the way that the API works, when you use this function you are essentially grabbing the most recent Tweet with some user data attached to it.

The next step is to grab the followers of each UN account. This is simple enough with a quick loop using a script that I modified from Joe Christian’s article on social media analysis. To avoid being rate limited (Twitter only allows us to pull so much information every 15 minutes) this loop only pulls five user accounts followers every five minutes.

### Get all the 'friends' who the account is following for all the UN accounts 

friend <- tibble()

for (i in seq_along(un_accounts_data$user_id)) {
  message("Getting followers for user #", i, " of 23")
  kk <- get_friends(un_accounts_data$screen_name[i],
                    n = un_accounts_data$friends_count[i],
                    retryonratelimit = TRUE)
  
  friend <- rbind(friend,kk)
  
  ### Only pull 5 accounts every five minutes to avoid being rate limited. 
  
  if(i %% 5 == 0){
    message("sleep for 5 minutes")
    Sys.sleep(5*60)}} 


 

Once you have the friend and the un_accounts_data dataframes, the rest of the analysis is just using dplyr and to merge them into a single data frame and to calculate the summary data.

### Add user ID from the UN accounts (target user)
### Change column names to use _from  and _to specifications
### _from == target account
### _to == friends of target account

from_named <- rename(friend, user_id_to = user_id, screen_name = user) %>% 
  left_join(select(un_accounts_data, screen_name, user_id)) %>% 
  rename(user_id_from = user_id,
         screen_name_from = screen_name) %>% 
  mutate(user_id_to = as.character(user_id_to))

### look up more detailed data for the friends of UN accounts. 

friend_data <- lookup_users(unique(from_named$user_id_to))

### merge the from_named and the to_named dataframe
### result is a df with screen names of both the target accounts and their followers plus user_ids

friend_named <- left_join(from_named, to_named)

friend_named_count <- friend_named %>% count(screen_name_to) %>% 
  filter(!is.na(screen_name_to))
  
### Get a list of all accounts that are followed by 50%+ UN accounts  

top_50_pct_accounts <- unique(filter(friend_named_count, n >= 12)$screen_name_to)

top_50_pct_accouts_count <- filter(friend_named_count, n >= 12) %>% 
  mutate(pct_un_follows = n/23) %>% 
  rename("screen_name" = "screen_name_to")

top_50_pct_accounts_data <- lookup_users(top_50_pct_accounts)

### Binding of un_accounts_data and top_50_pct_accounts data
### Removes any duplicate data from UN accounts 
### Also add in more specific dates on account creation

full_df <- rbind(un_accounts_data, filter(top_50_pct_accounts_data, !screen_name %in% un_accounts_data$screen_name)) %>% 
  mutate(account_created_at_date = as.Date(account_created_at),
                account_created_at_year = lubridate::year(account_created_at_date)) %>% 
  distinct() %>% 
  left_join(., top_50_pct_accouts_count)

The final dataframe full_df is the same full version dataset you can explore above.

One cool thing about rtweet is that it doesn’t just let download data from Twitter, you can also make lists, follower users, and DM people all programatically through R. This makes it really easy to make a Twitter list of the UN Twitter network that we have identified using the post_list function.

post_list(
  users = full_df$screen_name,
  name = "UN Twitter List",
  description = "Top Twitter accounts in the UN network")

Cleaning your Twitter account with R

July 2, 2021
rtweet rstats

Automate PowerPoint Production Using R

February 17, 2021
officer powerpoint DataViz rstats